ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ฐ€๋กœ๋“ฑ ํŒŒ์ด์ฌ

2021. 5. 16. 12:28ใ†๐Ÿ”ฑ Algorithm/Else

๋ฌธ์ œ ์„ค๋ช…

์„œ์šธ์‹œ์— ์ผ์ง์„  ๋ชจ์–‘์˜ ์ƒˆ๋กœ์šด ๋„๋กœ๊ฐ€ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ๋„๋กœ์˜ ์ „์ฒด ๊ธธ์ด๋Š” l์ด๊ณ  ๋„๋กœ์—๋Š” ์ด n๊ฐœ์˜ ๊ฐ€๋กœ๋“ฑ์ด ์„ธ์›Œ์กŒ์Šต๋‹ˆ๋‹ค. ์ด ๋„๋กœ์˜ ๋ชจ๋“  ๊ฐ€๋กœ๋“ฑ์— ์ „๊ตฌ๋ฅผ ์‚ฌ์„œ ๋‹ฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ „๊ตฌ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ธฐ์ค€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์ „๊ตฌ๋Š” ๊ธธ์˜ ์ขŒ์ธก, ์šฐ์ธก ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ๊ฐ d ๊ธธ์ด๋งŒํผ ๊ธธ์„ ๋ฐํž ์ˆ˜ ์žˆ๊ณ , d๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  2. ๋ชจ๋“  ๊ฐ€๋กœ๋“ฑ์—๋Š” ๊ฐ™์€ ์ข…๋ฅ˜(d ๊ฐ’์ด ๊ฐ™์€)์˜ ์ „๊ตฌ๋ฅผ ๋‹ฌ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  3. ์•ˆ์ „์„ ์œ„ํ•˜์—ฌ ๋„๋กœ์œ„์— ์–ด๋‘์šด ๋ถ€๋ถ„์ด ์žˆ์–ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ, d ๊ฐ’์ด ์ถฉ๋ถ„ํžˆ ํฌ๋‹ค๋ฉด ์ „์ฒด ๋„๋กœ๋ฅผ ๋ฐ๊ฒŒ ๋น„์ถœ ์ˆ˜ ์žˆ์ง€๋งŒ, d ๊ฐ’์ด ์ž‘์•„์ง„๋‹ค๋ฉด ๋„๋กœ ์œ„์— ๋น›์ด ๋‹ฟ์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ์ƒ๊ธธ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๋„๋กœ ์œ„์— ์–ด๋‘์šด ๋ถ€๋ถ„์ด ์ƒ๊ธฐ์ง€ ์•Š๋„๋ก ํ•˜๋Š” d ๊ฐ’ ์ค‘ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒด ๋„๋กœ์˜ ๊ธธ์ด l, ๊ฐ€๋กœ๋“ฑ์ด ์„ธ์›Œ์ ธ ์žˆ๋Š” ์œ„์น˜๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด v๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์œ„์˜ ๋ชจ๋“  ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” d ์˜ ์ตœ์†Ÿ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

  • l์€ 1 ์ด์ƒ 1,000,000,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • v์—๋Š” ๊ฐ€๋กœ๋“ฑ์˜ ์œ„์น˜์ •๋ณด๊ฐ€ ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ€๋กœ๋“ฑ์˜ ์œ„์น˜๋Š” 0 ์ด์ƒ l ์ดํ•˜์˜ ์ •์ˆ˜์ด๋ฉฐ, ๊ฐ™์€ ์œ„์น˜์— 2๊ฐœ ์ด์ƒ์˜ ๊ฐ€๋กœ๋“ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๊ฐ€๋กœ๋“ฑ์˜ ๊ฐœ์ˆ˜๋Š” 1์ด์ƒ 1,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

l v output
15 [15,5,3,7,9,14,0] 3
5 [2,5] 2

 


์ ‘๊ทผ

- ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด์ง„ํƒ์ƒ‰์˜ ์–‘์ƒ์œผ๋กœ ์ ‘๊ทผ

- ๊ฐ€๋กœ๋“ฑ ๊ฐ„์˜ distance๋ฅผ distance_list์— append

- distance ๊ฐ’์€ ์ธ์ ‘ํ•œ ๊ฐ€๋กœ๋“ฑ ๊ฐ„ ๊ฑฐ๋ฆฌ๋ฅผ 2๋“ฑ๋ถ„ํ•œ ๊ฐ’์œผ๋กœ ๊ตฌํ•œ๋‹ค. (๊ฑฐ๋ฆฌ๊ฐ€ ํ™€์ˆ˜์ผ ๊ฒฝ์šฐ +1)

def solution(road_length, locations):
    locations.sort()
    print(locations)
    distance_list = []  # ๊ฐ€๋กœ๋“ฑ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ

    if 0 < locations[0]:
        distance_list.append(locations[0])

    for x, y in zip(locations[1:], locations[:-1]):
        if (x - y) % 2 == 0:
            distance = (x - y) // 2
        else:
            distance = (x - y) // 2 + 1
        distance_list.append(distance)

    if road_length > locations[-1]:
        distance_list.append(road_length - locations[-1])
        
    return max(distance_list)

 

์ฃผ์˜ํ•  ์ 

- locations[0], locations[-1]์ด ๊ผญ road์˜ ์‹œ์ž‘์ ๊ณผ ๋์ ์— ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ํ•จ์ •์œผ๋กœ ์ด์–ด์ง„๋‹ค.
- locations[0], locations[-1] ์€ ์ฒซ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๊ฐ€๋กœ๋“ฑ์ผ ๋ฟ์ด์ง€, ์ด๋“ค์˜ ์œ„์น˜๊ฐ€ ๊ผญ road์˜ ์‹œ์ž‘์ ๊ณผ ๋์ ์— ์žˆ์„ ํ•„์š”๋Š” ์—†๋‹ค.

 

ํ’€์ด

- distance๋ฅผ ๊ฐ€์žฅ ํฌ๊ฒŒ ์žก์•„ distance_list์— ๋„ฃ์œผ๋ฉด, ๋ชจ๋“  ๊ฑฐ๋ฆฌ๋ฅผ ๋‹ค ๋น„์ถœ ์ˆ˜ ์žˆ๋‹ค.
- ํ•˜์ง€๋งŒ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์€, ๋ชจ๋“  ๊ฑฐ๋ฆฌ๋ฅผ ๋‹ค ๋น„์ถœ ์ˆ˜ ์žˆ๋Š” distance ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ
- distance_list์—” ๊ฐ€๋กœ๋“ฑ ์‚ฌ์ด๋ฅผ ๋‹ค ์ปค๋ฒ„ํ•  ์ˆ˜ ์žˆ๋Š” distance์˜ ๋ฒ”์œ„๊ฐ€ ๋“ค์–ด์žˆ๋‹ค. => ์ด์ค‘์—์„œ ์ตœ์†Œ๊ฐ’(min(distance_list))์„ ๊ตฌํ•˜๋ฉด, ์–ด๋–ค ๊ณณ์€ ์ปค๋ฒ„ํ•˜๊ณ , ๋‹ค๋ฅธ ๊ณณ์€ ์ปค๋ฒ„ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
- ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— d_list์˜ ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์˜ณ๋‹ค (๊ฐ€์žฅ ํฐ ๊ฐ’: ๊ฐ€๋กœ๋“ฑ ์‚ฌ์ด๊ฐ€ ๊ฐ€์žฅ ๋งŽ์ด ๋–จ์–ด์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ distance)

 

๋‹ค๋ฅธ ํ’€์ด : parametric search

def solution(road_length: int, locations: list):
    locations.sort()

    left = 1
    right = road_length

    while left <= right:
        # mid๋ฅผ ๋น›์˜ ๋ฒ”์œ„๋กœ ์ •์˜ => ์ ์ฐจ ์ค„์—ฌ๋‚˜๊ฐ„๋‹ค ์ด์ง„ ํƒ์ƒ‰ ์ฒ˜๋Ÿผ
        mid = (left + right) // 2
        if is_possible(road_length, locations, mid):  # True ์ผ ๋•Œ
            right = mid - 1
        else:
            left = mid + 1
    return right + 1


def is_possible(road_length, locations, light_range):
    if (0 < locations[0] - light_range) or (locations[-1] + light_range < road_length):
        return False
    for i in range(1, len(locations)):
        if locations[i - 1] + light_range < locations[i] - light_range:  # ๊ฐ€๋กœ๋“ฑ ๊ฐ„ ๊ณต๋ฐฑ์ด ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ
            return False
    return True