Skip to content
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

15-janghw0126 #176

Merged
merged 9 commits into from
Nov 21, 2024
Merged

15-janghw0126 #176

merged 9 commits into from
Nov 21, 2024

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Oct 12, 2024

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค | ์ง•๊ฒ€๋‹ค๋ฆฌ

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

1์‹œ๊ฐ„ ๋ฐ˜

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

๐Ÿ‘‰ ๋ฌธ์ œ ์ฝ”๋“œ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์ด ๋ฌธ์ œ๋Š” ๋ฐ”์œ„๋ฅผ n๊ฐœ ์ œ๊ฑฐํ•˜๊ณ  ๋‚œ ํ›„์— ๊ฐ ์ง€์  ์‚ฌ์ด์˜ ์ตœ์†Ÿ๊ฐ’๋“ค ์ค‘์— ์ตœ๋Œ“๊ฐ’์„ ์ฐพ์•„๋‚˜๊ฐ€๋Š” ๊ณผ์ •์ด๋ฏ€๋กœ ํƒ์ƒ‰์„ ์ด์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด, ์ˆœ์ฐจํƒ์ƒ‰์œผ๋กœ๋Š” ํšจ์œจ์„ฑ์„ ๋งŒ์กฑํ•˜๊ธฐ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ์ด์ง„ํƒ์ƒ‰์œผ๋กœ ๋‹ค๋ฆฌ๋ฅผ ๊ฑด๋„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ธ์›์„ ์ฐพ๋Š” ๊ฒƒ์ด ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๊ณ , ๊ทธ๋ž˜์„œ ์ด์ง„ํƒ์ƒ‰์„ ์ด์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค!

๐Ÿ‘€๋ฌธ์ œ ํ•ด๊ฒฐ ๋กœ์ง

  1. ์ฃผ์–ด์ง„ ๋ฐ”์œ„๋“ค์˜ ์œ„์น˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„์—, ๋„์ฐฉ ์ง€์ ์ธ distance๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
rocks.sort()
rocks.append(distance)

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, ๋งˆ์ง€๋ง‰ ๋ฐ”์œ„์—์„œ ๋„์ฐฉ์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋„ ๊ณ„์‚ฐ์— ํฌํ•จ๋ฉ๋‹ˆ๋‹ค!

  1. ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•œ left์™€ right๋ฅผ ์„ค์ •ํ•˜๊ณ , ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ ๊ฑฐ๋ฆฌ mid๋ฅผ ์„ค์ •ํ•˜์—ฌ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
while left <= right:
    mid = (left+right) // 2

์—ฌ๊ธฐ์„œ mid๋Š” ํ˜„์žฌ ๊ฒ€์‚ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์ง•๊ฒ€๋‹ค๋ฆฌ ์‚ฌ์ด์˜ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

  1. ๋ฐ”์œ„๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ํ™•์ธํ•˜๋ฉด์„œ ํ˜„์žฌ ์œ„์ฐจ์™€ ๋ฐ”์œ„ ์œ„์น˜ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๊ฐ€ mid๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ฐ”์œ„๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด current๋ฅผ ํ•ด๋‹น ๋ฐ”์œ„๋กœ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
current = 0
removed_rocks = 0
for rock in rocks:
    if rock-current < mid:
        removed_rocks += 1
    else:
        current = rock

mid๊ฐ€ ๊ธฐ์ค€์ด ๋˜๋Š” ์ด์œ ๋Š” ๋ฌธ์ œ์—์„œ ๊ฐ ์ง€์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’ ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•˜๋ผ๊ณ  ํ•˜์˜€์œผ๋ฏ€๋กœ, mid๋ณด๋‹ค ์ž‘์€ ๊ฑฐ๋ฆฌ๊ฐ€ ์ƒ๊ธฐ๋ฉด ๋ฐ”์œ„๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํ•ด๋‹น ๋ฐ”์œ„๋ฅผ ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰ , ๋ฐ”์œ„๋ฅผ n๊ฐœ๋ณด๋‹ค ๋งŽ์ด ์ œ๊ฑฐํ•œ ์ƒํ™ฉ์€ ํ˜„์žฌ ์„ค์ •ํ•œ mid๊ฐ€ ๋„ˆ๋ฌด ํฐ ๊ฐ’์ด๋ผ, ๊ฑฐ๋ฆฌ๋ฅผ ์ค„์—ฌ์„œ ๋ฐ”์œ„๋ฅผ ๋œ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

  1. ๋งŒ์•ฝ์— ์ œ๊ฑฐํ•œ ๋ฐ”์œ„์˜ ์ˆ˜๊ฐ€ ์ œ๊ฑฐํ•  ๋ฐ”์œ„์˜ ์ˆ˜์ธ n๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ๋„ˆ๋ฌด ๋งŽ์€ ๋ฐ”์œ„๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ์ค„์ด๊ณ  ๋„ˆ๋ฌด ๋งŽ์€ ๋ฐ”์œ„๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ๋Š˜๋ฆฝ๋‹ˆ๋‹ค.
else:
    answer = mid
    left = mid+1

์ด๋Ÿฌํ•œ ๋กœ์ง์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค!

๐Ÿ‘ฉโ€๐Ÿ’ป์ตœ์ข… ์ฝ”๋“œ

def solution(distance, rocks, n):
    rocks.sort()
    rocks.append(distance)
    left, right = 1, distance
    
    while left <= right:
        mid = (left+right) // 2
        current = 0
        removed_rocks = 0
        
        for rock in rocks:
            if rock-current < mid:
                removed_rocks += 1
            else:
                current = rock
                
        if removed_rocks > n:
            right = mid-1
        else:
            answer = mid
            left = mid+1
        
    return answer

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

Copy link
Member

@jung0115 jung0115 left a comment

Choose a reason for hiding this comment

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

๊ฐ ์ง€์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’ ์ค‘์— ๊ฐ€์žฅ ํฐ ๊ฐ’์ด๋ผ๋Š” ๋ง์„ ๋ฐ”๋กœ ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์„œ ์‚ด์ง ํ˜ผ๋ž€์Šค๋Ÿฌ์› ๋Š”๋ฐ ํ˜œ์› ๋‹˜์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  ํ™•์‹คํžˆ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค ๐Ÿฅฒ

์ด๋ฒˆ ์ฐจ์‹œ๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!

class Solution {
    fun solution(distance: Int, rocks: IntArray, n: Int): Int {
        var answer = 0
        var rockClone = rocks.sortedArray() + distance
        
        var left = 1
        var right = distance
        
        while(left <= right) {
            val mid = (left + right) / 2
            
            var deleteRockCount = 0 // ์ œ๊ฑฐํ•œ ๋ฐ”์œ„์˜ ์ˆ˜
            var position = 0
            
            rockClone.forEach { rock -> 
                if(rock - position < mid)
                    deleteRockCount++
                else
                    position = rock
            }
            
            if(deleteRockCount > n)
                right = mid - 1
            else {
                answer = mid
                left = mid + 1
            }
        }
        
        return answer
    }
}

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.

2 participants