Skip to content

카카오 2024 겨울#128

Open
sangYuLv wants to merge 3 commits intomainfrom
liv
Open

카카오 2024 겨울#128
sangYuLv wants to merge 3 commits intomainfrom
liv

Conversation

@sangYuLv
Copy link
Copy Markdown
Collaborator

@sangYuLv sangYuLv commented Apr 9, 2026

🔗 문제 링크

✔️ 소요된 시간

4시간

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces Swift solutions for three algorithmic challenges: 'Most Received Gift', 'Donut and Bar Graph', and 'Mountain Tiling'. The review feedback focuses on performance and code quality improvements, specifically suggesting the use of a dictionary for O(1) index lookups to avoid O(N) overhead in loops, adhering to Swift style conventions by avoiding semicolons in variable declarations, and optimizing the space complexity of the dynamic programming solution from O(N) to O(1).

Comment on lines +9 to +13

for gift in gifts {
let line = gift.split(separator: " ").map { String($0) }
let sender = friends.firstIndex(of: line[0])!
let receiver = friends.firstIndex(of: line[1])!
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

friends.firstIndex(of:)는 배열을 순회하므로 $O(N)$의 시간이 소요됩니다. 이를 gifts 루프 내에서 반복 호출하면 전체 시간 복잡도가 $O(G \times F)$가 되어 성능 저하의 원인이 될 수 있습니다. 친구 이름을 키로 하는 딕셔너리를 미리 생성하여 인덱스 조회를 $O(1)$로 최적화하는 것을 권장합니다.

Suggested change
for gift in gifts {
let line = gift.split(separator: " ").map { String($0) }
let sender = friends.firstIndex(of: line[0])!
let receiver = friends.firstIndex(of: line[1])!
let friendIndex = Dictionary(uniqueKeysWithValues: friends.enumerated().map { ($1, $0) })
for gift in gifts {
let line = gift.split(separator: " ").map { String($0) }
let sender = friendIndex[line[0]]!
let receiver = friendIndex[line[1]]!

import Foundation

func solution(_ edges: [[Int]]) -> [Int] {
var donut = 0; var stick = 0; var eight = 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Swift에서는 한 줄에 여러 구문을 작성할 때 세미콜론을 사용하지만, 가독성을 위해 각 변수를 별도의 줄로 선언하거나 쉼표(,)를 사용하여 나열하는 것이 더 권장되는 스타일입니다.

Suggested change
var donut = 0; var stick = 0; var eight = 0
var donut = 0, stick = 0, eight = 0

Comment on lines +6 to +18
var dp1 = [Int](repeating: 0, count: n+1)
var dp2 = [Int](repeating: 1, count: n+1)

for index in 1...n {
dp1[index] = (dp1[index - 1] + dp2[index - 1]) % 10007
if tops[index - 1] == 1 {
dp2[index] = (dp1[index - 1] * 2 + dp2[index - 1] * 3) % 10007
}
else {
dp2[index] = (dp1[index - 1] + dp2[index - 1] * 2) % 10007
}
}
return (dp1[n] + dp2[n]) % 10007
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

현재 DP 풀이는 $O(N)$의 공간 복잡도를 사용하고 있습니다. 점화식에서 이전 단계의 값만 참조하므로, 배열 대신 두 개의 변수만 사용하여 $O(1)$ 공간 복잡도로 최적화할 수 있습니다.

    var dp1 = 0
    var dp2 = 1
    
    for top in tops {
        let prevDp1 = dp1
        let prevDp2 = dp2
        dp1 = (prevDp1 + prevDp2) % 10007
        if top == 1 {
            dp2 = (prevDp1 * 2 + prevDp2 * 3) % 10007
        } else {
            dp2 = (prevDp1 + prevDp2 * 2) % 10007
        }
    }
    return (dp1 + dp2) % 10007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant