Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3fbd1f6 commit bb0af3dCopy full SHA for bb0af3d
kotlin/0525-contiguous-array.kt
@@ -0,0 +1,27 @@
1
+class Solution {
2
+ fun findMaxLength(nums: IntArray): Int {
3
+ var zero = 0
4
+ var one = 0
5
+ var res = 0
6
+
7
+ val diffIndex = HashMap<Int, Int> ()
8
9
+ for ((i, n) in nums.withIndex()) {
10
+ if (n == 0)
11
+ zero++
12
+ else
13
+ one++
14
+ if (one - zero !in diffIndex)
15
+ diffIndex[one - zero] = i
16
17
+ if (one == zero) {
18
+ res = one + zero
19
+ } else {
20
+ val idx = diffIndex[one - zero] ?: 0
21
+ res = maxOf(res, i - idx)
22
+ }
23
24
25
+ return res
26
27
+}
0 commit comments