Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4908b1c

Browse files
authored
Create 1658-minimum-operations-to-reduce-x-to-zero.kt
1 parentf3e4c67 commit4908b1c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
classSolution {
2+
funminOperations(nums:IntArray,x:Int):Int {
3+
var left=0
4+
var max=-1
5+
var cur=0
6+
var target= nums.sum()!!- x
7+
8+
for (rightin0 until nums.size) {
9+
cur+= nums[right]
10+
11+
while (left<= right&& cur> target) {
12+
cur-= nums[left++]
13+
}
14+
15+
if (cur== target) {
16+
max= maxOf(max, right- left+1)
17+
}
18+
}
19+
20+
returnif (max==-1)-1else nums.size- max
21+
}
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp