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
1 change: 1 addition & 0 deletions alstjr7437/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
| 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|
| 4์ฐจ์‹œ | 2025.05.13 | ๊ตฌํ˜„ | [ํ›„๋ณด ์ถ”์ฒœํ•˜๊ธฐ](https://www.acmicpc.net/problem/1713)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/24|
| 5์ฐจ์‹œ | 2025.05.22 | ์ด๋ถ„ํƒ์ƒ‰ | [ํผ์ฆ๊ฒŒ์ž„ ์ฑŒ๋ฆฐ์ง€](https://school.programmers.co.kr/learn/courses/30/lessons/340212)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/27|
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation

func puzzle(diffs: [Int], times: [Int], level: Int) -> Int{
Copy link
Member

Choose a reason for hiding this comment

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

์†”๋ฃจ์…˜๋ถ€ํ„ฐ ์ฝ์–ด์„œ ์ดํ•ด๊ฐ€ ๋˜๊ธดํ•ฉ๋‹ˆ๋‹ค!
๊ทธ๋ž˜๋„ puzzle์ด๋ผ๋Š” ํ•จ์ˆ˜๋ช…์€ ์กฐ๊ธˆ ๊ฐ€๋…์„ฑ์„ ๋‚ฎ์ถ˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

์Œ.. calTotalTime() ์ด๋Ÿฐ๊ฑด ์–ด๋–จ๊นŒ์š”~?

Copy link
Member Author

Choose a reason for hiding this comment

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

์‚ฌ์‹ค ํ•จ์ˆ˜๊ฐ€ ๋‹จ์ผ ์ฑ…์ž„ ์›์น™์„ ๋งž์ถ”๊ธฐ ์œ„ํ•ด์„œ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ
์ƒ๊ฐํ•ด๋ณด๋‹ˆ puzzle๊ฒŒ์ž„์„ ํ•˜๋Š” ์ €๋Š” ์ดํ•ด๋ฅผ ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ์ดํ•ด๋ฅผ ๋ชปํ•˜๊ฒ ๋„ค์š”!

๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ์ฃผ์„์„ ์ถ”๊ฐ€ํ•˜๋Š”๊ฒŒ ๋ญ”๊ฐ€ ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!

calTotalTime์„ ํ•ด๋„ ๋ญ”๊ฐ€ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ์ด ๋ณด๋ฉด ๋ฌด์Šจ ์ด ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š”๊ฑฐ์ง€? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด์„œ

puzzle๊ฒŒ์ž„์ธ๋ฐ ์„ค๋ช…์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹จ์ผ ์ฑ…์ž„ ์›์น™ + ๊ฐ€๋…์„ฑ์„ ์ง€ํ‚ค๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”!

var result = 0
for i in 0..<diffs.count {
if diffs[i] <= level {
result += times[i]
} else {
let discorrectCount = diffs[i] - level
let discorrectUseTime = times[i - 1] + times[i]
let totalUseTime = discorrectUseTime * discorrectCount + times[i]
result += totalUseTime
}
}

return result
}
Comment on lines +3 to +17
Copy link
Member

Choose a reason for hiding this comment

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

๊ฐ’์„ ๋„˜๊ธฐ๊ณ  solution()์—์„œ result <= limit ๊ฒ€์‚ฌ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์…จ๊ตฐ์š”!
์ €๋Š” ๊ฐ€๋Šฅ์„ฑ ์—ฌ๋ถ€๋ฅผ ํ•จ์ˆ˜์—์„œ ํŒ๋‹จํ•˜๊ณ  ๋„˜๊ธฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์–ด์š”!

// ==> ((time_cur + time_prev) * (diff - level)) + time_cur
    func isPossible(level: Int) -> Bool {
        
        var totalTime: Int64 = 0

        for i in 0..<diffs.count {
            let diff = diffs[i]
            let timeCur = times[i]
            let timePrev: Int
            
            // ์ฒซ ํผ์ฆ์€ ์ด์ „ ํผ์ฆ์ด ์—†์ž–์—ฌ -> 0
            if i == 0 {
                timePrev = 0
            } else {
                timePrev = times[i - 1]
            }

            if diff <= level {
                totalTime += Int64(timeCur)
            } else {
                let failCount = diff - level
                totalTime += Int64((timeCur + timePrev) * failCount + timeCur)
            }

            if totalTime > limit {
                return false
            }
        }
        return true
    }

Copy link
Member Author

Choose a reason for hiding this comment

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

์‚ฌ์‹ค ์ฝ”ํ…Œ ๋ฌธ์ œ ํ’€๋•Œ๋ผ ์ƒ๊ด€ ์—†๊ธดํ•œ๋ฐ
๊ฒŒ์ž„ ํ•œํŒ ๊ฒฐ๊ณผ + ํ†ต๊ณผ ๊ฐ€๋Šฅํ•˜๋ƒ vs ๊ฒŒ์ž„ ํ•œํŒ ๊ฒฐ๊ณผ
์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์ž๋ฅด๋Š”์ง€์— ๋”ฐ๋ผ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ์„ฑ์ด ๋‹ฌ๋ผ์ ธ์„œ ๊ทธ๋ ‡๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™๋„ค์š”!!

Copy link
Collaborator

@bishoe01 bishoe01 May 31, 2025

Choose a reason for hiding this comment

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

์ €๋„ limit๋ฅผ ๋„˜์—ˆ์„๋•Œ ํŒ๋‹จํ•˜๋Š”๊ฑฐ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์คฌ๋Š”๋ฐ, ๋งฅ๋ฝ์€ ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™์•„์š”.
ํ•จ์ˆ˜๋ฅผ ์–ด๋””๊นŒ์ง€ ์ž˜๋ผ์•ผํ• ์ง€ ๋ช…ํ™•ํ•œ ๊ธฐ์ค€์ด ์—†์ด ํ•ญ์ƒ ๋‹ค๋ฅธ๊ฒƒ๊ฐ™์€๋ฐ, ์ด๊ฑฐ๋Š” ์ทจํ–ฅ์ฐจ์ด์ผ๊นŒ์š”? ์ €๋Š” ๋„ˆ๋ฌด ๋‹ด์•„์ฃผ๋ฉด ์˜คํžˆ๋ ค ๋ณธ์ฝ”๋“œ๋ฅผ ๋‹ค ์˜ฎ๊ธด๋А๋‚Œ์ด๋ผ ์ตœ๋Œ€๊ธธ์ด๋ฅผ ์ค„์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ถ„ํ• ํ•œ๋‹ค๋Š” ๋งˆ์ธ๋“œ๋กœ ์ ‘๊ทผํ•˜๋Š”๋ฐ, ๋‹ค๋ฅธ๋ถ„๋“ค์€ ์–ด๋–ค ์ƒ๊ฐ์œผ๋กœ ๋ถ„๋ฆฌํ•˜์‹œ๋Š” ์ง€ ๊ถ๊ธˆํ•˜๋„ค์š”

while left <= right {
        let mid = (left + right) / 2
        totalSpendTime = 0
        for index in 0 ..< diffs.count {
            var prevTime = 0
            if index > 0 {
                prevTime = times[index - 1]
            }
            let tmp = caculateTime(mid, diffs[index], times[index], prevTime)
            totalSpendTime += tmp
        }
        if totalSpendTime > limit {
            left = mid + 1
        }
        else {
            level = mid
            right = mid - 1
        }
    }



func solution(_ diffs:[Int], _ times:[Int], _ limit:Int64) -> Int {
var start = 1
var end = diffs.max() ?? 100000
Comment on lines +21 to +22
Copy link
Member

Choose a reason for hiding this comment

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

์˜น ์ด๋ ‡๊ฒŒ ๋‹ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž๋กœ max()๋ฅผ ์ฒ˜๋ฆฌํ•˜์…จ๊ตฐ์š”.

์ €๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์งฐ๋Š”๋ฐ์š”

    // ์ด ๊ฒฝ์šฐ ์˜ต์…”๋„ ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ญ์ง€?
    // var right = diffs.max()!
    // ์ผ๋‹จ ๊ฐ€๋“œ๋ฌธ ์ฒ˜๋ฆฌ
    guard let maxDiff = diffs.max() else { exit(0) }

์ „ right๋กœ ํ–ˆ๋Š”๋ฐ(ํ‚จ๋” ์ฝ”๋“œ์—์„  end๋„ค์šฉ)
์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ญ๊ฐ€ ์žˆ๋‚˜~ ์ƒ๊ฐ์„ ํ•˜๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์ผ๋‹จ ๊ฐ€๋“œ๋ฌธ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‚˜์ค‘์—๋ณด๋‹ˆ ๋นˆ ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ๋งŒ ์•ˆ๋˜๋Š”๊ฑฐ๊ฐ™๋”๋ผ๊ตฌ์š”.

์‚ฌ์‹ค ๋ฌธ์ œ์—์„œ ๋นˆ ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง€์ง„ ์•Š๋Š”๊ฑฐ๊ฐ™์•„์„œ ์ค‘์š”ํ•˜์ง„ ์•Š์€๊ฑฐ๊ฐ™์Šต๋‹ˆ๋‹ค.ใ…‹ใ…‹ ใ…Žใ…Ž

Copy link
Member

Choose a reason for hiding this comment

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

์š”์ƒˆ ๋А๋ผ๋Š”๋ฐ ์ œ๊ฐ€ ๋ฐฉ์–ด์ ์œผ๋กœ ์ฝ”๋“œ์งœ๋Š” ์Šต๊ด€์ด ์กฐ๊ธˆ ์ƒ๊ธฐ์ง€ ์•Š์•˜๋‚˜ ์‹ถ์–ด์š”!
๊ทผ๋ฐ ๋น„ํšจ์œจ์„ ๊ฐ€์ ธ์˜ค๊ธฐ๋„ ํ•˜๋Š”๊ฑฐ๊ฐ™์•„์„œ ๋” ์กฐ์‹ฌํžˆ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Member Author

Choose a reason for hiding this comment

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

์˜น ์ด๋ ‡๊ฒŒ guard๋ฌธ์„ ํ•˜๋‹ˆ ์˜ˆ์™ธ๋ฅผ ๋” ํ™•์‹คํžˆ ์žก๋Š” ๋А๋‚Œ์ด๋ผ ์ข‹๋„ค์š”!!

์ด ๋ฌธ์ œ์—์„œ ๊ทธ๋Ÿฐ ์˜ˆ์ œ๋Š” ์—†์ง€๋งŒ
๊ฐ‘์ž๊ธฐ ์ดˆ๊ณผ๋˜๋Š” ๊ฒฝ์šฐ 0์„ ์ถœ๋ ฅํ•ด๋ผ

์ด๋ ‡๊ฒŒ ๋˜๋ฉด Gil ์ฒ˜๋Ÿผ guard๋ฌธ์œผ๋กœ ํƒˆ์ถœํ•ด์„œ print(0)์„ ์ฐ์œผ๋ฉด ๋์ด๋ผ ๋”์šฑ ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”!

์ „ ์‚ฌ์‹ค max()๊ฐ€ ์˜ต์…”๋„๋กœ ์˜ค๋Š”์ง€ ๋ชฐ๋ผ์„œ ๊ธ‰ํ•˜๊ฒŒ 10000์„ ๋„ฃ์—ˆ์–ด์š”..

Copy link
Collaborator

@bishoe01 bishoe01 May 31, 2025

Choose a reason for hiding this comment

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

๊ธฐ๊ฐ€๋ง‰ํžˆ๋„ค์š”.. ์ €๋Š” ์–ธ์ œ์ฏค guard๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋„ฃ์„ ์ˆ˜ ์žˆ์„๊นŒ์š”..
guard์“ฐ๋ฉด ๋ช…์‹œ์ ์œผ๋กœ ์˜ˆ์™ธ๊ฐ€ ๋А๊ปด์ ธ์„œ ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š” !

var mid = 0
var answer = 0

while start <= end {
mid = (start + end) / 2
let result = puzzle(diffs: diffs, times: times, level: mid)

if result <= limit {
answer = mid
end = mid - 1
} else {
start = mid + 1
}
}

return answer
}