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

64-9kyo-hwang #221

Merged
merged 7 commits into from
Oct 3, 2024
Merged

64-9kyo-hwang #221

merged 7 commits into from
Oct 3, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณ ๋“์  Kit - ์ด๋ถ„ํƒ์ƒ‰ "์ง•๊ฒ€๋‹ค๋ฆฌ"

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

1์‹œ๊ฐ„

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

์ง์ „ PR์—์„œ ์ด์ง„ ํƒ์ƒ‰ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋Š”๋ฐ, ๊ทธ ๋•Œ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ณ ๋“์  Kit์—์„œ ์ด๋ถ„ํƒ์ƒ‰ ์นดํ…Œ๊ณ ๋ฆฌ ๋ฌธ์ œ ํ•˜๋‚˜๋ฅผ ์•ˆ ํ’€์—ˆ๋˜ ๊ธฐ์–ต์ด ๋‚˜์„œ ์ด ๋ฌธ์ œ๋กœ ์„ ์ •ํ–ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์šด ์ข‹๊ฒŒ๋„(?) ์ ‘๊ทผ ๋ฐฉ์‹์ด ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ๋ณ„๋„์˜ ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ๊ณ  ์—†์ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค :D

๋„์ฐฉ์ง€์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ตœ๋Œ€ 10์–ต์ด๋ผ ์„ ํ˜• ํƒ์ƒ‰์œผ๋กœ๋Š” ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋‹ค. ์ฆ‰ ์ด์ง„ ํƒ์ƒ‰์„ ์ ์šฉํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ง€๋‚œ๋ฒˆ๊ณผ ๋น„์Šทํ•œ ์ ‘๊ทผ๋ฒ•์„ ์ ์šฉํ•œ๋‹ค.

distance์˜ ํ•˜ํ•œ(low)์„ 1, ์ƒํ•œ์„ distance(high)๋กœ ์„ค์ •ํ•œ๋‹ค. ์ด ๋ฒ”์œ„ ์‚ฌ์ด์— ์žˆ๋Š” ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ถ”์ •์„ ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์ถ”์ • ๊ฑฐ๋ฆฌ๊ฐ’์€ "๋ชจ๋“  ๋Œ์€ ์ตœ์†Œํ•œ ์ด ๊ฑฐ๋ฆฌ๋งŒํผ ๋–จ์–ด์ ธ์žˆ๋‹ค"๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด์ž: [2, 14, 11, 21, 17], distance=25.

  • distance๊นŒ์ง€ ๋Œ ์ง€์ ์— ์ถ”๊ฐ€ํ•œ ๋’ค ์ •๋ ฌ์„ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค: [2, 11, 14, 17, 21, 25]
  • ์ถ”์ • ๊ฑฐ๋ฆฌ์˜ ํ•˜ํ•œ์€ 1, ์ƒํ•œ์€ 25๋กœ ์„ค์ •ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ํƒ์ƒ‰์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰ํ•œ๋‹ค.

  1. ์ฒซ ๋ฒˆ์งธ ์ถ”์ธก๊ฐ’์€ (1 + 25) / 2 = 13์ด๋‹ค. ์ด์ œ ์œ„์น˜ 0์—์„œ ์‹œ์ž‘ํ•ด์„œ ๊ฐ ๋Œ ๊ฐ„ ๊ฐ„๊ฒฉ์„ ํ™•์ธํ•œ๋‹ค.

์ œ๋ชฉ์„-์ž…๋ ฅํ•ด์ฃผ์„ธ์š”_

  1. ๋‘ ๋ฒˆ์งธ ์ถ”์ธก๊ฐ’์€ (1 + 12) / 2 = 6์ด๋‹ค. ์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์ƒํ•œ์ด ๊ฐ์†Œํ•œ๋‹ค.

  2. ์„ธ ๋ฒˆ์งธ ์ถ”์ธก๊ฐ’์€ (1 + 5) / 2 = 3์ด๋‹ค.

์ œ๋ชฉ์„-์ž…๋ ฅํ•ด์ฃผ์„ธ์š”_ (1)

...

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜ํ•œ์ด ์ฆ๊ฐ€ํ•  ๋•Œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๊ฐ’์„ ๊ธฐ๋กํ•œ๋‹ค.

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(int InDistance, vector<int> InRocks, int NumRemove) 
{
    InRocks.emplace_back(InDistance);
    sort(InRocks.begin(), InRocks.end());
    
    int LowerDistance = 1, UpperDistance = InDistance;
    int Answer = 0;
    
    auto IsValid = [&](int EstimateDistance)
    {
        int Count = 0, CurrentPos = 0;
        for(int RockPos : InRocks)
        {
            int Distance = RockPos - CurrentPos;
            if(Distance >= EstimateDistance)
            {
                CurrentPos = RockPos;
            }
            else
            {
                Count++;
            }
        }
        
        return Count <= NumRemove;
    };

    while(LowerDistance <= UpperDistance)
    {
        int EstimateDistance = (LowerDistance + UpperDistance) / 2;
        if(IsValid(EstimateDistance))
        {
            Answer = EstimateDistance;
            LowerDistance = EstimateDistance + 1;
        }
        else
        {
            UpperDistance = EstimateDistance - 1;
        }
    }
    
    return Answer;
}

