https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋‚˜๋งŒ์˜ ์นด์นด์˜ค ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌ์ง€๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ 4๊ฐœ ์ง€ํ‘œ๋กœ ์„ฑ๊ฒฉ ์œ ํ˜•์„ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์„ฑ๊ฒฉ์€ ๊ฐ ์ง€ํ‘œ์—์„œ ๋‘ ์œ ํ˜• ์ค‘ ํ•˜๋‚˜๋กœ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.

์ง€ํ‘œ ๋ฒˆํ˜ธ ์„ฑ๊ฒฉ ์œ ํ˜•
1๋ฒˆ ์ง€ํ‘œ ๋ผ์ด์–ธํ˜•(R), ํŠœ๋ธŒํ˜•(T)
2๋ฒˆ ์ง€ํ‘œ ์ฝ˜ํ˜•(C), ํ”„๋กœ๋„ํ˜•(F)
3๋ฒˆ ์ง€ํ‘œ ์ œ์ด์ง€ํ˜•(J), ๋ฌด์ง€ํ˜•(M)
4๋ฒˆ ์ง€ํ‘œ ์–ดํ”ผ์น˜ํ˜•(A), ๋„ค์˜คํ˜•(N)

 

4๊ฐœ์˜ ์ง€ํ‘œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์„ฑ๊ฒฉ ์œ ํ˜•์€ ์ด 16(=2 x 2 x 2 x 2)๊ฐ€์ง€๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "RFMN"์ด๋‚˜ "TCMA"์™€ ๊ฐ™์€ ์„ฑ๊ฒฉ ์œ ํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ฒ€์‚ฌ์ง€์—๋Š” ์ด n๊ฐœ์˜ ์งˆ๋ฌธ์ด ์žˆ๊ณ , ๊ฐ ์งˆ๋ฌธ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ 7๊ฐœ์˜ ์„ ํƒ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋งค์šฐ ๋น„๋™์˜
  • ๋น„๋™์˜
  • ์•ฝ๊ฐ„ ๋น„๋™์˜
  • ๋ชจ๋ฅด๊ฒ ์Œ
  • ์•ฝ๊ฐ„ ๋™์˜
  • ๋™์˜
  • ๋งค์šฐ ๋™์˜

๊ฐ ์งˆ๋ฌธ์€ 1๊ฐ€์ง€ ์ง€ํ‘œ๋กœ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์–ด๋–ค ํ•œ ์งˆ๋ฌธ์—์„œ 4๋ฒˆ ์ง€ํ‘œ๋กœ ์•„๋ž˜ ํ‘œ์ฒ˜๋Ÿผ ์ ์ˆ˜๋ฅผ ๋งค๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์„ ํƒ์ง€ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜
๋งค์šฐ ๋น„๋™์˜ ๋„ค์˜คํ˜• 3์ 
๋น„๋™์˜ ๋„ค์˜คํ˜• 2์ 
์•ฝ๊ฐ„ ๋น„๋™์˜ ๋„ค์˜คํ˜• 1์ 
๋ชจ๋ฅด๊ฒ ์Œ ์–ด๋–ค ์„ฑ๊ฒฉ ์œ ํ˜•๋„ ์ ์ˆ˜๋ฅผ ์–ป์ง€ ์•Š์Šต๋‹ˆ๋‹ค
์•ฝ๊ฐ„ ๋™์˜ ์–ดํ”ผ์น˜ํ˜• 1์ 
๋™์˜ ์–ดํ”ผ์น˜ํ˜• 2์ 
๋งค์šฐ ๋™์˜ ์–ดํ”ผ์น˜ํ˜• 3์ 

 

์ด๋•Œ ๊ฒ€์‚ฌ์ž๊ฐ€ ์งˆ๋ฌธ์—์„œ ์•ฝ๊ฐ„ ๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•  ๊ฒฝ์šฐ ์–ดํ”ผ์น˜ํ˜•(A) ์„ฑ๊ฒฉ ์œ ํ˜• 1์ ์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฒ€์‚ฌ์ž๊ฐ€ ๋งค์šฐ ๋น„๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•  ๊ฒฝ์šฐ ๋„ค์˜คํ˜•(N) ์„ฑ๊ฒฉ ์œ ํ˜• 3์ ์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์œ„ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋„ค์˜คํ˜•์ด ๋น„๋™์˜, ์–ดํ”ผ์น˜ํ˜•์ด ๋™์˜์ธ ๊ฒฝ์šฐ๋งŒ ์ฃผ์–ด์ง€์ง€ ์•Š๊ณ , ์งˆ๋ฌธ์— ๋”ฐ๋ผ ๋„ค์˜คํ˜•์ด ๋™์˜, ์–ดํ”ผ์น˜ํ˜•์ด ๋น„๋™์˜์ธ ๊ฒฝ์šฐ๋„ ์ฃผ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๊ฐ ์„ ํƒ์ง€๋Š” ๊ณ ์ •์ ์ธ ํฌ๊ธฐ์˜ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋งค์šฐ ๋™์˜๋‚˜ ๋งค์šฐ ๋น„๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด 3์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
๋™์˜๋‚˜ ๋น„๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด 2์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
์•ฝ๊ฐ„ ๋™์˜๋‚˜ ์•ฝ๊ฐ„ ๋น„๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด 1์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
๋ชจ๋ฅด๊ฒ ์Œ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด ์ ์ˆ˜๋ฅผ ์–ป์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๊ฒ€์‚ฌ ๊ฒฐ๊ณผ๋Š” ๋ชจ๋“  ์งˆ๋ฌธ์˜ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜๋ฅผ ๋”ํ•˜์—ฌ ๊ฐ ์ง€ํ‘œ์—์„œ ๋” ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์„ฑ๊ฒฉ ์œ ํ˜•์ด ๊ฒ€์‚ฌ์ž์˜ ์„ฑ๊ฒฉ ์œ ํ˜•์ด๋ผ๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ํ•˜๋‚˜์˜ ์ง€ํ‘œ์—์„œ ๊ฐ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด, ๋‘ ์„ฑ๊ฒฉ ์œ ํ˜• ์ค‘ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๋น ๋ฅธ ์„ฑ๊ฒฉ ์œ ํ˜•์„ ๊ฒ€์‚ฌ์ž์˜ ์„ฑ๊ฒฉ ์œ ํ˜•์ด๋ผ๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

