11501 주식
2805 나무자르기
14891 톱니바퀴
4179 불!
2616 소형기관차
지난 주 카카오 문제 풀이 (https://school.programmers.co.kr/learn/challenges?order=recent&partIds=37527 - 미로 탈출 명령어)
def solution(alp, cop, problems):
max_alp = max(max(problem[0] for problem in problems), alp)
max_cop = max(max(problem[1] for problem in problems), cop)
# DP 배열 초기화: 무한대로 설정하여 최소값 갱신이 용이하도록 함
dp = [[float('inf')] * (max_cop + 1) for _ in range(max_alp + 1)]
dp[alp][cop] = 0 # 초기 상태
for cur_alp in range(alp, max_alp + 1):
for cur_cop in range(cop, max_cop + 1):
if cur_alp < max_alp:
dp[cur_alp + 1][cur_cop] = min(dp[cur_alp + 1][cur_cop], dp[cur_alp][cur_cop] + 1)
if cur_cop < max_cop:
dp[cur_alp][cur_cop + 1] = min(dp[cur_alp][cur_cop + 1], dp[cur_alp][cur_cop] + 1)
for alp_req, cop_req, alp_rwd, cop_rwd, cost in problems:
if cur_alp >= alp_req and cur_cop >= cop_req:
next_alp = min(max_alp, cur_alp + alp_rwd)
next_cop = min(max_cop, cur_cop + cop_rwd)
dp[next_alp][next_cop] = min(dp[next_alp][next_cop], dp[cur_alp][cur_cop] + cost)
return dp[max_alp][max_cop]
# 예시
print(solution(10, 10, [[10,15,2,1,2],[20,20,3,3,4]]))
print(solution(0, 0, [[0,0,2,1,2],[4,5,3,1,2],[4,11,4,0,2],[10,4,0,4,2]]))
모두가 못 푼 첫 문제
다들 집에서 풀어보길
종빈
import java.util.*;
class Solution {
public int solution(int alp, int cop, int[][] problems) {
int alpMax = 0;
int copMax = 0;
for (int[] ints : problems) {
alpMax = Math.max(ints[0], alpMax);
copMax = Math.max(ints[1], copMax);
}
alpMax = Math.max(alpMax,alp);
copMax = Math.max(copMax,cop);
if(alp>=alpMax&&cop>=copMax) return 0;
int[][] dp = new int[alpMax + 1][copMax +1];
for (int i = 0; i < alpMax+1; i++) {
Arrays.fill(dp[i], Integer.MAX_VALUE-10000);
}
dp[alp][cop] = 0;
for (int i = alp; i <= alpMax; i++) {
for (int j = cop; j <= copMax; j++) {
if (i < alpMax) {
dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j] + 1);
}
if (j < copMax) {
dp[i][j + 1] = Math.min(dp[i][j + 1], dp[i][j] + 1);
}
for (int[] problem : problems) {
if (i >= problem[0] && j >= problem[1]) {
int next_alp = Math.min(alpMax, i + problem[2]);
int next_cop = Math.min(copMax, j + problem[3]);
dp[next_alp][next_cop] = Math.min(dp[next_alp][next_cop], dp[i][j] + problem[4]);
}
}
}
}
return dp[alpMax][copMax];
}
}