๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://programmers.co.kr/learn/courses/30/lessons/42748
๋์ ํ์ด
- for๋ฌธ์ผ๋ก commands๋ฅผ ๋์๊ฐ๋ฉด์ ์ถ๋ ฅ
- alist์ ์ง๋ฅธ ๋ฆฌ์คํธ๋ฅผ ๋ด๊ณ
- new์ answer ์ฐจ๋ก๋๋ก append
# ์ฒซ๋ฒ์งธ๋ก ์์ ์ ํ์๋ ํ์ด
# def solution(array, commands):
# answer = []
# for i in range(len(commands)):
# a = int(commands[i][0]) - 1 #2-1=1
# b = int(commands[i][1]) - 1 #5-1=4
# c = int(commands[i][2]) - 1 #3-1=2
# new = array[a:b+1]
# new.sort()
# answer.append(new[c])
# return answer
# ์ง๊ธ ๋๋ฒ์จฐ๋ก ํผ ํ์ด
def solution(array, commands):
answer = []
for i in commands:
alist = array[i[0]-1:i[1]]
alist.sort()
new = alist[i[2]-1]
answer.append(new)
return answer
๋ค๋ฅธ์ฌ๋ ํ์ด
https://programmers.co.kr/learn/courses/30/lessons/42748/solution_groups?language=python3
๋๋คํจ์ ์ฌ์ฉ
def solution(array, commands):
return list(map(lambda x:sorted(array[x[0]-1:x[1]])[x[2]-1], commands))
๋๋ ๋น์ทํ์ง๋ง ๋ณ์ ์ ์ธ์ด ๋ค๋ฆ
def solution(array, commands):
answer = []
for command in commands:
i,j,k = command
answer.append(list(sorted(array[i-1:j]))[k-1])
return answer
์ค ์ด๋ ๊ฒ i,j,k ๋ก ์ ์ธ์ด ๊ฐ๋ฅํ๊ตฌ๋, ์ด๋ ๊ฒ ํ๋ฉด ์กฐ๊ธ ๋ ๊ฐ๋จํ๊ฒ ์งค ์ ์์๋ฏ! ๊ทผ๋ฐ ๋ฉ๋ชจ๋ฆฌ ํ ๋น๋์ด ๋ ๋์๊ฑฐ ์๋...? ํ ... ์๊ฐ๋ณต์ก๋๋ฅผ ๊ตฌํ๋ ๋ฐฉ๋ฒ, ํจ์จ์ฑ์ ๋ํด์ ๊ณต๋ถ๋ฅผ ์ข ๋ ํด๋ด์ผ๊ฒ ๋ค.
๋ฐ์ํ
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง ์์ฆ 1 - ๋ด์ (level.1) (0) | 2021.09.08 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ๋ ฌ - ๊ฐ์ฅ ํฐ ์ (level.2) (0) | 2021.09.07 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํด์-์ ํ๋ฒํธ ๋ชฉ๋ก (level.1) (0) | 2021.09.06 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํด์-์์ฃผํ์ง ๋ชปํ ์ ์ ( level.1) (0) | 2021.09.06 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฝ์์ ๊ฐ์์ ๋ง์ (level.1) (0) | 2021.09.03 |