20250828 baekjoon

This commit is contained in:
songyc macbook 2025-08-28 21:16:57 +09:00
parent 8424b1dca2
commit 788d997b7a
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,22 @@
import java.util.*;
public class _1149 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
int[] RGB = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int dpR = RGB[0], dpG = RGB[1], dpB = RGB[2];
int prevR = dpR, prevG = dpG, prevB = dpB;
for(int i=1; i<N; i++) {
RGB = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
dpR = RGB[0] + Math.min(prevG, prevB);
dpG = RGB[1] + Math.min(prevR, prevB);
dpB = RGB[2] + Math.min(prevG, prevR);
prevR = dpR; prevG = dpG; prevB = dpB;
}
sc.close();
System.out.println(Math.min(dpR,Math.min(dpG, dpB)));
}
}

View File

@ -0,0 +1,17 @@
if let n = readLine(), let N = Int(n) {
if let line1 = readLine(), let rgb = line1.split(separator: " ").compactMap({Int($0)}) as? [Int], rgb.count==3 {
var dpR = rgb[0], dpG = rgb[1], dpB = rgb[2]
var prevR = dpR, prevG = dpG, prevB = dpB
for _ in 1..<N {
if let lineN = readLine(), let RGB = lineN.split(separator: " ").compactMap({Int($0)}) as? [Int], RGB.count==3 {
dpR = RGB[0] + min(prevG, prevB)
dpG = RGB[1] + min(prevR, prevB)
dpB = RGB[2] + min(prevR, prevG)
prevR = dpR; prevG = dpG; prevB = dpB
}
}
print(min(dpR, dpG, dpB))
}
}