내 풀이
import java.util.HashMap;
import java.util.Map;
class Solution {
public int solution(int[] topping) {
int answer = 0;
if (topping.length == 1) {
return 0;
}
Map<Integer, Integer> leftSet = new HashMap<>();
Map<Integer, Integer> rightSet = new HashMap<>();
for (int topp : topping) {
rightSet.put(topp, rightSet.getOrDefault(topp, 0) + 1);
}
for (int topp : topping) {
leftSet.put(topp, leftSet.getOrDefault(topp, 0) + 1);
int moveVal = rightSet.getOrDefault(topp, 0);
if (moveVal - 1 == 0) {
rightSet.remove(topp);
} else {
rightSet.put(topp, moveVal - 1);
}
if (leftSet.size() == rightSet.size()) {
answer++;
}
}
return answer;
}
}
다른사람 풀이 참고
class Solution {
public int solution(int[] topping) {
int answer = 0;
int[] left = new int[10001], right = new int[10001];
int ls = 0, rs = 0;
for(int i : topping){
if(right[i] == 0) rs++;
right[i]++;
}
for(int i : topping) {
right[i]--;
if (right[i] == 0) rs--;
if (left[i] == 0) ls++;
left[i]++;
if (rs == ls) answer++;
}
return answer;
}
}