当前位置:网站首页>Huawei machine test question: longest continuous subsequence
Huawei machine test question: longest continuous subsequence
2022-07-05 01:33:00 【JOEL-T99】
Scala Realization
Title Description :
- Yes N A sequence of positive integers , Given an integer sum
- Find the longest continuous subsequence so that their sum equals sum
- Returns the length of the secondary subsequence , If there is no sequence that meets the requirements return -1
remarks :
- The input sequence consists only of numbers and English commas , Numbers are separated by English commas
- Sequence length 1<=N<=200, The input sequence does not consider exceptions
- The title ensures that the input sequence meets the requirements
Example
Input :
- 1,2,3,4,2
- 6
Output :
- 3
analysis :
- 1,2,3 and 4,2 Both sequences can meet the requirements , So the longest continuous sequence is 1,2,3 So the result is 3
Input :
- 1,2,3,4,2
- 20
Output :
- -1
explain :
- There are no subsequences that meet the requirements , return -1
Code :
def main(args: Array[String]): Unit = {
// Get input
val elems = StdIn.readLine().split(",").map(_.toInt).toList
val sum = StdIn.readLine().toInt
// Declare variables
var tmp = 0
var maxLen = -1
// Calculation maxLen
for (i <- elems.indices) {
tmp += elems(i)
// Continuously increase the following number , If it is greater than sum Is out of , If it is equal to sum Reset maxLen
breakable {
for (j <- i + 1 until elems.length) {
tmp += elems(j)
// Out of the loop
if (tmp > sum) break
// Reset maxLen
if (tmp == sum) {
maxLen = Math.max(maxLen, j - i + 2)
break
}
// If you can't find it, set it to -1
maxLen = -1
}
}
// Reset tmp
tmp = 0
}
// Output results
println(maxLen)
}
边栏推荐
- Robley's global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
- [development of large e-commerce projects] performance pressure test - Performance Monitoring - heap memory and garbage collection -39
- Database postragesq role membership
- Using openpyxl module to write the binary list into excel file
- Yyds dry goods inventory [Gan Di's one week summary: the most complete and detailed in the whole network]; detailed explanation of MySQL index data structure and index optimization; remember collectio
- Include rake tasks in Gems - including rake tasks in gems
- Senior Test / development programmers write no bugs? Qualifications (shackles) don't be afraid of mistakes
- Exploration and Practice of Stream Batch Integration in JD
- Yyds dry inventory swagger positioning problem ⽅ formula
- Wechat applet: wechat applet source code download new community system optimized version support agent member system function super high income
猜你喜欢
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #
Take you ten days to easily complete the go micro service series (IX. link tracking)
[CTF] AWDP summary (WEB)
[wave modeling 1] theoretical analysis and MATLAB simulation of wave modeling
Exploration and practice of integration of streaming and wholesale in jd.com
流批一體在京東的探索與實踐
Chia Tai International Futures: what is the master account and how to open it?
Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
Yyds dry goods inventory kubernetes management business configuration methods? (08)
PowerShell:在代理服务器后面使用 PowerShell
随机推荐
Take you ten days to easily complete the go micro service series (IX. link tracking)
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
Basic operation of database and table ----- the concept of index
Game 280 of leetcode week
Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Chia Tai International Futures: what is the master account and how to open it?
PowerShell:在代理服务器后面使用 PowerShell
Database performance optimization tool
Analysis and comparison of leetcode weekly race + acwing weekly race (t4/t3)
Introduction to the gtid mode of MySQL master-slave replication
[swagger]-swagger learning
What sparks can applet container technology collide with IOT
Exploration and practice of integration of streaming and wholesale in jd.com
Discrete mathematics: reasoning rules
LeetCode周赛 + AcWing周赛(T4/T3)分析对比
phpstrom设置函数注释说明
Redis(1)之Redis简介
PHP Basics - detailed explanation of DES encryption and decryption in PHP
Take you ten days to easily complete the go micro service series (IX. link tracking)