Skip to content

Conversation

@alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented May 22, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํผ์ฆ๊ฒŒ์ž„ ์ฑŒ๋ฆฐ์ง€
image

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

์ฃผ์˜ํ•  ๋ถ€๋ถ„!

testCase๋ฅผ ์นœ์ ˆํžˆ ์ฃผ์ง€ ์•Š์œผ๋ฉด ์ƒ๊ฐ์•ˆํ•˜๊ณ  ํ’€๋‹ค๊ฐ€ ๊ด‘ํƒˆํ• ๋ป” ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ฒ˜์Œ์— ๊ทธ๋ƒฅ ํผ์ฆ ๊ฒŒ์ž„ ๊ฒฐ๊ณผ๋งŒ ์ˆœ์„œ๋Œ€๋กœ ์ญ‰ ๋Œ๋ฆฌ๋ฉด ๋˜๊ฒ ๊ตฐ!
์ด๋ ‡๊ฒŒ ์ƒ๊ฐ์„ ํ–ˆ๋Š”๋ฐ testCase๋ฅผ ์นœ์ ˆํžˆ(์ด๊ฑฐ ์•ˆ์คฌ์œผ๋ฉด ๋ฐ”๋กœ ์ฝ”ํ…Œ ๊ด‘ํƒˆํ• ๋ป”) ์ฃผ๊ธธ๋ž˜ ๋‹คํ–‰์ด์˜€์Šต๋‹ˆ๋‹ค...

diffs times limit result
[1, 99999, 100000, 99995] [9999, 9001, 9999, 9001] 3456789012 39354

์œ„์™€ ๊ฐ™์ด ์˜ˆ์ œ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์–ด ์ด๊ฑฐ ์™„ํƒ ๋Œ๋ฆฌ๋ฉด ๋ฌด์กฐ๊ฑด ์‹œ๊ฐ„ ์ดˆ๊ณผ์ธ๊ฑธ ์•Œ๊ฒ ๋”๋ผ๊ตฌ์š”
thanks to Programmers




โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ ์ด๋ฒˆ ๋ฌธ์ œ์—์„œ ํฌ๊ฒŒ ์ƒ๊ฐํ•  ๋ถ€๋ถ„์€ ๋‘๊ฐ€์ง€์ž…๋‹ˆ๋‹ค!

  1. ํผ์ฆ ๊ฒŒ์ž„ ๊ฒฐ๊ณผ๊ฐ’ ์–ป์–ด์˜ค๊ธฐ!
  2. ์ˆ™๋ จ๋„์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ




ํผ์ฆ ๊ฒŒ์ž„์€ ์‚ฌ์‹ค ๋กœ์ง์„ ํ•œ๋ฒˆ ๋ณด๊ณ  ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ”๋Š”๋ฐ ์˜ˆ์ œ๋ฅผ ๋ณด๋‹ˆ๊นŒ ์ดํ•ด๋ฅผ ํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. ๋‚ด๊ฐ€ ํ’€ ๋ฌธ์ œ
    • total += ํ˜„์žฌ ๋ฌธ์ œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
  2. ๋‚ด๊ฐ€ ๋ชปํ’€ ๋ฌธ์ œ
    1. ๋ช‡๋ฒˆ ํ‹€๋ฆฌ๋Š”์ง€ ๊ตฌํ•˜๊ธฐ
    2. ์ด์ „ ๋ฌธ์ œ ํ‘ธ๋Š” ์‹œ๊ฐ„ + ์ง€๊ธˆ ๋ฌธ์ œ ํ‘ธ๋Š” ์‹œ๊ฐ„ ๊ณ„์‚ฐ
    3. 2๋ฒˆ์—์„œ ๊ตฌํ–ˆ๋˜ ์‹œ๊ฐ„ * ํ‹€๋ฆฐ ํšŸ์ˆ˜ + ํ˜„์žฌ ๋ฌธ์ œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
    4. total += 3๋ฒˆ ์‹œ๊ฐ„