์ฐธ๊ณ ๋กœ ์ •ํ™•ํžˆ n๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ผ n๊ฐœ ์ดํ•˜์ผ ๋•Œ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋”๋ผ๋„ ๊ดœ์ฐฎ์€ ์ด์œ ๋Š” ํƒ์ƒ‰์œผ๋กœ ๊ฐ’์„ ์ฐพ๋Š” ๊ณผ์ •์—์„œ ๊ฒฐ๊ตญ Count == n์ด ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ถ”๊ฐ€๋กœ, ์ƒํ•œ์ด ์ฆ๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ’์„ ๊ธฐ๋กํ•˜๊ธฐ ๋•Œ๋ฌธ์— Answer์—๋Š” ์ด์ „๋ณด๋‹ค ํฐ ๊ฐ’๋งŒ ๊ธฐ๋ก๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„๋กœ max๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ๋‹ค.

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

์‚ด์ง ์• ๋งคํ•œ ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ ์งˆ๋ฌธ ๊ฒŒ์‹œํŒ์„ ํ™•์ธํ•ด๋ณด๋‹ˆ ์ด๋ฏธ ์งˆ๋ฌธ ๋‚จ๊ธฐ์‹  ๋ถ„์ด ์žˆ์–ด ์ด ๊ธ€์„ ํ™•์ธํ–ˆ์—ˆ๋‹ค.

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

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

๊ทธ๋ฆผ๋ณด๋‹ˆ๊นŒ ๋„ˆ๋ฌด ์นœ์ ˆํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ์…จ๋„ค์š” ๐Ÿ‘๐Ÿ‘

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

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

์‚ฌ์‹ค ์ด์ „์— ๊ตฌ๊ฐ„ ๋‚˜๋ˆ„๊ธฐ 2์—์„œ ๊ธด๊ฐ€๋ฏผ๊ฐ€ํ•˜๊ฒŒ ๋„˜์–ด๊ฐ€์„œ... ์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ์‹œ์›ํ•˜๊ฒŒ ํ’€์–ด๋‚ด์ง„ ๋ชปํ–ˆ๋„ค์š”...ใ…œใ… 

์ด๋ฒˆ PR ์„ค๋ช…์ด ๋„ˆ๋ฌด ์ž˜ ๋˜์–ด์žˆ์–ด์„œ ๊ทธ๋•Œ ์ดํ•ด ๋ชปํ–ˆ๋˜ ๋‚ด์šฉ๋“ค๊นŒ์ง€ ํ™•์‹คํžˆ ์ดํ•ดํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค!

def solution(distance, rocks, n):

    rocks.sort()
    rocks.append(distance)

    def binary_search(start, end):
        
        if (start > end):
            return start

        mid = (start + end)//2

        remove_cnt = 0
        pos = 0

        for rock in rocks:
            if rock-pos <= mid:
                remove_cnt += 1
            else:
                pos = rock
        
        if remove_cnt <= n:
            return binary_search(mid+1, end)
        else:
            return binary_search(start, mid-1)
    
    return binary_search(0, distance)


int solution(int InDistance, vector<int> InRocks, int NumRemove)
{
InRocks.emplace_back(InDistance);
Copy link
Member

Choose a reason for hiding this comment

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

์ €๋Š” ์ด ํ•œ ์ค„ ๋•Œ๋ฌธ์— ํ•œ์ฐธ ๋” ๊ณ ์ƒํ•œ ๊ฒƒ ๊ฐ™์€๋ฐ...

๋งˆ์ง€๋ง‰ ๊ตฌ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ๋„ฃ์–ด์ค˜์•ผํ•ด์„œ InDistance๋ฅผ ๋„ฃ๋Š” ๊ฒƒ ๊ฐ™๊ธดํ•œ๋ฐ
InDistance๊ฐ€ ์—†์–ด์ง€๋Š” ๋ฐ”์œ„์˜ ์ˆ˜(Count)๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ ์ˆ˜๋„ ์žˆ์ง€ ์•Š๋‚˜์š”...?

๋ƒ…๋‹ค ๋ก ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜๊ณ  ๋๋‚œ๊ฒŒ ๋„ˆ๋ฌด ํ—ˆ๋ฌดํ•ด์š”...ใ…œใ… 

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

์—„์ฒญ๋‚˜๊ฒŒ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ์…”์„œ ์ดํ•ดํ•˜๊ณ  ๊ฐˆ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!!
์ตœ์†Œ ๊ฐ„๊ฒฉ์„ ์žก๊ณ  ๋ฌธ์ œ๋ฅผ ํ‘ผ ๋ถ€๋ถ„์ด ์ฐธ์‹ ํ•˜๋„ค์š”.
๋ถ„๋ช… ์ด์ง„ ํƒ์ƒ‰์ธ๋ฐ ์ƒ๊ฐ์„ ์กฐ๊ธˆ๋งŒ ๋ฐ”๊ฟ”์„œ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋‹ˆ.. ์ €๋Š” ์ตœ์†Œ ๊ฐ„๊ฒฉ์„ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ๋„ค์š”๐Ÿ˜ญ
์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!!

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