From 3189f2a185527145aa4352c2e0c5d3e052647019 Mon Sep 17 00:00:00 2001 From: youngjoo00 Date: Sat, 30 Mar 2024 00:21:19 +0900 Subject: [PATCH 1/4] =?UTF-8?q?add:=20BOJ=5F12852=5F1=EB=A1=9C=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EA=B8=B0=202.swift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...353\241\234\354\235\264\353\223\234.swift" | 43 ++++++++++++++++ ...3\247\214\353\223\244\352\270\260 2.swift" | 50 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 "youngjoo00/week_15/BOJ_12852_1\353\241\234 \353\247\214\353\223\244\352\270\260 2.swift" diff --git "a/youngjoo00/week_14/BOJ_11404_\355\224\214\353\241\234\354\235\264\353\223\234.swift" "b/youngjoo00/week_14/BOJ_11404_\355\224\214\353\241\234\354\235\264\353\223\234.swift" index e69de29..645db33 100644 --- "a/youngjoo00/week_14/BOJ_11404_\355\224\214\353\241\234\354\235\264\353\223\234.swift" +++ "b/youngjoo00/week_14/BOJ_11404_\355\224\214\353\241\234\354\235\264\353\223\234.swift" @@ -0,0 +1,43 @@ +// BOJ_11404_플로이드.swift + +let city = Int(readLine()!)! +let bus = Int(readLine()!)! +let INF = Int.max +var graph: [[Int]] = Array(repeating: Array(repeating: INF, count: city + 1), count: city + 1) + +// 본인은 가중치 0 +for i in 1...city { + graph[i][i] = 0 +} + +for _ in 0.. dp[i/2] + 1 { + dp[i] = dp[i/2] + 1 + path[i] = i / 2 + } + + // 현재 인덱스의 횟수 vs. 인덱스를 3로 나누고 + 1(나누는 데 사용한 횟수) + // 이 둘을 비교해서 적은 횟수를 dp[i] 에 담음 + if i % 3 == 0 && dp[i] > dp[i/3] + 1 { + dp[i] = dp[i/3] + 1 + path[i] = i / 3 + } + } + + print(dp[n]) + + var list: [Int] = [] + + // 인덱스를 갖고 역추적하기 + var current = n + + while current != 0 { + list.append(current) + current = path[current] + } + + list.forEach { print($0, terminator: " ") } +} From 418d05d12e021919045952516949e582fdca15aa Mon Sep 17 00:00:00 2001 From: youngjoo00 Date: Sat, 30 Mar 2024 18:31:07 +0900 Subject: [PATCH 2/4] add: BOJ_9252_LCS 2.swift --- youngjoo00/week_15/BOJ_9252_LCS 2.swift | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 youngjoo00/week_15/BOJ_9252_LCS 2.swift diff --git a/youngjoo00/week_15/BOJ_9252_LCS 2.swift b/youngjoo00/week_15/BOJ_9252_LCS 2.swift new file mode 100644 index 0000000..7f06a86 --- /dev/null +++ b/youngjoo00/week_15/BOJ_9252_LCS 2.swift @@ -0,0 +1,54 @@ +// BOJ_9252_LCS 2.swift + +let input1 = readLine()!.map { $0 } +let input2 = readLine()!.map { $0 } + +var dp = Array(repeating: Array(repeating: 0, count: input2.count + 1), count: input1.count + 1) + +// input1과 input2의 각 문자를 순회하며 dp 테이블 업데이트 +// 두 문자가 같다면 이전단계 까지의 최장 공통 수열이 가진 값 + 1 을 진행 +for i in 1...input1.count { + for j in 1...input2.count { + // 현재 문자가 같은 경우, 왼쪽 위 대각선 값에 1을 더해 현재 위치에 저장 + if input1[i-1] == input2[j-1] { + dp[i][j] = dp[i-1][j-1] + 1 + } else { + // 현재 문자가 다른 경우, 왼쪽 값과 위쪽 값 중 더 큰 값을 현재 위치에 저장 + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + } + } +} + +// dp 테이블의 마지막 요소 출력: input1과 input2의 최장 공통 부분 수열(LCS)의 길이 +print(dp[input1.count][input2.count]) + +// LCS를 추출하기 위한 결과 배열 +var result: [Character] = [] + +// dp 테이블의 오른쪽 아래에서 시작 +var i = input1.count +var j = input2.count + +// 역추적 시작 +while i > 0 && j > 0 { + // 위쪽으로 이동: 현재 위치의 값이 위쪽 값과 같은 경우 + if dp[i][j] == dp[i-1][j] { + i -= 1 + } else if dp[i][j] == dp[i][j-1] { // 왼쪽으로 이동: 현재 위치의 값이 왼쪽 값과 같은 경우 + j -= 1 + } else { + // 대각선으로 이동: 현재 위치의 값이 왼쪽 위 대각선 값 + 1인 경우. + // 해당 문자가 LCS에 포함되므로 결과 배열에 추가 + result.append(input1[i-1]) + i -= 1 + j -= 1 + } +} + +// 결과 배열을 뒤집어서 올바른 순서로 만듬 +let reversedResult = result.reversed() + +// 결과 배열이 비어있지 않다면, 결과 배열의 각 문자를 출력함 +if !reversedResult.isEmpty { + reversedResult.forEach { print($0, terminator: "") } +} \ No newline at end of file From d218572a86ab07856808f701fbe092d36a9d0569 Mon Sep 17 00:00:00 2001 From: youngjoo00 Date: Sun, 31 Mar 2024 13:21:40 +0900 Subject: [PATCH 3/4] =?UTF-8?q?add:=20BOJ=5F14002=5F=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EA=B8=B4=20=EC=A6=9D=EA=B0=80=ED=95=98=EB=8A=94=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=88=98=EC=97=B4=204.swift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.swift" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "youngjoo00/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.swift" diff --git "a/youngjoo00/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.swift" "b/youngjoo00/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.swift" new file mode 100644 index 0000000..e69de29 From de7b497ac3d2a0a610efffa7baf98241240255b4 Mon Sep 17 00:00:00 2001 From: youngjoo00 Date: Sun, 31 Mar 2024 13:22:50 +0900 Subject: [PATCH 4/4] =?UTF-8?q?add:=20BOJ=5F14002=5F=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EA=B8=B4=20=EC=A6=9D=EA=B0=80=ED=95=98=EB=8A=94=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=88=98=EC=97=B4=204.swift?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\266\204 \354\210\230\354\227\264 4.swift" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git "a/youngjoo00/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.swift" "b/youngjoo00/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.swift" index e69de29..a53fb91 100644 --- "a/youngjoo00/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.swift" +++ "b/youngjoo00/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.swift" @@ -0,0 +1,33 @@ +// BOJ_14002_가장 긴 증가하는 부분 수열 4.swift + +let n = Int(readLine()!)! +let array = readLine()!.split(separator: " ").map { Int($0)! } +var dp = Array(repeating: 1, count: n) + +for i in 0.. array[j] { + // dp 배열에 있는 값중 현재 순회하는 dp[i] 와 직전의 담아둔 값 + 1 중에 어떤 것이 더 큰지 비교 + if dp[i] < dp[j] + 1 { + dp[i] = dp[j] + 1 + } + } + } +} + +var LIS: [Int] = [] +var length = dp.max()! +print(length) + +// 역순으로 순회 +for i in (0..