Programmers, ์ง•๊ฒ€๋‹ค๋ฆฌ

2021. 12. 10. 22:05ใ†ETC/Algorithm

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ ์ด๋ถ„ํƒ์ƒ‰ ๋ฌธ์ œ! 

 

๋ฌธ์ œ ์„ค๋ช…

์ถœ๋ฐœ์ง€์ ๋ถ€ํ„ฐ distance๋งŒํผ ๋–จ์–ด์ง„ ๊ณณ์— ๋„์ฐฉ์ง€์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ์‚ฌ์ด์—๋Š” ๋ฐ”์œ„๋“ค์ด ๋†“์—ฌ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”์œ„ ์ค‘ ๋ช‡ ๊ฐœ๋ฅผ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ๋„์ฐฉ์ง€์ ์ด 25๋งŒํผ ๋–จ์–ด์ ธ ์žˆ๊ณ , ๋ฐ”์œ„๊ฐ€ [2, 14, 11, 21, 17] ์ง€์ ์— ๋†“์—ฌ์žˆ์„ ๋•Œ ๋ฐ”์œ„ 2๊ฐœ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ์ถœ๋ฐœ์ง€์ , ๋„์ฐฉ์ง€์ , ๋ฐ”์œ„ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์ œ๊ฑฐํ•œ ๋ฐ”์œ„์˜ ์œ„์น˜ ๊ฐ ๋ฐ”์œ„ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’
[21, 17] [2, 9, 3, 11] 2
[2, 21] [11, 3, 3, 8] 3
[2, 11] [14, 3, 4, 4] 3
[11, 21] [2, 12, 3, 8] 2
[2, 14] [11, 6, 4, 4] 4

 

์œ„์—์„œ ๊ตฌํ•œ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’ ์ค‘์— ๊ฐ€์žฅ ํฐ ๊ฐ’์€ 4์ž…๋‹ˆ๋‹ค.

์ถœ๋ฐœ์ง€์ ๋ถ€ํ„ฐ ๋„์ฐฉ์ง€์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ distance, ๋ฐ”์œ„๋“ค์ด ์žˆ๋Š” ์œ„์น˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด rocks, ์ œ๊ฑฐํ•  ๋ฐ”์œ„์˜ ์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋ฐ”์œ„๋ฅผ n๊ฐœ ์ œ๊ฑฐํ•œ ๋’ค ๊ฐ ์ง€์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’ ์ค‘์— ๊ฐ€์žฅ ํฐ ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

์ œํ•œ์‚ฌํ•ญ

  • ๋„์ฐฉ์ง€์ ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ distance๋Š” 1 ์ด์ƒ 1,000,000,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๋ฐ”์œ„๋Š” 1๊ฐœ ์ด์ƒ 50,000๊ฐœ ์ดํ•˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • n ์€ 1 ์ด์ƒ ๋ฐ”์œ„์˜ ๊ฐœ์ˆ˜ ์ดํ•˜์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

distance rocks n return
25 [2, 14, 11, 21, 17] 2 4

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ

 

C++

int solution(int distance, vector<int> rocks, int n) {
    int answer = 0;
    int low=0, high=distance;
    vector<int> dist;
    
    sort(rocks.begin(), rocks.end());
    rocks.push_back(distance);
    
    while (low < high) {
        int mid = (low+high) / 2;
        int rm = 0, start = 0;
        
        for (int roc: rocks) {
            if (roc - start < mid) rm++;
            else start = roc;
        }
        
        if (rm <= n) {
            low = mid + 1;
            if (mid > answer) answer = mid;
        } else high = mid;
    }
    
    return answer;
}

 

๋ฌธ์ œ ์ ‘๊ทผ๋ฒ•

๊ฐ€์žฅ ๋จผ์ € ์ด๋ถ„ํƒ์ƒ‰ ์กฐ๊ฑด์œผ๋กœ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ ํ›„, [0 ~ ๋งˆ์ง€๋ง‰ ๋Œ ~ distance] ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊นŒ์ง€ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด distance๋ฅผ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋ถ„ ํƒ์ƒ‰์—์„œ ๋จผ์ € ์งš๊ณ  ๊ฐ€์•ผํ•  ๊ฒƒ์€ ์–ด๋–ค ๊ฒƒ์„ ๊ฒ€์ƒ‰ํ•  ๊ฒƒ์ธ์ง€์™€ ์–ด๋–ค ์กฐ๊ฑด์œผ๋กœ ํ•„ํ„ฐ๋งํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์ •ํ™•ํžˆ ํ•ด์•ผํ•˜๋Š” ๊ฒŒ ์ค‘์š”ํ•œ ๊ฒƒ ๊ฐ™์•„์š”. 

 

โœ”๏ธ ์–ด๋–ค ๊ฒƒ์„ ๊ฒ€์ƒ‰ํ•  ๊ฒƒ์ธ์ง€ -> ๋Œ ์‚ฌ์ด์˜ ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ ๊ฑฐ๋ฆฌ ์ค‘ ์ตœ๋Œ“๊ฐ’

