20251031 baekjoon

This commit is contained in:
songyc macbook 2025-10-31 20:50:14 +09:00
parent 53931866bc
commit 711f05187f
2 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,37 @@
import sys
input = sys.stdin.readline
maxCost = 1000
N = int(input())
R = []
G = []
B = []
for _ in range(N) :
r, g, b = map(int, input().split())
R.append(r)
G.append(g)
B.append(b)
result = []
for i in range(3) :
dpR = [maxCost if i != 0 else R[0]]
dpG = [maxCost if i != 1 else G[0]]
dpB = [maxCost if i != 2 else B[0]]
for n in range(1, N) :
r, g, b = R[n], G[n], B[n]
dpR.append(r + min(dpG[n-1], dpB[n-1]))
dpG.append(g + min(dpR[n-1], dpB[n-1]))
dpB.append(b + min(dpR[n-1], dpG[n-1]))
if i==0 :
result.append(min(dpG[N-1], dpB[N-1]))
elif i==1 :
result.append(min(dpR[N-1], dpB[N-1]))
else :
result.append(min(dpR[N-1], dpG[N-1]))
print(min(result))

View File

@ -0,0 +1,51 @@
func opPriority(_ op: Character) -> Int {
return (op == "*") || (op == "/") ? 2 : (op == "+") || (op == "-") ? 1 : 0
}
func compareToPriority(_ op1: Character, _ op2: Character) -> Bool {
return opPriority(op1) >= opPriority(op2)
}
if let input = readLine() {
var result: String = ""
var op: [Character] = []
for c in input {
if "A" <= c && c <= "Z" {
result.append(c)
}
else {
if c == "(" {
op.append(c)
}
else if c == ")" {
while !op.isEmpty && op.last != "(" {
if let top = op.popLast() {
result.append(top)
}
}
if !op.isEmpty {
_ = op.popLast()
}
}
else {
while !op.isEmpty && compareToPriority(op.last!, c) {
if let top = op.popLast() {
result.append(top)
}
}
op.append(c)
}
}
}
while !op.isEmpty {
if let top = op.popLast() {
result.append(top)
}
}
print(result)
}