-
Notifications
You must be signed in to change notification settings - Fork 0
카카오 2024 겨울 #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sangYuLv
wants to merge
3
commits into
main
Choose a base branch
from
liv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
카카오 2024 겨울 #128
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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])! | ||
|
|
||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 DP 풀이는 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 |
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
friends.firstIndex(of:)는 배열을 순회하므로gifts루프 내에서 반복 호출하면 전체 시간 복잡도가