Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ •๋ ฌ-k๋ฒˆ์งธ ์ˆ˜ (level.1)

๊ฐ์ž ๐Ÿฅ” 2021. 9. 7. 16:51
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ๋งํฌ

https://programmers.co.kr/learn/courses/30/lessons/42748

 

๋‚˜์˜ ํ’€์ด

  1. for๋ฌธ์œผ๋กœ commands๋ฅผ ๋Œ์•„๊ฐ€๋ฉด์„œ ์ถœ๋ ฅ
  2. alist์— ์งœ๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ด๊ณ 
  3. 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 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๋žŒ๋‹คํ•จ์ˆ˜ ์‚ฌ์šฉ

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 ๋กœ ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๊ตฌ๋‚˜, ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์กฐ๊ธˆ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์งค ์ˆ˜ ์žˆ์„๋“ฏ! ๊ทผ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๋Ÿ‰์ด ๋” ๋†’์€๊ฑฐ ์•„๋ƒ...? ํ ... ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•, ํšจ์œจ์„ฑ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€๋ฅผ ์ข€ ๋” ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

๋ฐ˜์‘ํ˜•