๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/Programmers

[Programmers] ๋ชจ์˜๊ณ ์‚ฌ

 

 

๋ฌธ์ œ 

 

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค.

1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

 

์ œํ•œ ์‚ฌํ•ญ
  • ์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

 

 

์ฝ”๋“œ 
def solution(answers):
    one = [1,2,3,4,5]
    two = [2,1,2,3,2,4,2,5]
    three = [3,3,1,1,2,2,4,4,5,5]
    answer = []
    cnt,knt,pnt = 0,0,0
    for i in range(len(answers)):
        s1 = i % 5
        s2 = i % 8
        s3 = i % 10
        if answers[i] == one[s1]:
            cnt += 1
        if answers[i] == two[s2]:
            knt += 1
        if answers[i] == three[s3]:
            pnt += 1
    k = max(cnt,knt,pnt)
    
    if k == cnt:
        answer.append(1)
    if k == knt:
        answer.append(2)
    if k == pnt:
        answer.append(3)
    return answer

 

 

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ชจ์˜๊ณ ์‚ฌ

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค. 1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š”

programmers.co.kr