โœ”๏ธ ์–ด๋–ค ์กฐ๊ฑด์œผ๋กœ ํ•„ํ„ฐ๋งํ•  ๊ฒƒ์ธ์ง€ -> ์ตœ์†Œํ•œ์˜ ๊ฐ€๋Šฅํ•œ ๊ฑฐ๋ฆฌ๋ฅผ ํƒ์ƒ‰ ํ›„ ๊ฐ€๋Šฅํ•œ ์กฐ๊ฑด์ธ์ง€ ํŒ๋‹จ ํ›„ ์ตœ๋Œ“๊ฐ’ ๋ฐ˜์˜

 

 

์˜ˆ์ œ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์„ค๋ช…์„ ์ด์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.

 

// low = 0, high = 25, mid = 13
[0]  2  11  14  17  21  [25]

 

๋จผ์ € mid ๊ฐ’์€ '์ตœ์†Œ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๋Œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ'์ž„์„ ํ™•์ธํ•˜๊ณ !

์ด๋ถ„ํƒ์ƒ‰์˜ ๊ณผ์ •์„ ๋‘ ๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

 

1. mid ๊ฐ’์„ ์ ์šฉํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ๊ฐ’ ๋„์ถœ  ๐Ÿ‘‰๐Ÿป mid ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ์†Œ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ, ๋ฐ”์œ„๊ฐ€ ๋ช‡ ๊ฐœ ์ œ๊ฑฐ๋˜๋Š”๊ฐ€

 

2. ๊ฒฐ๊ณผ๊ฐ’์˜ ์œ ํšจ์„ฑ ํŒ๋‹จ ํ›„ ๋ฒ”์œ„ ๊ฐฑ์‹   ๐Ÿ‘‰๐Ÿป  mid ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ์†Œ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ, ์ œ๊ฑฐ๋œ ๋ฐ”์œ„๊ฐ€ n์˜ ๋ฒ”์œ„ ๋‚ด์— ๋“ค์–ด์˜ค๋Š” ์œ ํšจ๊ฐ’์ธ๊ฐ€

 

์œ„์™€ ๊ฐ™์ด ๋‘ ๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ๋ฅผ ํ•จ๊ป˜ ์‚ดํŽด๋ณด๋„๋ก ํ• ๊ฒŒ์š”.

 

 

1. mid ๊ฐ’์„ ์ ์šฉํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ๊ฐ’ ๋„์ถœ

mid ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ์†Œ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ, ๋ฐ”์œ„๊ฐ€ ๋ช‡ ๊ฐœ ์ œ๊ฑฐ๋˜๋Š”๊ฐ€

 

์ด ๊ฐ’์ด ์ตœ์†Œ๊ฐ’์ธ์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์œผ๋กœ ๋ช‡ ๊ฐœ์˜ ๋Œ์ด ์‚ญ์ œ๋˜๋Š” ์ง€๋ฅผ ์ฒดํฌํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์•„์š”.

 

๊ฐฑ์‹ ๋˜๋Š” start ๋Š”  ↓ ๋กœ, ์‚ญ์ œ๋˜๋Š” ๋Œ์€ R๋กœ ํ‘œ์‹œํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 ↓           ↓
[0]  2  11  14  17  21  [25]
     R   R       R   R    R

 

์ฐจ๊ทผ์ฐจ๊ทผ ๋ณด์ž๋ฉด, 0์—์„œ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๊ฐ๊ฐ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ mid(13) ๋ณด๋‹ค ์ž‘์„ ๋•Œ ๋Œ์„ ์—†์• ์ค„๊ฑฐ์˜ˆ์š”.

0์„ ๊ธฐ์ค€์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ๋ฐ”์œ„์˜ ์œ„์น˜ 2๋Š” mid ๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ œ๊ฑฐํ•œ๋‹ค๋ฉด, [0, 11, 14, 17, 21, 25] ์˜ ํ˜•ํƒœ๋กœ ๋ณด์ด๊ฒ ์ฃ . 

 

๊ทธ๋Ÿผ ๋‹ค์Œ์œผ๋กœ ๋‘ ๋ฒˆ์งธ ๋ฐ”์œ„์˜ ์œ„์น˜ 11๊ณผ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋ณผ๊นŒ์š”? ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ mid๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ œ๊ฑฐํ•œ๋‹ค๋ฉด, [0, 14, 17, 21, 25] ์˜ ํ˜•ํƒœ๋กœ ๋ณด์ด๊ฒ ์ฃ .

 

