From 96b18191a895c365c547af1bb858c65ecaffc411 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Thu, 4 Dec 2025 21:12:01 +0900 Subject: [PATCH] baekjoon 20251204 --- code_study/Baekjoon/python/2473.py | 35 ++++++++++++++++++++++++ code_study/Baekjoon/swift/2342_1.swift | 30 ++++++++++++++++++++ code_study/Baekjoon/swift/2342_2.swift | 0 code_study/Baekjoon/ts/2342_1.ts | 38 ++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 code_study/Baekjoon/python/2473.py create mode 100644 code_study/Baekjoon/swift/2342_1.swift create mode 100644 code_study/Baekjoon/swift/2342_2.swift create mode 100644 code_study/Baekjoon/ts/2342_1.ts diff --git a/code_study/Baekjoon/python/2473.py b/code_study/Baekjoon/python/2473.py new file mode 100644 index 0000000..7eb0cb7 --- /dev/null +++ b/code_study/Baekjoon/python/2473.py @@ -0,0 +1,35 @@ +import sys +input = sys.stdin.readline + +N = int(input()) +value = sorted(list(map(int, input().split()))) + +def twoPointer(fixIndex) : + absSum = 30**9 + fixNum = value[fixIndex] + L, vL, R, vR = fixIndex+1, value[fixIndex+1], len(value)-1, value[len(value)-1] + + while L != R : + temp = value[L] + value[R] + fixNum + + if absSum > abs(temp) : + absSum = abs(temp) + vL = value[L] + vR = value[R] + + if temp > 0 : + R -= 1 + else : + L += 1 + + return absSum, (fixNum, vL, vR) + +resultValue = 30**9 +resultPair = (0,0,0) +for i in range(len(value)-2) : + a, b = twoPointer(i) + if resultValue > a : + resultPair = b + resultValue = a + +print(" ".join(map(str, resultPair))) \ No newline at end of file diff --git a/code_study/Baekjoon/swift/2342_1.swift b/code_study/Baekjoon/swift/2342_1.swift new file mode 100644 index 0000000..e236c58 --- /dev/null +++ b/code_study/Baekjoon/swift/2342_1.swift @@ -0,0 +1,30 @@ +if let input = readLine(), + let seq = input.split(separator: " ").compactMap({Int($0)}) as? [Int] +{ + let force: [[Int]] = [ + [1,2,2,2,2], + [2,1,3,4,3], + [2,3,1,3,4], + [2,4,3,1,3], + [2,3,4,3,1] + ] + + var dp: [[[Int]]] = Array(repeating: Array(repeating: Array(repeating: 0, count: 5), count: 5), count: seq.count-1) + + func solve(_ step: Int, _ l: Int, _ r: Int) -> Int { + if step == seq.count-1 { return 0 } + + if dp[step][l][r] != 0 { return dp[step][l][r] } + + let target = seq[step] + + let moveLeftCost = solve(step + 1, target, r) + force[l][target] + let moveRightCost = solve(step + 1, l, target) + force[r][target] + + dp[step][l][r] = min(moveLeftCost, moveRightCost) + + return dp[step][l][r] + } + + print(solve(0, 0, 0)) +} diff --git a/code_study/Baekjoon/swift/2342_2.swift b/code_study/Baekjoon/swift/2342_2.swift new file mode 100644 index 0000000..e69de29 diff --git a/code_study/Baekjoon/ts/2342_1.ts b/code_study/Baekjoon/ts/2342_1.ts new file mode 100644 index 0000000..f8475f8 --- /dev/null +++ b/code_study/Baekjoon/ts/2342_1.ts @@ -0,0 +1,38 @@ +const inst: number[] = require("fs").readFileSync(0).toString().trim().split(" ").map(Number); +const steps: number = inst.length - 1; + +const force: number[][] = [ + [1,2,2,2,2], + [0,1,3,4,3], + [0,3,1,3,4], + [0,4,3,1,3], + [0,3,4,3,1] +]; + +let dp: number[][][] = Array.from( {length: 2}, (_, i) => + Array.from({length: 5}, () => + Array.from({length: 5}, () => + i===1 ? 0 : Infinity + ) + ) +); + +let currentIsZero: boolean = true; + +for(let i=steps-1; i >= 0; i--) { + const current: number = currentIsZero ? 0 : 1; + const next: number = currentIsZero ? 1 : 0; + const target: number = inst[i]; + + for(let l=0; l<5; l++) { + for(let r=0; r<5; r++) { + const moveLeft: number = dp[next][target][r] + force[l][target]; + const moveRight: number = dp[next][l][target] + force[r][target]; + dp[current][l][r] = Math.min(moveLeft, moveRight); + } + } + + currentIsZero = !currentIsZero; +} + +console.log(dp[currentIsZero ? 1 : 0][0][0]); \ No newline at end of file