Skip to content

Commit 9742d78

Browse files
committed
solve: 프로그래머스 문제 풀이 - 3일차
1 parent 8f67401 commit 9742d78

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(int[] topping) {
5+
int n = topping.length;
6+
7+
int[] leftCount = new int[n];
8+
int[] rightCount = new int[n];
9+
10+
Map<Integer, Integer> leftMap = new HashMap<>();
11+
for (int i=0; i<n; i++) {
12+
leftMap.put(topping[i], leftMap.getOrDefault(topping[i], 0) + 1);
13+
leftCount[i] = leftMap.size();
14+
}
15+
16+
Map<Integer, Integer> rightMap = new HashMap<>();
17+
for (int i=n-1; i>=0; i--) {
18+
rightMap.put(topping[i], rightMap.getOrDefault(topping[i], 0) + 1);
19+
rightCount[i] = rightMap.size();
20+
}
21+
22+
int count = 0;
23+
for (int i=0; i<n-1; i++) {
24+
if (leftCount[i] == rightCount[i+1]) {
25+
count++;
26+
}
27+
}
28+
29+
return count;
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean solution(String[] phone_book) {
5+
boolean answer = true;
6+
7+
Arrays.sort(phone_book);
8+
9+
for (int i=1; i<phone_book.length; i++) {
10+
if (phone_book[i].startsWith(phone_book[i-1]))
11+
return false;
12+
}
13+
14+
return true;
15+
}
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(int[] priorities, int location) {
5+
int answer = 0;
6+
7+
int size = priorities.length;
8+
9+
// arr[] = {인덱스, 우선순위}
10+
int[] arr = new int[size];
11+
Queue<int[]> queue = new LinkedList<>();
12+
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
13+
for (int i=0; i<size; i++) {
14+
queue.offer(new int[]{i, priorities[i]});
15+
pq.offer(priorities[i]);
16+
}
17+
18+
while (!pq.isEmpty()) {
19+
int[] process = queue.poll();
20+
int priority = pq.peek();
21+
22+
// 우선순위가 가장 높으면
23+
if (process[1] == priority) {
24+
pq.poll();
25+
answer++;
26+
if (process[0] == location) {
27+
break;
28+
}
29+
} else {
30+
// 우선순위가 높은게 아니라면 가장 뒤로
31+
queue.offer(process);
32+
}
33+
}
34+
35+
// System.out.println(pq);
36+
37+
return answer;
38+
}
39+
}

0 commit comments

Comments
 (0)