Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions WEEK10/프로그래머스_가장많이받은선물/liv.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 프로그래머스 - 가장 많이 받은 선물

import Foundation

func solution(_ friends: [String], _ gifts: [String]) -> Int {
var giftScore = [Int](repeating: 0, count: friends.count)
var history = [[String: Int]](repeating: [:], count: friends.count)
var next = [Int](repeating: 0, count: friends.count)

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])!
Comment on lines +9 to +13
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]]!


giftScore[sender] += 1
giftScore[receiver] -= 1

history[sender][line[1], default: 0] += 1
}

for (index, name) in friends.enumerated() {
var friend = index + 1
while friend < friends.count {
let friendName = friends[friend]
let fromMe = history[index][friendName] ?? 0
let fromFriend = history[friend][name] ?? 0

if fromMe > fromFriend { next[index] += 1 }
else if fromMe < fromFriend { next[friend] += 1 }
else {
let myScore = giftScore[index]
let friendScore = giftScore[friend]
if myScore > friendScore { next[index] += 1 }
else if myScore < friendScore { next[friend] += 1 }
}

friend += 1
}
}

return next.max() ?? 0
}
46 changes: 46 additions & 0 deletions WEEK10/프로그래머스_도넛과막대그래프/liv.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 프로그래머스 - 도넛과 막대 그래프

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

var answer = 0

var count = 0
edges.forEach { count = max(count, max($0[0], $0[1])) }

var inTable = [Int](repeating: 0, count: count + 1)
var outTable = [Set<Int>](repeating: [], count: count + 1)
edges.forEach { edge in
outTable[edge[0]].insert(edge[1])
inTable[edge[1]] += 1
}

var startItems = Set<Int>()
for (number, targets) in outTable.enumerated()
where targets.count >= 2 && inTable[number] == 0 {
answer = number
startItems = outTable[answer]
break
}

for item in startItems {
var visited = Set<Int>()
var current = item

while true {
if visited.contains(current) { donut += 1; break }

let nextItems = outTable[current]
visited.insert(current)

if nextItems.isEmpty { stick += 1; break }

if nextItems.count == 2 { eight += 1; break }

current = nextItems.first!
}
}

return [answer, donut, stick, eight]
}
19 changes: 19 additions & 0 deletions WEEK10/프로그래머스_산모양타일링/liv.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 프로그래머스 - 산 모양 타일링

import Foundation

func solution(_ n: Int, _ tops: [Int]) -> Int {
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
Comment on lines +6 to +18
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

}