์งˆ๋ฌธ๋งˆ๋‹ค ํŒ๋‹จํ•˜๋Š” ์ง€ํ‘œ๋ฅผ ๋‹ด์€ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด survey์™€ ๊ฒ€์‚ฌ์ž๊ฐ€ ๊ฐ ์งˆ๋ฌธ๋งˆ๋‹ค ์„ ํƒํ•œ ์„ ํƒ์ง€๋ฅผ ๋‹ด์€ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด choices๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ, ๊ฒ€์‚ฌ์ž์˜ ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ๋ฅผ ์ง€ํ‘œ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ survey์˜ ๊ธธ์ด ( = n) ≤ 1,000
  • survey์˜ ์›์†Œ๋Š” "RT", "TR", "FC", "CF", "MJ", "JM", "AN", "NA" ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
  • survey[i]์˜ ์ฒซ ๋ฒˆ์งธ ์บ๋ฆญํ„ฐ๋Š” i+1๋ฒˆ ์งˆ๋ฌธ์˜ ๋น„๋™์˜ ๊ด€๋ จ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฐ›๋Š” ์„ฑ๊ฒฉ ์œ ํ˜•์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • survey[i]์˜ ๋‘ ๋ฒˆ์งธ ์บ๋ฆญํ„ฐ๋Š” i+1๋ฒˆ ์งˆ๋ฌธ์˜ ๋™์˜ ๊ด€๋ จ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฐ›๋Š” ์„ฑ๊ฒฉ ์œ ํ˜•์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • choices์˜ ๊ธธ์ด = survey์˜ ๊ธธ์ด
  • choices[i]๋Š” ๊ฒ€์‚ฌ์ž๊ฐ€ ์„ ํƒํ•œ i+1๋ฒˆ์งธ ์งˆ๋ฌธ์˜ ์„ ํƒ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • 1 ≤ choices์˜ ์›์†Œ ≤ 7
choices ๋œป
1 ๋งค์šฐ ๋น„๋™์˜
2 ๋น„๋™์˜
3 ์•ฝ๊ฐ„ ๋น„๋™์˜
4 ๋ชจ๋ฅด๊ฒ ์Œ
5 ์•ฝ๊ฐ„ ๋™์˜
6 ๋™์˜
7 ๋งค์šฐ ๋™์˜

 

 

์ž…์ถœ๋ ฅ ์˜ˆ

survey choices result
["AN", "CF", "MJ", "RT", "NA"] [5, 3, 2, 7, 5] "TCMA"
["TR", "RT", "TR"] [7, 1, 3] "RCJA"

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(survey, choices):
    answer = ''

    types = [["R","T"],["C","F"],["J","M"],["A","N"]]
    scores = [[0,0],[0,0],[0,0],[0,0]]

    # survey ๊ธฐ๋ฐ˜ ์ ์ˆ˜ ๊ณ„์‚ฐ
    n = len(survey)
    for idx in range(n):
        front = survey[idx][0]
        back = survey[idx][1]
        if "R" in survey[idx]:
            if choices[idx] < 4:
                scores[0][types[0].index(front)] += (4-choices[idx])
            else:
                scores[0][types[0].index(back)] += (choices[idx]-4)
        elif "C" in survey[idx]:
            if choices[idx] < 4:
                scores[1][types[1].index(front)] += (4-choices[idx])
            else:
                scores[1][types[1].index(back)] += (choices[idx]-4)
        elif "J" in survey[idx]:
            if choices[idx] < 4:
                scores[2][types[2].index(front)] += (4-choices[idx])
            else:
                scores[2][types[2].index(back)] += (choices[idx]-4)
        elif "A" in survey[idx]:
            if choices[idx] < 4:
                scores[3][types[3].index(front)] += (4-choices[idx])
            else:
                scores[3][types[3].index(back)] += (choices[idx]-4)          



    # scores๋ฅผ ๋ณด๊ณ  ์ ์ˆ˜๊ฐ€ ๋†’์€ ์œ ํ˜• ์ถœ๋ ฅ 
    for idx in range(4):
        if scores[idx][0] >= scores[idx][1]:
            answer += types[idx][0]
        else:
            answer += types[idx][1]

    return answer

 

ํ’€์ด # 1

  • ๊ฐ ํƒ€์ž…๊ณผ ์ ์ˆ˜๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ์ƒ์„ฑ
  • survey ๊ธธ์ด๋งŒํผ ์ธ๋ฑ์Šค๋ฅผ ๋Œ๋ฉด์„œ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
    • survey์—์„œ ์•ž๊ธ€์ž์™€ ๋’ท๊ธ€์ž๋ฅผ ๊ตฌ๋ถ„
    • ์–ด๋–ค ๊ธ€์ž๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  socres์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
    • ์ ์ˆ˜๊ฐ€ 4๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ž๊ธ€์ž์˜ ์ธ๋ฑ์Šค์— 4๋ณด๋‹ค ํฌ๋ฉด ๋’ท๊ธ€์ž์˜ ์ธ๋ฑ์Šค์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
  • ์•ž์—์„œ ๊ณ„์‚ฐํ•œ scores๋ฅผ ๋Œ๋ฉด์„œ ์ ์ˆ˜๊ฐ€ ๋†’์€ ๊ฐ’์˜ type์„ answer๋กœ ์ถœ๋ ฅ

 

