Skip to content

Commit fe00bd1

Browse files
committed
yongseon: 13주차 알고리즘 풀이
1 parent d6925bd commit fe00bd1

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎w13/yongseon/귤고르기.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package w13.yongseon;
2+
3+
import java.util.*;
4+
5+
public class 귤고르기 {
6+
static Map<Integer, Integer> tangerineMap = new HashMap();
7+
8+
public int solution(int k, int[] tangerine) {
9+
int answer = 0;
10+
11+
for(int t : tangerine) {
12+
tangerineMap.put(t, tangerineMap.getOrDefault(t, 0) + 1);
13+
}
14+
15+
List<Integer> sortedKeys = new ArrayList<>(tangerineMap.keySet());
16+
sortedKeys.sort((size1, size2) -> tangerineMap.get(size2) - tangerineMap.get(size1));
17+
18+
for(Integer key: sortedKeys) {
19+
k -= tangerineMap.get(key);
20+
21+
answer++;
22+
23+
if(k <= 0) {
24+
break;
25+
}
26+
}
27+
28+
return answer;
29+
}
30+
}

‎w13/yongseon/디펜스게임.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package w13.yongseon;
2+
3+
import java.util.*;
4+
5+
6+
public class 디펜스게임 {
7+
8+
public int solution(int n, int k, int[] enemy) {
9+
int answer = 0;
10+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
11+
12+
for (int i = 0; i < enemy.length; i++) {
13+
n -= enemy[i];
14+
maxHeap.add(enemy[i]);
15+
16+
if (n < 0) {
17+
if (k > 0) {
18+
int maxEnemy = maxHeap.poll();
19+
n += maxEnemy;
20+
k--;
21+
} else {
22+
break;
23+
}
24+
}
25+
answer++;
26+
}
27+
return answer;
28+
}
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package w13.yongseon;
2+
3+
import java.util.*;
4+
5+
public class 롤케이크자르기 {
6+
public int solution(int[] topping) {
7+
int answer = 0;
8+
9+
// 각 토핑의 개수를 관리하는 맵 생성
10+
Map<Integer, Integer> toppingCount = new HashMap<>();
11+
Set<Integer> currentTopping = new HashSet<>();
12+
13+
// 전체 토핑의 개수를 초기화
14+
for (int t : topping) {
15+
toppingCount.put(t, toppingCount.getOrDefault(t, 0) + 1);
16+
}
17+
18+
// 토핑을 순회하면서 현재 세트와 남은 토핑 개수를 업데이트
19+
for (int i = 0; i < topping.length; i++) {
20+
int t = topping[i];
21+
22+
// 현재 토핑에 추가
23+
currentTopping.add(t);
24+
25+
// 남은 토핑에서 해당 토핑의 개수를 감소
26+
toppingCount.put(t, toppingCount.get(t) - 1);
27+
if (toppingCount.get(t) == 0) {
28+
toppingCount.remove(t);
29+
}
30+
31+
// 남은 토핑의 종류와 현재 토핑의 종류가 같으면 카운트 증가
32+
if (currentTopping.size() == toppingCount.size()) {
33+
answer++;
34+
}
35+
}
36+
37+
return answer;
38+
}
39+
}

‎w13/yongseon/주식가격.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package w13.yongseon;
2+
import java.util.*;
3+
4+
public class 주식가격 {
5+
static Stack<Integer> stack = new Stack<>();
6+
7+
public int[] solution(int[] prices) {
8+
int[] answer = new int[prices.length];
9+
10+
for(int i=0; i<prices.length; i++) {
11+
while (!stack.isEmpty() && prices[i] < prices[stack.peek()]) {
12+
answer[stack.peek()] = i-stack.peek();
13+
stack.pop();
14+
}
15+
16+
stack.push(i);
17+
}
18+
19+
while(!stack.isEmpty()) {
20+
answer[stack.peek()] = prices.length - stack.peek()-1;
21+
stack.pop();
22+
}
23+
24+
return answer;
25+
}
26+
}

0 commit comments

Comments
 (0)