์ด ๋ฒˆ์—๋Š” ์„ธ ๋ฒˆ์งธ๋ฅผ ๋ณด๋„๋ก ํ• ๊ฒŒ์š”. (์ฐธ๊ณ ๋กœ ์‚ญ์ œ๋˜๋Š” ๋Œ์˜ ๊ฐœ์ˆ˜๋Š” ์ง€๊ธˆ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„์š”. ๋ช‡ ๊ฐœ์˜ ๋Œ์ด ์‚ญ์ œ๋˜๋Š”์ง€์˜ '๊ฐ€๋Šฅ์„ฑ ํŒ๋‹จ'์€ ์ด๋ถ„ํƒ์ƒ‰์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•  ๋•Œ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.) ์ด ๋ฒˆ์—๋Š” mid ๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ œ๊ฑฐํ•  ์ˆ˜ ์—†์–ด์š”.

๊ทธ๋ž˜์„œ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๋„ค ๋ฒˆ์งธ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋‹ค์Œ ๋ฐ”์œ„์™€์˜ ๊ฑฐ๋ฆฌ ๋น„๊ต๋Š” ์ด์ „์˜ ๋Œ์ธ [14] ๋กœ ์„ค์ •์„ ํ•ด์•ผํ•  ํ…Œ๋‹ˆ, start๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

 

 

์ด ๊ณผ์ •์„ ๋ชจ๋‘ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด, ์ œ๊ฑฐ๋˜๋Š” ๋ฐ”์œ„์˜ ๊ฐœ์ˆ˜๊ฐ€ 5๊ฐœ์ธ ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ , ์ด์ œ ๊ฐ€๋Šฅ์„ฑ ํŒ๋‹จ์„ ํ•ด์•ผํ•ด์š”.

 

 

 

2. ๊ฒฐ๊ณผ๊ฐ’์˜ ์œ ํšจ์„ฑ ํŒ๋‹จ ํ›„ ๋ฒ”์œ„ ๊ฐฑ์‹ 

mid ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ์†Œ๋กœ ์„ค์ •ํ–ˆ์„ ๋•Œ, ์ œ๊ฑฐ๋œ ๋ฐ”์œ„๊ฐ€ n์˜ ๋ฒ”์œ„ ๋‚ด์— ๋“ค์–ด์˜ค๋Š” ์œ ํšจ๊ฐ’์ธ๊ฐ€

 

mid๊ฐ€ 13์ผ ๋•Œ ์ œ๊ฑฐ๋˜๋Š” ๋ฐ”์œ„๋Š” 5๊ฐœ์ž…๋‹ˆ๋‹ค. n๋ณด๋‹ค ํฐ ์ˆ˜์ž…๋‹ˆ๋‹ค. 

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋ ๊นŒ์š”? 

๋ฐ”์œ„๋ฅผ ์ค„์—ฌ์•ผ๊ฒ ์ฃ ? ๊ทธ๋Ÿผ high๋ฅผ ๋‚ฎ๊ฒŒ ์ฃผ๋ฉด ๋˜๊ณ , ์ด๋ถ„ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์˜ํ•ด high๊ฐ’์„ mid ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ ๋‹ต์„ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ์š”.

๊ณ„์† ์ง„ํ–‰์‹œํ‚ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์•„์š”.

 

/* low = 0, high = 25, mid = 13
 * R = 5, ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๋ถˆ๊ฐ€๋Šฅ
 */
 ↓           ↓
[0]  2  11  14  17  21  [25]
     R   R       R   R    R


/* low = 0, high = 13, mid = 6
 * R = 3, ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๋ถˆ๊ฐ€๋Šฅ
 */ 
 ↓       ↓       ↓        ↓
[0]  2  11  14  17  21  [25]
     R       R       R    


/* low = 0, high = 6, mid = 3
 * R = 1, ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๊ฐ€๋Šฅ.
 * ์ตœ์†Œ๊ฑฐ๋ฆฌ ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด answer๊ณผ ๋น„๊ต ํ›„ ๊ณ„์† ์ง„ํ–‰
 */
 ↓       ↓   ↓   ↓   ↓    ↓
[0]  2  11  14  17  21  [25]
     R 


/* low = 4, high = 6, mid = 5
 * R = 3, ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๋ถˆ๊ฐ€๋Šฅ.
 */
 ↓       ↓       ↓        ↓
[0]  2  11  14  17  21  [25]
     R       R       R    


/* low = 4, high = 5, mid = 4
 * R = 2, ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๊ฐ€๋Šฅ.
 * ์ตœ์†Œ๊ฑฐ๋ฆฌ ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด answer๊ณผ ๋น„๊ต ํ›„ ๊ณ„์† ์ง„ํ–‰
 */
 ↓       ↓       ↓   ↓    ↓
[0]  2  11  14  17  21  [25]
     R       R
     
     
// low = 5, high = 5 -> while ์กฐ๊ฑด ํƒˆ์ถœ ~

 

 

์ดํ•ด์— ๋„์›€์ด ๋˜์…จ๋‚˜์š” ใ€ฐ๏ธ 

๊ทธ๋Ÿผ ๋ชจ๋‘ ์ฆ์ฝ”ํ•˜์„ธ์š”.,,