Skip to content

Commit 9bb33de

Browse files
Merge pull request #2240 from CodingTestStudy2/이진희
[이진희] 2026/10/31
2 parents 0891a74 + 4bae69c commit 9bb33de

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int longestBalanced(int[] nums) {
3+
int maxLen = 0;
4+
int n = nums.length;
5+
6+
for (int i = 0; i < n; i++) {
7+
Set<Integer> evenSet = new HashSet<>();
8+
Set<Integer> oddSet = new HashSet<>();
9+
10+
for (int j = i; j < n; j++) {
11+
int current = nums[j];
12+
13+
if (current % 2 == 0) {
14+
evenSet.add(current);
15+
} else {
16+
oddSet.add(current);
17+
}
18+
19+
if (evenSet.size() == oddSet.size()) {
20+
maxLen = Math.max(maxLen, j - i + 1);
21+
}
22+
}
23+
}
24+
25+
return maxLen;
26+
}
27+
}

0 commit comments

Comments
 (0)