[Programmers] K๋ฒˆ์งธ ์ˆ˜

Updated:

K๋ฒˆ์งธ ์ˆ˜

K๋ฒˆ์งธย ์ˆ˜ ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ฐ”๋กœ ์ด๋™ํ•œ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๊ต‰์žฅํžˆ ์‰ฌ์šด ๋ฌธ์ œ์ธ๋ฐ ์ฝ”๋“œ๋ฅผ ์ค„์ด๋Š” ๊ณผ์ •์ด ์ธ์ƒ์ ์ด๋ผ์„œ ๊ธ€๋กœ ๋‚จ๊ธด๋‹ค.

์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์ฃผ์–ด์ง„ index ์— ๋งž๊ฒŒ ์ž๋ฅธ ํ›„ ์ •๋ ฌํ•˜๊ณ  ํŠน์ • ๊ฐ’์„ ๋ฝ‘์•„๋‚ด๋Š” ๊ฒƒ์ด๋‹ค.

def solution(array, commands):
	answer = []
	for val in commands:
		i = val[0]
		j = val[1]
		k = val[2]
		temp = array[i-1:j]
		temp.sort()
		answer.append(temp[k-1])
	return answer

def solution(array, commands):
	answer = []
	for val in commands:
		i, j, k = val
		temp = list(sorted(array[i-1:j]))
		answer.append(temp[k-1])
	return answer

def solution(array, commands):
	answer = []
	for val in commands:
		i, j, k = val
		answer.append(list(sorted(array[i-1:j]))[k-1])
	return answer

๋งจ ์ฒ˜์Œ solution ํ•จ์ˆ˜๊ฐ€ ๋‚ด๊ฐ€ ํ’€์—ˆ๋˜ ํ•จ์ˆ˜์ด๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ •๋ง ํŽธํ•˜๊ณ  ์‹ ๊ธฐํ–ˆ๋˜๊ฒŒ ๋‘ ๋ฒˆ์งธ solution ์„ ๋ณด๋ฉด i, j, k ๋ณ€์ˆ˜๋ฅผ ํ•œ๋ฒˆ์— ๋Œ€์ž…ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰ solution ์„ ๋ณด๋ฉด ๋ฐฐ์—ด์„ ์ž๋ฅด๊ณ , ์ •๋ ฌํ•ด์„œ ํŠน์ • index ๊ฐ’์„ ์ถ”์ถœํ•ด append ํ•ด์ฃผ๋Š” ๊ณผ์ •์ด ๋ชจ๋‘ ํ•œ๋ฒˆ์—

์ด๋ฃจ์–ด ์ง„๋‹ค..

์•ž์„œ ํ’€์—ˆ๋˜ ๋ฌธ์ œ๋“ค๋„ ์—ฌ๋Ÿฌ ์ค„์„ ๋” ์งง๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋Š”๊ฒŒ ๋งŽ์ด ์žˆ์—ˆ๋‹ค

๊ณ„์† ์ค„์ผ ์ˆ˜ ์žˆ๋„๋ก ์—ฐ์Šต ํ•ด์•ผ๊ฒ ๋‹ค.


Categories:

Updated:

Leave a comment