当前位置:网站首页>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)
}
边栏推荐
- 流批一体在京东的探索与实践
- Arbitrum: two-dimensional cost
- [wave modeling 1] theoretical analysis and MATLAB simulation of wave modeling
- Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
- Database postragesq PAM authentication
- Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
- 流批一體在京東的探索與實踐
- Phpstrom setting function annotation description
- WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
- Chia Tai International Futures: what is the master account and how to open it?
猜你喜欢

Remote control service
![[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #](/img/1c/deaf20d46e172af4d5e11c28c254cf.jpg)
[flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #

【CTF】AWDP总结(Web)

Armv8-a programming guide MMU (3)

Database performance optimization tool

【海浪建模3】三维随机真实海浪建模以及海浪发电机建模matlab仿真

Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #

微信小程序:独立后台带分销功能月老办事处交友盲盒

How to safely eat apples on the edge of a cliff? Deepmind & openai gives the answer of 3D security reinforcement learning

Blue Bridge Cup Square filling (DFS backtracking)
随机推荐
Intel sapphire rapids SP Zhiqiang es processor cache memory split exposure
微信小程序:全网独家小程序版本独立微信社群人脉
La jeunesse sans rancune de Xi Murong
MATLB|多微电网及分布式能源交易
WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?
The server time zone value ‘� й ��� ʱ 'is unrecognized or representatives more than one time zone【
Global and Chinese market of network connected IC card smart water meters 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
流批一体在京东的探索与实践
Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
Basic operation of database and table ----- phased test II
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
Yyds dry inventory swagger positioning problem ⽅ formula
Game 280 of leetcode week
Grabbing and sorting out external articles -- status bar [4]
PowerShell:在代理服务器后面使用 PowerShell
Kibana installation and configuration
Hedhat firewall
Global and Chinese market of veterinary thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
Single step debugging of master data reading of SAP commerce cloud products