# 2
def solution(survey, choices):
    answer = ''

    scores = {"A":0, "N":0, "C":0, "F":0, "M":0, "J":0, "R":0, "T":0}
    for idx, choice in enumerate(choices):
        if choice - 4 > 0:
            scores[survey[idx][1]] += choice - 4
        elif choice - 4 < 0:
            scores[survey[idx][0]] += 4 - choice

    if scores["R"] >= scores["T"]:
        answer += "R"
    else:
        answer += "T"

    if scores["C"] >= scores["F"]:
        answer += "C"
    else:
        answer += "F"

    if scores["J"] >= scores["M"]:
        answer += "J"
    else:
        answer += "M"

    if scores["A"] >= scores["N"]:
        answer += "A"
    else:
        answer += "N"
    

    return answer

 

ํ’€์ด #2

  • ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค์— ์œ ํ˜• ๊ฐ’์— ์ ์ˆ˜๋ฅผ ํ‘œ๊ธฐ
  • ์ ์ˆ˜๊ฐ€ 4๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ž๊ธ€์ž์˜ ์ธ๋ฑ์Šค์— 4๋ณด๋‹ค ํฌ๋ฉด ๋’ท๊ธ€์ž์˜ ์ธ๋ฑ์Šค์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
  • RT, CF, JM, AN์˜ ์ ์ˆ˜๋ฅผ ๊ฐ๊ฐ ๋น„๊ตํ•ด์„œ answer๋กœ ์ถœ๋ ฅ

 

# 3
def solution(survey, choices):
    types = {"RT":0,"CF":0,"JM":0,"AN":0}
    
    for A,B in zip(survey,choices):
        if A not in types.keys():
            A = A[::-1]
            types[A] -= B-4
        else:
            types[A] += B-4

    answer = ''    
    for name in types.keys():
        if types[name] > 0:
            answer += name[1]
        elif types[name] < 0:
            answer += name[0]
        else:
            answer += sorted(name)[0]

    return answer

 

ํ’€์ด #3

  • ์ง์ด ๋˜๋Š” ์œ ํ˜•๋ผ๋ฆฌ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋กœ ๊ตฌ์„ฑ
  • survey์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์— ํ‚ค๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์ ์ˆ˜ ๋นผ๊ธฐ (์ˆœ์„œ ๋ณ€๊ฒฝ O)
    • ์ ์ˆ˜๊ฐ€ ๋†’์œผ๋ฉด (์ ์ˆ˜-4)๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฏ€๋กœ ์–‘์ˆ˜ = ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ์ˆ˜ ๋นผ๊ธฐ
    • ์ ์ˆ˜๊ฐ€ ๋‚ฎ์œผ๋ฉด (์ ์ˆ˜-4)๊ฐ€ 0๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์Œ์ˆ˜ = ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
  • survey์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค์ธ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ƒฅ ์ ์ˆ˜ ๋”ํ•˜๊ธฐ (์ˆœ์„œ ๋ณ€๊ฒฝ X)
    • ์ ์ˆ˜๊ฐ€ ๋†’์œผ๋ฉด (์ ์ˆ˜-4)๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฏ€๋กœ ์–‘์ˆ˜ = ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
    • ์ ์ˆ˜๊ฐ€ ๋‚ฎ์œผ๋ฉด (์ ์ˆ˜-4)๊ฐ€ 0๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์Œ์ˆ˜ = ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ์ˆ˜ ๋นผ๊ธฐ
  • ์œ„์˜ ๋ฐฉ์‹๋Œ€๋กœ ์ ์ˆ˜๋ฅผ ๋”ํ•˜๊ณ  ๋นผ๋ฉด ์ ์ˆ˜๋Š” ๊ฒฐ๊ตญ ํ‚ค์˜ ๋’ท ๋ฌธ์ž ๊ธฐ๋ฐ˜
    • ์ ์ˆ˜๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด ๋’ท ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  0๋ณด๋‹ค ์ž‘์€๋ฉด ์•ž ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅ

 

 

๋ฌธ์ œ์— ๋Œ€ํ•ด ์„ค๋ช…์ด ์ž์„ธํ•ด์„œ ์ดํ•ด๋Š” ์ž˜ ๋˜์—ˆ๋Š”๋ฐ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋‹ˆ๊นŒ ์ข€ ๋ณต์žกํ•ด์กŒ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋Š” ์ ์ˆ˜๊ฐ€ 4๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ž์— ์žˆ๋Š” ๋ฌธ์ž์— +3, 4๋ณด๋‹ค ํฌ๋ฉด ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— +3์ด ๋˜๋Š” ๊ฒƒ์„ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ’€์ด ๊ธฐ๋ฐ˜์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋‘ ๋ฒˆ์งธ ํ’€์ด๊นŒ์ง€ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ๋ณด๋‹ค ๋ณด๋‹ˆ ์„ธ ๋ฒˆ์งธ ํ’€์ด๊ฐ€ ์ง„์งœ ์”ฝํฌ๋น…์ด์–ด์„œ ์ถ”๊ฐ€ํ–ˆ๋‹ค....๋ฌธ์ž์—ด์„ ๋ฐ”๊พธ๋ฉด์„œ ์ ์ˆ˜ ๋”ํ•˜๊ธฐ? ๋„๋Œ€์ฒด ์ด๋Ÿฐ ์ƒ๊ฐ์€ ์–ด๋–ป๊ฒŒ ํ•˜์‹œ๋Š”๊ฑฐ์—์š”? ํ•ด์„ํ•˜๊ธฐ๋„ ์–ด๋ ค์› ๋Š”๋ฐ...์ฒœ์žฌ ์•„๋‹ˆ์‹ ๊ฐ€์š”...? 

https://school.programmers.co.kr/learn/courses/30/lessons/120840

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋จธ์“ฑ์ด๋Š” ๊ตฌ์Šฌ์„ ์นœ๊ตฌ๋“ค์—๊ฒŒ ๋‚˜๋ˆ„์–ด์ฃผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์Šฌ์€ ๋ชจ๋‘ ๋‹ค๋ฅด๊ฒŒ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ๋จธ์“ฑ์ด๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ตฌ์Šฌ์˜ ๊ฐœ์ˆ˜ balls์™€ ์นœ๊ตฌ๋“ค์—๊ฒŒ ๋‚˜๋ˆ„์–ด ์ค„ ๊ตฌ์Šฌ ๊ฐœ์ˆ˜ share์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, balls๊ฐœ์˜ ๊ตฌ์Šฌ ์ค‘ share๊ฐœ์˜ ๊ตฌ์Šฌ์„ ๊ณ ๋ฅด๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ balls ≤ 30
  • 1 ≤ share ≤ 30
  • ๊ตฌ์Šฌ์„ ๊ณ ๋ฅด๋Š” ์ˆœ์„œ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • share ≤ balls

 

์ž…์ถœ๋ ฅ ์˜ˆ

balls share result
3 2 3
5 3 10

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1 (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
def solution(balls, share):
    answer = 1
    
    ball_list = list(range(balls,1,-1))
    share_list = list(range(1,share+1))
    
    for idx in range(1,share+1):
        answer *= ball_list[idx-1]
        answer = int(answer/share_list[idx-1])
    
    
    return answer

 

ํ’€์ด # 1

  • ๊ณต์˜ ๊ฐฏ์ˆ˜์—์„œ 1์”ฉ ๋นผ์„œ ball_list์— ๋‹ด๊ณ , ๊ณต์œ ํ•  ๊ณต์˜ ๊ฐฏ์ˆ˜๋ฅผ 1๋ถ€ํ„ฐ 1์”ฉ ๋”ํ•ด share_list์— ๋‹ด๊ธฐ
  • share ์ˆซ์ž ๋ฒ”์œ„์—์„œ ball_list์˜ ์›์†Œ๋Š” ๊ณฑํ•˜๊ณ  share_list์˜ ์›์†Œ๋Š” ๋‚˜๋ˆ„๊ธฐ

 

# 2
def solution(balls, share):
    answer = 1
    cnt = 0

    while cnt < share:
        answer *= (balls-cnt)
        answer = int(answer/(cnt+1))
        cnt += 1    
    
    return answer

 

ํ’€์ด #2

  • cnt๊ฐ€ share์˜ ์ˆซ์ž๋ณด๋‹ค ์ž‘์„๋™์•ˆ ๊ณฑํ•˜๊ธฐ์™€ ๋‚˜๋ˆ„๊ธฐ๋ฅผ ์‹คํ–‰
    • balls-cnt๋ฅผ ๊ณฑํ•˜๊ธฐ
    • ์œ„์˜ ์ˆซ์ž์—์„œ cnt+1์„ ๋‚˜๋ˆ„๊ธฐ 

 

# 3
import math

def solution(balls, share):
    return math.comb(balls, share)

 

ํ’€์ด #3

  • math ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ combination ํ•จ์ˆ˜ ์‚ฌ์šฉ

 

 

์ฒ˜์Œ์— ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๊ณ  ๋‚˜๋ˆ„๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํ•˜๋‹ค๊ฐ€ ์ž˜ ์•ˆ๋˜๊ธธ๋ž˜ ๋ฆฌ์ŠคํŠธ๋กœ ํ’€์–ด๋ดค๋Š”๋ฐ ์—ญ์‹œ๋‚˜ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋‹ค. ๊ทธ๋ž˜์„œ ์ˆซ์ž๋ฅผ ์กฐ์ž‘ํ•ด์„œ while๋ฌธ์— ๋„ฃ์—ˆ๋”๋‹ˆ ๊ฒฐ๊ณผ๋Š” ๋‚˜์™”์ง€๋งŒ ๋ญ”๊ฐ€ ๋” ๋น ๋ฅด๊ฒŒ ๋‚˜์™”์œผ๋ฉด ์ข‹๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ค๋ณด๋‹ˆ math ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— combination ํ•จ์ˆ˜๊ฐ€ ์žˆ์—ˆ๋‹ค...๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ์“ฐ๋Š”๊ฑฐ ์ตœ๊ณ ...!!

https://school.programmers.co.kr/learn/courses/30/lessons/120876

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์„ ๋ถ„ 3๊ฐœ๊ฐ€ ํ‰ํ–‰ํ•˜๊ฒŒ ๋†“์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ ์„ ๋ถ„์˜ ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ๊ฐ€ [[start, end], [start, end], [start, end]] ํ˜•ํƒœ๋กœ ๋“ค์–ด์žˆ๋Š” 2์ฐจ์› ๋ฐฐ์—ด lines๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋‘ ๊ฐœ ์ด์ƒ์˜ ์„ ๋ถ„์ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์˜ ๊ธธ์ด๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

lines๊ฐ€ [[0, 2], [-3, -1], [-2, 1]]์ผ ๋•Œ ๊ทธ๋ฆผ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์„ ๋ถ„์ด ๋‘ ๊ฐœ ์ด์ƒ ๊ฒน์นœ ๊ณณ์€ [-2, -1], [0, 1]๋กœ ๊ธธ์ด 2๋งŒํผ ๊ฒน์ณ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ œํ•œ ์‚ฌํ•ญ

  • lines์˜ ๊ธธ์ด = 3
  • lines์˜ ์›์†Œ์˜ ๊ธธ์ด = 2
  • ๋ชจ๋“  ์„ ๋ถ„์€ ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ์ž…๋‹ˆ๋‹ค.
  • lines์˜ ์›์†Œ๋Š” [a, b] ํ˜•ํƒœ์ด๋ฉฐ, a, b๋Š” ๊ฐ๊ฐ ์„ ๋ถ„์˜ ์–‘ ๋์  ์ž…๋‹ˆ๋‹ค.
  • -100 ≤ a < b ≤ 100

 

์ž…์ถœ๋ ฅ ์˜ˆ

lines result
[[0, 1], [2, 5], [3, 9]] 2
[[-1, 1], [1, 3], [3, 9]] 0
[[0, 5], [3, 9], [1, 10]] 8

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(lines):
    answer = 0
    count = [0]*200
    
    starts = []
    ends = []

    for line in lines:
        start, end = line

        starts.append(start)
        ends.append(end)

        for num in range(start, end):
            count[num+100] += 1

    for cnt in count[100+min(starts):100+max(ends)+1]:
        if cnt > 1:
            answer += 1

    return answer

 

ํ’€์ด # 1

  • ์›์†Œ๊ฐ€ 0์ธ ๊ธธ์ด 200์˜ ๋ฆฌ์ŠคํŠธ count ์ƒ์„ฑ
    • ์„ ๋ถ„์ด ์  (-100,100) ๋ฒ”์œ„์— ์žˆ์œผ๋ฏ€๋กœ ๊ธธ์ด๊ฐ€ 200
  • ๊ฐ ์„ ๋ถ„์˜ ์‹œ์ž‘ ๋ฐ ๋ ์ขŒํ‘œ๋ฅผ ๋‹ด์„ starts / ends ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  • linelist ๋Œ๊ธฐ
    • ๊ฐ ์„ ๋ถ„์˜ ์‹œ์ž‘ ๋ฐ ๋ ์ขŒํ‘œ ์ถ”๊ฐ€
    • ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ ์‚ฌ์ด ์ˆซ์ž์— +1 (count ๋ฆฌ์ŠคํŠธ ๋ฒ”์œ„๊ฐ€ -100~100์ด๋ฏ€๋กœ ์ธ๋ฑ์Šค์— +100)
  • ์‹œ์ž‘ ์ขŒํ‘œ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜์™€ ๋ ์ขŒํ‘œ ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜ ๋ฒ”์œ„์—์„œ count๊ฐ€ 2๋ณด๋‹ค ํฌ๋ฉด ์„ ๋ถ„์ด ๊ฒน์น˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ +1

 

# 2
def solution(lines):
    s1 = set(i for i in range(lines[0][0], lines[0][1]))
    s2 = set(i for i in range(lines[1][0], lines[1][1]))
    s3 = set(i for i in range(lines[2][0], lines[2][1]))
    
    return len((s1 & s2) | (s2 & s3) | (s1 & s3))

 

ํ’€์ด #2

  • ๊ฐ ๋ผ์ธ์˜ ์‹œ์ž‘ ์ขŒํ‘œ๋ถ€ํ„ฐ ๋ ์ขŒํ‘œ๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์„ธํŠธ๋กœ ์ƒ์„ฑ (s1 / s2 / s3)
  • ์„ ๋ถ„๋ผ๋ฆฌ์˜ ๊ต์ง‘ํ•ฉ(&)๋“ค์˜ ํ•ฉ์ง‘ํ•ฉ(|) ๊ธธ์ด๋ฅผ ์ถœ๋ ฅ

 

 

(-100,100) ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฑธ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ์—ˆ๋Š”๋ฐ ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค๊ณ  ๋‚˜๋‹ˆ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๊ตณ์ด ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ์˜ min/max๋Š” ๊ตฌํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์„ ๊ฑฐ ๊ฐ™๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด์ฒ˜๋Ÿผ ์„ธํŠธ์—์„œ ํ•ฉ์ง‘ํ•ฉ๊ณผ ๊ต์ง‘ํ•ฉ์„ ์ด์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ธ๋ฐ...๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด ๋‚ด๋Š” ๊ฒŒ ํ•ญ์ƒ ์–ด๋ ต๋‹ค...

https://school.programmers.co.kr/learn/courses/30/lessons/120880

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์ •์ˆ˜ n์„ ๊ธฐ์ค€์œผ๋กœ n๊ณผ ๊ฐ€๊นŒ์šด ์ˆ˜๋ถ€ํ„ฐ ์ •๋ ฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ n์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋” ํฐ ์ˆ˜๋ฅผ ์•ž์— ์˜ค๋„๋ก ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด numlist์™€ ์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ numlist์˜ ์›์†Œ๋ฅผ n์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€๊นŒ์šด ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ n ≤ 10,000
  • 1 ≤ numlist์˜ ์›์†Œ ≤ 10,000
  • 1 ≤ numlist์˜ ๊ธธ์ด ≤ 100
  • numlist๋Š” ์ค‘๋ณต๋œ ์›์†Œ๋ฅผ ๊ฐ–์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

numlist n result
[1, 2, 3, 4, 5, 6] 4 [4, 5, 3, 6, 2, 1]
[10000,20,36,47,40,6,10,7000] 30 [36, 40, 20, 47, 10, 6, 7000, 10000]

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(numlist, n):
    # Key = ์ˆซ์ž / Value = ์ฐจ์ด
    num_diff = {}
    for num in numlist:
        num_diff[num] = abs(num-n)   

    rank = dict(sorted(num_diff.items(), key=lambda x:(x[1],-x[0])))
    
    return list(rank.keys())

 

ํ’€์ด # 1

  • num_diff ๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    • Key๊ฐ’์— numlist์˜ ์ˆซ์ž / Value๊ฐ’์— numlist ์ˆซ์ž์™€ n์˜ ์ฐจ์ด ์ž…๋ ฅ
  • rank๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    • num_diff์˜ Value(= numlist ์ˆซ์ž์™€ n์˜ ์ฐจ์ด) ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ → x[1]
    • Value๊ฐ€ ๊ฐ™๋‹ค๋ฉด Key๊ฐ€ ํฐ ๊ฐ’ ๋จผ์ € ์˜ค๋„๋ก ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ → -x[0]
  • ์ •๋ ฌ๋œ rank ๋”•์…”๋„ˆ๋ฆฌ์˜ Key๊ฐ’(=numlist์˜ ์ˆซ์ž) ์ถœ๋ ฅ

 

# 2
def solution(numlist, n):
    numlist.sort(key=lambda x:(abs(n-x), -x))
    return numlist

 

ํ’€์ด #2

  • numlist ์ˆซ์ž์™€ n์˜ ์ฐจ์ด ์˜ค๋ฆ„์ฐจ์ˆœ / numlist ์ˆซ์ž ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
  • ์ •๋ ฌ๋œ numlist ์ถœ๋ ฅ

 

 

์ฒซ ๋ฒˆ์งธ ํ’€์ด๋Š” '์ฐจ์ด๊ฐ’ ๋งŒ๋“ค๊ธฐ → ์ฐจ์ด๊ฐ’๊ณผ ์ˆซ์ž๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์— ๋„ฃ๊ธฐ → ์ฐจ์ด๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ → ์ •๋ ฌ๋œ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ํ‚ค ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ' ์˜ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ณ  ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” '์ฐจ์ด๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ'๊ฐ€ ๋์ด๋‹ค. ์›๋ฆฌ?๋Š” ๊ฐ™์ง€๋งŒ ์–ผ๋งˆ๋‚˜ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ์„œ ์ฝ”๋“œ๊ฐ€ ์ข€ ๋” ๊น”๋”ํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค๊ณ  3928490๋ฒˆ์งธ ๋‹ค์งํ•˜๋Š” ์ค‘์ด๋‹ค. 

https://school.programmers.co.kr/learn/courses/30/lessons/181832

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์–‘์˜ ์ •์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. n × n ๋ฐฐ์—ด์— 1๋ถ€ํ„ฐ n^2๊นŒ์ง€ ์ •์ˆ˜๋ฅผ ์ธ๋ฑ์Šค [0][0]๋ถ€ํ„ฐ ์‹œ๊ณ„๋ฐฉํ–ฅ ๋‚˜์„ ํ˜•์œผ๋กœ ๋ฐฐ์น˜ํ•œ ์ด์ฐจ์› ๋ฐฐ์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ n ≤ 30

 

์ž…์ถœ๋ ฅ ์˜ˆ

n result
4 [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]
5 [[1, 2, 3, 4, 5], [16, 17, 18, 19, 6], [15, 24, 25, 20, 7], [14, 23, 22, 21, 8], [13, 12, 11, 10, 9]]

 

 

์†Œ์Šค ์ฝ”๋“œ
def solution(n):
    if n == 1:
        return [[1]]

    answer = [[0 for j in range(n)] for i in range(n)]

    i = 0       # ํ–‰
    j = 0       # ์—ด
    dir = 'r'   # ๋ฐฉํ–ฅ
    
    for num in range(n*n):
        answer[i][j] = num+1
        if dir == 'r':
            j += 1
            if j == n-1 or answer[i][j+1] != 0:
                dir = 'd'
        elif dir == 'd':
            i += 1
            if i == n-1 or answer[i+1][j] != 0:
                dir = 'l'
        elif dir == 'l':
            j -= 1
            if j == 0 or answer[i][j-1] != 0:
                dir = 'u'
        else:
            i -= 1
            if i == 0 or answer[i-1][j] != 0:
                dir = 'r'            
    
    
    return answer

 

ํ’€์ด

  • 1 x 1 ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ์— [[1]]์„ ์ถœ๋ ฅ
  • ํ–‰ ๋ฒˆํ˜ธ = i / ์—ด ๋ฒˆํ˜ธ = j / ๋ฐฉํ–ฅ = dir (r:์˜ค๋ฅธ์ชฝ → d:์•„๋ž˜์ชฝ  → l:์™ผ์ชฝ → u:์œ„์ชฝ)
  • n x n ๋ฒ”์œ„๋ฅผ ๋Œ๋ฉด์„œ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ์ฑ„์›Œ์ฃผ๋Š” for๋ฌธ
    • ๋ฐฉํ–ฅ์ด ์˜ค๋ฅธ์ชฝ์ธ ๊ฒฝ์šฐ : ์—ด ํ•œ ์นธ ์ด๋™ / ๋งˆ์ง€๋ง‰ ์—ด์ด๊ฑฐ๋‚˜ ์นธ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
    • ๋ฐฉํ–ฅ์ด ์•„๋ž˜์ชฝ์ธ ๊ฒฝ์šฐ : ํ–‰ ํ•œ ์นธ ์ด๋™ / ๋งˆ์ง€๋ง‰ ํ–‰์ด๊ฑฐ๋‚˜ ์นธ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
    • ๋ฐฉํ–ฅ์ด ์™ผ์ชฝ์ธ ๊ฒฝ์šฐ : ์—ด ํ•œ ์นธ ์•ž์œผ๋กœ ์ด๋™ / ์ฒซ ๋ฒˆ์งธ ์—ด์ด๊ฑฐ๋‚˜ ์นธ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
    • ๋ฐฉํ–ฅ์ด ์œ„์ชฝ์ธ ๊ฒฝ์šฐ : ํ–‰ ํ•œ ์นธ ์œ„๋กœ ์ด๋™ / ์ฒซ ๋ฒˆ์งธ ํ–‰์ด๊ฑฐ๋‚˜ ์นธ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ

 

 

์ด๊ฑฐ ๋ ˆ๋ฒจ0 ๋งž์•„์š”..? ๊ทธ๋ ‡๋‹ค๋ฉด ๋‚ด ์‹ค๋ ฅ์ด ๋งˆ์ด๋„ˆ์Šค์ธ๊ฐ€๋ณด๋‹ค....์ด๋Ÿฐ ์ €๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ํ•ด๋ณด๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ๊ฒ€์ƒ‰ํ•ด์„œ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ์•ž์œผ๋กœ ๋” ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค...ํ”Œ๋Ÿฌ์Šค ์‹ค๋ ฅ์ด ๋  ๋•Œ๊นŒ์ง€ ํ™”์ดํŒ…..!!

 

https://school.programmers.co.kr/learn/courses/30/lessons/181894

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์ •์ˆ˜ ๋ฐฐ์—ด arr๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ฐฐ์—ด ์•ˆ์˜ 2๊ฐ€ ๋ชจ๋‘ ํฌํ•จ๋œ ๊ฐ€์žฅ ์ž‘์€ ์—ฐ์†๋œ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

๋‹จ, arr์— 2๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ [-1]์„ return ํ•ฉ๋‹ˆ๋‹ค.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ arr์˜ ๊ธธ์ด ≤ 100,000
  • 1 ≤ arr์˜ ์›์†Œ ≤ 10

 

์ž…์ถœ๋ ฅ ์˜ˆ

arr result
[1, 2, 1, 4, 5, 2, 9] [2, 1, 4, 5, 2]
[1, 2, 1] [2]
[1, 1, 1] [-1]
[1, 2, 1, 2, 1, 10, 2, 1] [2, 1, 2, 1, 10, 2]

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1 (์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ์‹คํŒจ)
def solution(arr):
    cnt = arr.count(2)
    start = end = 0
    answer = []

    if cnt == 0:
        answer = [-1]
    elif cnt == 1:
        answer = [2]
    else:
        for idx in range(len(arr)):
            if arr[idx]==2 and cnt==arr.count(2):
                start = idx
                cnt -= 1
            elif arr[idx]==2 and cnt==1:
                end = idx
            elif arr[idx]==2:
                cnt -= 1
        answer = arr[start:end+1]
    
    return answer

 

ํ’€์ด # 1

  • arr์—์„œ 2์˜ ๊ฐฏ์ˆ˜๋ฅผ cnt๋กœ ์„ค์ •ํ•˜๊ณ  2๊ฐ€ ์—†์œผ๋ฉด answer = [-1], 1๊ฐœ๋ฉด answer = [2]
  • arr์—์„œ 2๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ
    • 2์˜ ๊ฐฏ์ˆ˜ cnt์—์„œ 2๊ฐ€ ๋“ฑ์žฅํ•  ๋•Œ๋งˆ๋‹ค 1์”ฉ ๋นผ์„œ ๋ช‡ ๋ฒˆ์งธ ๋“ฑ์žฅ์ธ์ง€ ํ™•์ธ
    • start = ์ฒ˜์Œ 2๊ฐ€ ๋“ฑ์žฅํ•œ ์ธ๋ฑ์Šค / end = ๋งˆ์ง€๋ง‰ 2๊ฐ€ ๋“ฑ์žฅํ•œ ์ธ๋ฑ์Šค
    • answer = arr์˜ start๋ถ€ํ„ฐ end๊นŒ์ง€์˜ ์ธ๋ฑ์Šค

 

# 2
def solution(arr):
    answer = []
    idx_list = []

    for idx in range(len(arr)):
        if arr[idx] == 2:
            idx_list.append(idx)

    cnt = len(idx_list)
    if cnt == 0:
        answer = [-1]
    else:
        answer = arr[idx_list[0]:idx_list[-1]+1]

    return answer

 

ํ’€์ด #2

  • arr๋ฅผ ๋Œ๋ฉด์„œ 2๊ฐ€ ๋“ฑ์žฅํ•  ๋•Œ๋งˆ๋‹ค idx_list์— idx ์ถ”๊ฐ€
  • idx_list์˜ ๊ฐฏ์ˆ˜๋ฅผ cnt๋กœ ๋‘๊ณ  0์ด๋ฉด answer = [-1]
  • idx_list์˜ ๊ฐฏ์ˆ˜(=cnt)๊ฐ€ 0์•„ ์•„๋‹Œ ๊ฒฝ์šฐ
    • idx_list์—๋Š” 2์˜ ์ธ๋ฑ์Šค๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
    • idx_list[0] = ์ฒ˜์Œ 2๊ฐ€ ๋“ฑ์žฅํ•œ ์ธ๋ฑ์Šค / idx_list[-1]  = ๋งˆ์ง€๋ง‰ 2๊ฐ€ ๋“ฑ์žฅํ•œ ์ธ๋ฑ์Šค
    • answer = arr์˜ idx_list[0]๋ถ€ํ„ฐ idx_list[-1]๊นŒ์ง€์˜ ์ธ๋ฑ์Šค

 

# 3
def solution(arr):
    answer = []
    if 2 not in arr:
        answer = [-1]
    else:
        answer = arr[arr.index(2) : len(arr) - arr[::-1].index(2)]
    return answer

 

ํ’€์ด #3

  • arr์— 2๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด answer = [-1]
  • arr์— 2๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”๊ฒฝ์šฐ
    • arr.index(2) = ์ฒ˜์Œ์œผ๋กœ 2๊ฐ€ ๋“ฑ์žฅํ•œ ์ธ๋ฑ์Šค
    • arr[::-1].index(2) = arr๋ฅผ ๊ฑฐ๊พธ๋กœ ๋ดค์„ ๋•Œ ์ฒ˜์Œ 2๊ฐ€ ๋“ฑ์žฅํ•œ ์ธ๋ฑ์Šค = ๋งˆ์ง€๋ง‰์œผ๋กœ 2๊ฐ€ ๋“ฑ์žฅํ•œ ์ธ๋ฑ์Šค
    • arr๋ฅผ ๊ฑฐ๊พธ๋กœ ๋ดค์„ ๋•Œ์˜ ์ธ๋ฑ์Šค์ด๋ฏ€๋กœ ์ „์ฒด ๊ธธ์ด์—์„œ ๋นผ๊ธฐ...!!

 

 

์ฒซ ๋ฒˆ์งธ ํ’€์ด๋Š” ์ด๊ฒƒ ์ €๊ฒƒ ์กฐ๊ฑด์„ ๋ง๋ถ™์ด๋‹ค ๋ณด๋‹ˆ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜ ๋ฐ–์— ์—†์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‘ ๋ฒˆ์งธ ํ’€์ด๋กœ๋Š” 2๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ธ๋ฑ์Šค๋งŒ ์ถ”์ถœํ•ด์„œ ์ฒ˜์Œ๊ณผ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์˜ arr๋ฅผ ์ถœ๋ ฅํ–ˆ๋‹ค. ์ด๊ฒƒ๋„ ๊ดœ์ฐฎ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ํ‘ผ ์„ธ ๋ฒˆ์งธ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด๋‹ˆ๊นŒ ์‹ ๋ฐ•ํ•˜๊ณ  ๊น”๋”ํ–ˆ๋‹ค...๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด์„œ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค. ๋‚˜๋„ ์„ผ์Šค์Ÿ์ด๊ฐ€ ๋˜๊ณ  ์‹ถ๋‹ค...!!

 

https://school.programmers.co.kr/learn/courses/30/lessons/181934

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋‘ ์ˆ˜๊ฐ€ n๊ณผ m์ด๋ผ๋ฉด
">", "=" : n >= m
"<", "=" : n <= m
">", "!" : n > m
"<", "!" : n < m
๋‘ ๋ฌธ์ž์—ด ineq์™€ eq๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ineq๋Š” "<"์™€ ">"์ค‘ ํ•˜๋‚˜๊ณ , eq๋Š” "="์™€ "!"์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ์ •์ˆ˜ n๊ณผ m์ด ์ฃผ์–ด์งˆ ๋•Œ, n๊ณผ m์ด ineq์™€ eq์˜ ์กฐ๊ฑด์— ๋งž์œผ๋ฉด 1์„ ์•„๋‹ˆ๋ฉด 0์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ n, m ≤ 100

 

์ž…์ถœ๋ ฅ ์˜ˆ

ineq eq n m result
"<" "=" 20 50 1
">" "!" 41 78 0

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(ineq, eq, n, m):
    answer = 0      
    
    if (n>m) and (ineq==">"):
        answer = 1
    elif (n<m) and (ineq=="<"):
        answer = 1
    elif (n==m) and (eq=="="):
        answer= 1
    
    return answer

 

ํ’€์ด # 1

  • ์ˆซ์ž n,m์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•˜๊ณ  ineq์™€ ๊ฐ™์€์ง€ ํ™•์ธ
  • ๋‘ ์ˆซ์ž๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” eq๋ฅผ ๋น„๊ตํ•˜์—ฌ ํ™•์ธ

 

# 2
def solution(ineq, eq, n, m):
    answer = 0      
    
    eq = eq.replace("!","")
    answer = int(eval(str(n)+ineq+eq+str(m)))
    
    return answer

 

ํ’€์ด #2

  • eq๊ฐ€ "!"์ธ ๊ฒฝ์šฐ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
    • replace : ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜
    • ๋ณ€์ˆ˜.replace("๋ณ€๊ฒฝ ์ „ ๋ฌธ์ž์—ด", "๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ๋ฌธ์ž์—ด")
  • ์ˆซ์ž์™€ ์—ฐ์‚ฐ์ž๋ฅผ ๋‚˜์—ดํ•˜์—ฌ ์‹คํ–‰
    • eval : ๋ฌธ์ž์—ด์„ ์‹(expression)์œผ๋กœ ๋ฐ›์•„์„œ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜
    • ์‹์„ ๋ฌธ์ž์—ด๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์ˆซ์ž๋ฅผ string ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ
  • True / False๋ฅผ ์ˆซ์ž๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด int ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ

 

 

๋ฌธ์ž์—ด ๋ณ€๊ฒฝํ•˜๋Š” ๊ฑฐ ์ƒ๊ฐ๋„ ๋ชปํ–ˆ๋Š”๋ฐ ์„ธ์ƒ์— ๋˜‘๋˜‘ํ•œ ์‚ฌ๋žŒ์ด ๋งŽ๋‹ค...๐Ÿ˜ฎ

+ Recent posts