์œผ๋กœ puzzle์ด ๋Œ์•„๊ฐ”์Šต๋‹ˆ๋‹ค!
(์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€๋ฉด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ํŽธํ•˜์‹ค๊ฑฐ์—์š”!)




๊ทธ๋Ÿฌ๊ณ  ๋‚˜๋จธ์ง€ ์ด๋ถ„ ํƒ์ƒ‰์€ ๋งค๋ฒˆ ์ง„ํ–‰ํ–ˆ๋˜ ์ด๋ถ„ ํƒ์ƒ‰์ž…๋‹ˆ๋‹ค!

๊ฒฐ๊ณผ ๊ฐ’์ด limit๋ณด๋‹ค ํฌ๋ฉด? -> start๋ฅผ ๋†’์ž„
๊ฒฐ๊ณผ ๊ฐ’์ด limit๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด? -> end๋ฅผ ๋‚ฎ์ถค
(ํ•˜์ง€๋งŒ ๊ฒฐ๊ณผ ๊ฐ’ ์ €์žฅ์„ ์œ„ํ•ด answer์— ์ €์žฅํ•ด๋‘  -> ์ดํ›„ ๋ถ€๋ถ„์ด limit๋ณด๋‹ค ํฌ๋ฉด ์ด answer์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด)




์ •๋‹ต ์ฝ”๋“œ

import Foundation

func puzzle(diffs: [Int], times: [Int], level: Int) -> Int{
    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
}


func solution(_ diffs:[Int], _ times:[Int], _ limit:Int64) -> Int {
    var start = 1
    var end = diffs.max() ?? 100000
    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
}




๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ๋Š” ๋จผ๊ฐ€ ์–ธ์–ด์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋งŽ์ง€ ์•Š์œผ๋ฉด ์ฐธ ํž˜๋“  ๊ฒƒ ๊ฐ™์•„์š”
์˜ค๋ž˜๋งŒ์— max๋ฅผ ์ป๋‹ค๊ณ  ๋ฐ”๋กœ max(diffs)๋ฅผ ์“ฐ๊ณ  ์™œ ์•ˆ๋˜์ง€ ํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค ใ…Ž..

Copy link
Member

@giljihun giljihun left a comment

Choose a reason for hiding this comment

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

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ฝ”๋ฉ˜ํŠธ์—์„œ ๋งŽ์€ ์–˜๊ธฐ๋ฅผ ํ•œ๊ฑฐ๊ฐ™๋„ค์š” !!

์ œ๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ "๋ ˆ๋ฒจ์€ ๋ฌด์กฐ๊ฑด diff์˜ ๋ฒ”์œ„ ์•ˆ์— ์žˆ์Œ" ์„ ์ƒ๊ฐํ•ด๋‚ด๊ณ ,
์ด๋ถ„ํƒ์ƒ‰์„ ๋“ค์–ด๊ฐ€๋Š”๊ฑฐ์˜€๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“œ๋„ค์š”!

