Skip to content

Commit bb0af3d

Browse files
authored
Create 0525-contiguous-array.kt
1 parent 3fbd1f6 commit bb0af3d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

kotlin/0525-contiguous-array.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy