[Programmers] ๊ตฌ๋ช…๋ณดํŠธ

Updated:

๊ตฌ๋ช…๋ณดํŠธ

๊ตฌ๋ช…๋ณดํŠธ ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ฐ”๋กœ ์ด๋™ํ•œ๋‹ค.

ํ•œ ๋ฒˆ์— ๋‘ ๋ช… ๋ฐ–์— ๋ชป ํƒ€๊ณ  ๋ฌด๊ฒŒ ์ œํ•œ์ด ์žˆ๋Š” ๊ตฌ๋ช…๋ณดํŠธ๋ฅผ ์ด์šฉํ•ด ์‚ฌ๋žŒ๋“ค์„ ๊ตฌํ•  ๋•Œ, ์ตœ์†Œํ•œ์˜ ์šดํ–‰ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์‚ฌ๋žŒ๋“ค์˜ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ •๋ ฌ ํ•œ ๋’ค ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ์™ผ์ชฝ, ๋งจ ์˜ค๋ฅธ์ชฝ ์—์„œ ์‹œ์ž‘ํ•ด ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค.


def solution(people, limit):
	answer = 0
	people = sorted(people)

	left_idx = 0
	right_idx = len(people) - 1

	while left_idx <= right_idx:
		if people[left_idx] + people[right_idx] <= limit:
			left_idx += 1
			right_idx -= 1
			answer += 1
		else:
			right_idx -= 1
			answer += 1

	return answer

์ œ์ผ ๊ฐ€๋ฒผ์šด ๋ฌด๊ฒŒ์™€ ์ œ์ผ ๋ฌด๊ฑฐ์šด ๋ฌด๊ฒŒ๋ฅผ ๋”ํ–ˆ์„๋•Œ ์ œํ•œ ๋ฌด๊ฒŒ๋ฅผ ๋„˜์ง€ ์•Š์œผ๋ฉด ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ index ๋ฅผ ๋ชจ๋‘ ์ด๋™ ์‹œํ‚จ๋‹ค.

๋งŒ์•ฝ ์ œ์ผ ๊ฐ€๋ฒผ์šด ๋ฌด๊ฒŒ์™€ ์ œ์ผ ๋ฌด๊ฑฐ์šด ๋ฌด๊ฒŒ๋ฅผ ๋”ํ–ˆ์„๋•Œ ์ œํ•œ ๋ฌด๊ฒŒ๋ฅผ ๋„˜๋Š”๋‹ค๋ฉด ์ œ์ผ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์€ ํ˜ผ์ž ํƒˆ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.

๋•Œ๋ฌธ์— ์˜ค๋ฅธ์ชฝ index ๋งŒ ์ด๋™์‹œํ‚ค๊ณ  ๋ฐฐ์˜ ์šดํ–‰ ํšŸ์ˆ˜๋ฅผ ์˜ฌ๋ ค์ฃผ๋ฉด ๋œ๋‹ค.


Categories:

Updated:

Leave a comment