/*
 ํ˜„์žฌ ํผ์ฆ ๋‚œ์ด๋„ - diff
 ํ˜„์žฌ ํผ์ฆ ์†Œ์š”์‹œ๊ฐ„ - time_cur
 ์ด์ „ ํผ์ฆ ์†Œ์š”์‹œ๊ฐ„ - time_prev
 ์ˆ™๋ จ๋„ - level
 
 - diff <= level : ํผ์ฆ ํ‹€๋ฆฌ์ง€ ์•Š๊ณ , time_curr๋งŒํผ ์‹œ๊ฐ„ ์‚ฌ์šฉ
 - diff > level : (diff - level)๋ฒˆ ํผ์ฆ์„ ํ‹€๋ฆผ,
    ์ด ๊ฒฝ์šฐ, ((diff - level) * time_curr)๋งŒํผ ์‹œ๊ฐ„ ์‚ฌ์šฉํ•˜๊ณ 
    time_prev๋งŒํผ์˜ ์‹œ๊ฐ„์„ ์‚ฌ์šฉํ•ด์„œ ์ด์ „ ํผ์ฆ์„ ๋‹ค์‹œ ํ’€๊ณ  ์™€์•ผํ•จ. (์ด ๊ฒฝ์šฐ ๋ฌด์กฐ๊ฑด ํ‹€๋ฆฌ์ง€ ์•Š์Œ.)
 - ์œ„ ๊ณผ์ • ์ดํ›„, ๋ฌด์กฐ๊ฑด time_cur๋งŒํผ ์‹œ๊ฐ„์„ ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ•จ.
 
 ==> ((time_cur + time_prev) * (diff - level)) + time_cur
 
 ์ „์ฒด ์ œํ•œ ์‹œ๊ฐ„ limit.
 ์–ด๋–ค level์ด ํผ์ฆ์„ ๋ชจ๋‘ ํ•ด๊ฒฐํ•˜๋Š” ์ตœ์†Œ ๋ ˆ๋ฒจ์ผ๊นŒ?
 
 1. ๋ ˆ๋ฒจ์€ ๋ฌด์กฐ๊ฑด diff์˜ ๋ฒ”์œ„ ์•ˆ์— ์žˆ์Œ. (์™œ๋ƒํ•˜๋ฉด max๋‚œ์ด๋„๋ณด๋‹ค ํฌ๋ฉด ๋‹ค ok์ž„.)
 2. ํ•ด๋‹น ๋ฒ”์œ„ ์•ˆ์—์„œ์˜ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์•„์•ผํ•จ.
 */


import Foundation

func solution(_ diffs:[Int], _ times:[Int], _ limit:Int64) -> Int {
    
    // ==> ((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
    }
    
    var left = 1
    
    // ์ด ๊ฒฝ์šฐ ์˜ต์…”๋„ ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ญ์ง€?
    // var right = diffs.max()!
    // ์ผ๋‹จ ๊ฐ€๋“œ๋ฌธ ์ฒ˜๋ฆฌ
    guard let maxDiff = diffs.max() else { exit(0) }
    
    var right = maxDiff
    var answer = right

    while left <= right {
        let mid = (left + right) / 2
        if isPossible(level: mid) {
            answer = mid
            right = mid - 1
        } else {
            left = mid + 1
        }
    }
    return answer
}

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

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
        }
    }

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์“ฐ๋ฉด ๋ช…์‹œ์ ์œผ๋กœ ์˜ˆ์™ธ๊ฐ€ ๋А๊ปด์ ธ์„œ ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š” !

@alstjr7437
Copy link
Member Author

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ฝ”๋ฉ˜ํŠธ์—์„œ ๋งŽ์€ ์–˜๊ธฐ๋ฅผ ํ•œ๊ฑฐ๊ฐ™๋„ค์š” !!

์ œ๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ "๋ ˆ๋ฒจ์€ ๋ฌด์กฐ๊ฑด diff์˜ ๋ฒ”์œ„ ์•ˆ์— ์žˆ์Œ" ์„ ์ƒ๊ฐํ•ด๋‚ด๊ณ , ์ด๋ถ„ํƒ์ƒ‰์„ ๋“ค์–ด๊ฐ€๋Š”๊ฑฐ์˜€๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“œ๋„ค์š”!

@giljihun
Comment ๋Œ“๊ธ€๋“ค ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค!!
ํ‘ผ ์ฝ”๋“œ ์ค‘ ๊ถ๊ธˆํ•œ๊ฒŒ Int64๋ฅผ ๋”ฐ๋กœ ์ค€ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?

+if๋ฌธ ๊ถ๊ธˆ์ฆ

            let timePrev: Int
            // ์ฒซ ํผ์ฆ์€ ์ด์ „ ํผ์ฆ์ด ์—†์ž–์—ฌ -> 0
            if i == 0 {
                timePrev = 0
            } else {
                timePrev = times[i - 1]
            }
            let timePrev = (i == 0) ? 0 : times[i - 1]

