-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path905.sort-array-by-parity.java
More file actions
30 lines (28 loc) · 955 Bytes
/
Copy path905.sort-array-by-parity.java
File metadata and controls
30 lines (28 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public int totalFruit(int[] tree) {
int sz = 2;
HashMap<Integer, Integer> lsm = new HashMap<Integer, Integer>();
Queue<Integer> q = new LinkedList<Integer>();
int ans = 0;
for(int tr: tree){
lsm.put(tr, lsm.getOrDefault(tr, 0)+1);
q.offer(tr);
Set<Integer> ky = lsm.keySet();
while(lsm.size() >2){
int curr = q.poll();
if(lsm.get(curr) == 1)
{
lsm.remove(curr);
break;
}else
lsm.put(curr, lsm.get(curr)-1);
}
int tres = 0;
for(int k:ky){
tres += lsm.get(k);
}
ans = Math.max(ans, tres);
}
return ans;
}
}