Skip to content

Commit

Permalink
solved 보석 쇼핑 - 108.25ms 91.9mb
Browse files Browse the repository at this point in the history
  • Loading branch information
Endura0535 committed Jan 24, 2025
1 parent 63c2bed commit 7a4719e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Programmers/보석쇼핑/보석쇼핑_옥수빈.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.*;
import java.awt.Point;

class Solution {
public int[] solution(String[] gems) {
Set<String> gemSet = new HashSet<>(List.of(gems));
int gemCnt = gemSet.size();
Map<String, Integer> statusMap = new HashMap<>();
List<Point> list = new ArrayList<>();
int s = 0;
for (int e = 0; e < gems.length; e++) {
statusMap.put(gems[e], statusMap.getOrDefault(gems[e], 0) + 1);
if (statusMap.size() < gemCnt)
continue;
while (true) {
list.add(new Point(s + 1, e + 1));
s++;
statusMap.put(gems[s - 1], statusMap.get(gems[s - 1]) - 1);
if (statusMap.get(gems[s - 1]) == 0) {
statusMap.remove(gems[s - 1]);
break;
}
}
}

Collections.sort(list, (o1, o2) -> {
if (o1.y - o1.x == o2.y - o2.x)
return o1.x - o2.x;
return (o1.y - o1.x) - (o2.y - o2.x);
});

int[] answer = new int[2];
answer[0] = list.get(0).x;
answer[1] = list.get(0).y;

return answer;
}
}

0 comments on commit 7a4719e

Please sign in to comment.