์ด๋ ‡๊ฒŒ if i == 0 ๊ณผ ๊ฐ™์€ ๋ถ€๋ถ„์€ ์œ„์™€ ๊ฐ™์ด ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!

@giljihun
Copy link
Member

@alstjr7437
func solution(~ _ limt:Int64)์—์„œ ๋ฆฌ๋ฐ‹์ด int64๊ณ 
์ฒ˜์Œ์— Int64์•ˆํ•ด์ค€ ๋ถ€๋ถ„์—์„œ ๊ณ„์† ์˜ค๋ฅ˜๊ฐ€ ๋– ์„œ, ๋ฐ‘์— ๊ฐ’๋“ค๋„ ๊ทธ๋ ‡๊ฒŒ ์„ ์–ธํ•ด์„œ ์คฌ์Šต๋‹ˆ๋‹ค..!
๋ถ€๋ถ„์ ์œผ๋กœ ๋ถˆํ•„์š”ํ•œ ๋ถ€๋ถ„์ด ์žˆ๋„ค์š”~ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.!

Copy link
Collaborator

@bishoe01 bishoe01 left a comment

Choose a reason for hiding this comment

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

๋ฌธ์ œ๊ฐ€ ๋ˆˆ์— ์ž˜ ์•ˆ๋“ค์–ด์™”์–ด์š”! ์„ค๋ช…์ด ์žฅํ™ฉํ•˜๋‹ˆ๊นŒ ํ—ท๊ฐˆ๋ฆฌ๋„ค์š”.

Level์ด ์ˆ™๋ จ๋„๋ผ๊ณ  ํ‘œํ˜„๋˜์–ด์žˆ์–ด์„œ ํ˜ผ๋ž€..

๊ฒฐ๊ตญ์—” ๊ทธ๋ƒฅ ์ ์ ˆํ•œ Level๊ฐ’ ์ฐพ๋Š”๊ฑฐ์˜€๋„ค์š”!

๊ทธ๋Ÿผ์ด์ œ ๊ทธ๋Ÿฌ๋ฉด ์‹œ์ž‘์ ์„ ์–ด๋–ป๊ฒŒ ์žก์„ ๊ฒƒ์ด๋ƒ...! ์˜€๋Š”๋ฐ,
left๋ฅผ ์ตœ์†Œ๋ ˆ๋ฒจ๋กœ , right๋ฅผ ์ตœ๋Œ€๋ ˆ๋ฒจ๋กœ ์žก๊ณ  ์ด๋ถ„ํƒ์ƒ‰์„ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค !

  1. caculateTime ํ•จ์ˆ˜๋กœ ํ•˜๋‚˜์˜ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ๊ณ„์‚ฐํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋นผ์คฌ์Šต๋‹ˆ๋‹ค.
  2. ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ limit ๋„˜๋ƒ ์•ˆ๋„˜๋ƒ๋กœ ๋ฒ”์œ„ ์กฐ์ •ํ•ด์ฃผ๋ฉด์„œ ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค.
import Foundation

func solution(_ diffs: [Int], _ times: [Int], _ limit: Int64) -> Int {
    var level = -1
    var left = diffs.min()!
    var right = diffs.max()!

    var totalSpendTime = 0

    func caculateTime(_ level: Int, _ diff: Int, _ time: Int, _ prevTime: Int) -> Int {
        let gap = diff - level
        // level์ด ๋” ๋†’์„ ๋•Œ - ๋ฐ”๋กœํ’€๋ฆด๋•Œ
        if gap <= 0 {
            return time
        }
        else {
//            print(gap)
//            print(time)
//            print(prevTime)
//            print((gap * (time + prevTime)) + time)
            return (gap * (time + prevTime)) + time
        }
    }

    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
//            print(totalSpendTime)
        }
        if totalSpendTime > limit {
//            print("TOTLAL", totalSpendTime)
            left = mid + 1
        }
        else {
            level = mid
            right = mid - 1
        }
//        print(totalSpendTime)
    }
    return level
}

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.

4 participants