Skip to content
Merged
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
3 changes: 2 additions & 1 deletion alstjr7437/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
|:----:|:---------:|:----:|:-----:|:----:|
| 1์ฐจ์‹œ | 2025.03.26 | BFS | [๋ฏธ๋กœ ํƒ์ƒ‰](https://www.acmicpc.net/problem/2178)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/2|
| 2์ฐจ์‹œ | 2025.04.08 | Heap | [์ตœ๋Œ€ํž™](https://www.acmicpc.net/problem/11279)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/7|
| 3์ฐจ์‹œ | 2025.04.30 | ํˆฌํฌ์ธํ„ฐ | [์ตœ๋Œ€ํž™](https://www.acmicpc.net/problem/7795)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/17|
| 3์ฐจ์‹œ | 2025.04.30 | ํˆฌํฌ์ธํ„ฐ | [๋จน์„ ๊ฒƒ์ธ๊ฐ€ ๋จนํž ๊ฒƒ์ธ๊ฐ€](https://www.acmicpc.net/problem/7795)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/17|
| 4์ฐจ์‹œ | 2025.05.13 | ๊ตฌํ˜„ | [ํ›„๋ณด ์ถ”์ฒœํ•˜๊ธฐ](https://www.acmicpc.net/problem/1713)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/24|
---
29 changes: 29 additions & 0 deletions alstjr7437/๊ตฌํ˜„/BOJ_1713.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let N = Int(readLine()!)!
let totalCount = Int(readLine()!)!
Copy link
Member

Choose a reason for hiding this comment

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

guard let votingCnt = Int(readLine() ?? "") else { exit(0) }

if votingCnt == 0 {
    exit(0)
}

์ „ ์ž๊พธ ์—๋Ÿฌ๋– ์„œ
"์„ค๋งˆ ๋ฌด์Šจ ํˆฌํ‘œ์ž 0๋ช…! ์ด๋Ÿฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•˜๋‚˜? ใ… ใ… " ํ•˜๋ฉด์„œ
์—ฌ๊ธฐ์— ์ด๋Ÿฐ ์กฐ๊ฑด๋„ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ทธ๋žฌ๋„ค์š” ใ…‹ใ…‹ใ…‹

let recommend = readLine()!.split(separator: " ").map { Int(String($0))! }

var result: [Int: (recommendCount: Int, time: Int)] = [:]
Copy link
Collaborator

Choose a reason for hiding this comment

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

๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ•˜์…จ๊ตฐ์š”.

var currentTime = 0
Copy link
Member

Choose a reason for hiding this comment

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

var candidates: [Int: (votedCnt: Int, order: Int)] = [:]
var order = 1

currentTime ๋ณ€์ˆ˜๋ช… ์ข‹๋„ค์š”, ์ €๋Š” order(์ˆœ์„œ)๋กœ ํ–ˆ์Šด๋‹ค.

Copy link
Collaborator

Choose a reason for hiding this comment

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

order ์ข‹๋„ค์š” ์ €๋Š” time? ํ•˜๋ ค๋‹ค๊ฐ€ ์™€๋‹ฟ์ง€์•Š์•„์„œ numberํ•ด์คฌ๋Š”๋ฐ, (์ €๋Š” ๊ธฐํ˜ธ~๋ฒˆ์„ symbol๋กœ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.) ํ—ท๊ฐˆ๋ฆฌ๋”๋ผ๊ตฌ์š”


for student in recommend {
if result[student] != nil {
result[student]!.recommendCount += 1
} else {
Comment on lines +9 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ตœ๊ทผ์— swiftyํ•˜๊ฒŒ ์ฝ”๋“œ ์“ฐ๋Š” ๋ฒ•์—๋Œ€ํ•ด ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ต์…”๋„ nil ๊ฐ’์„ ์ฒดํฌํ•ด์ฃผ๋Š” ์ฒด์ด๋‹์ด ์žˆ๋Š”๋ฐ ์ €๋„ ํ‚จ๋”์ฒ˜๋Ÿผ ์ต์ˆ™ํ•œ ๋ฐฉ๋ฒ•๋งŒ ์“ฐ๊ฒŒ ๋˜๋”๋ผ๊ณ ์š”. ์ฒด์ด๋‹์„ ์จ์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์“ธ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”!

์˜ˆ์‹œ

// ๊ธฐ์กด
if result[student] != nil {
    result[student]!.recommendCount += 1
}

// ์ฒด์ด๋‹
result[student]?.recommendCount += 1

Copy link
Member

Choose a reason for hiding this comment

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

์ด๊ฑฐ ์งฑ์งฑ swifty ใ…‡ใ…ˆํ•ฉ๋‹ˆ๋‹ค.

if result.count < N {
result[student] = (1, currentTime)
} else {
let removeValue = result.min {
if $0.value.recommendCount == $1.value.recommendCount {
return $0.value.time < $1.value.time
}
return $0.value.recommendCount < $1.value.recommendCount
}!.key

result.removeValue(forKey: removeValue)
result[student] = (1, currentTime)
}
}
currentTime += 1
}

print(result.keys.sorted().map { String($0) }.joined(separator: " "))
Comment on lines +9 to +29
Copy link
Collaborator

Choose a reason for hiding this comment

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

๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค์™€ ๋ฒจ๋ฅ˜ ์‚ฌ์šฉํ•˜์…”์„œ ํ‘ธ์…ง๋„ค์š”.
์ฒ˜์Œ์—๋Š”

struct Candidate {
    let number: Int
    var votes: Int
    var time: Int
}

struct๋กœ ์ƒํƒœ๊ด€๋ฆฌํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ ์—ญ์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์งœ์„ธ๊ตฐ์š”.

์‚ฌ์‹ค ์ €๋Š” ๋ฌธ์ œ ๋ณด์ž๋งˆ์ž stack๊ตฌ์กฐ๊ฐ€ ๋– ์˜ฌ๋ผ์„œ ๋ฐ”๋กœ stack๊ตฌ์กฐ๋ฅผ ์งœ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

Choose a reason for hiding this comment

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

์ €๋„ ๋ณด๊ณ  struct, class ์จ๋ณด๊ณ  ์‹ถ์–ด์„œ ์–ต์ง€๋กœ ์จ๋ณด๋‹ค๊ฐ€ ์ข€ ์˜ค๋ž˜๊ฑธ๋ฆฌ๊ณ  ๋ณต์žกํ•ด์ง„ ๊ฒƒ ๊ฐ™์•„์š”. ์ •๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ dictionary๊ฐ€ ๋งŽ์€๊ฑธ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋„ค์š” ใ„ทใ„ท..
dictionary ๊ตฌ์กฐ๋ฅผ key value ์“ฐ๋ฉด์„œ ์—„์ฒญ ๊น”๋”ํ•˜๊ฒŒ ์ž˜ ํ‘ธ์‹ ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค... ๋ฐฐ์›Œ๊ฐ€์š”