[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜์–ด ๋๋ง์ž‡๊ธฐ/ ํŒŒ์ด์ฌ / Python/ Summer/Winter Coding(~2018)/ ์นด์นด์˜ค ์ฝ”ํ…Œ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜์–ด ๋๋ง์ž‡๊ธฐ/ ํŒŒ์ด์ฌ / Python/ Summer/Winter Coding(~2018)/ ์นด์นด์˜ค ์ฝ”ํ…Œ

728x90
๋ฐ˜์‘ํ˜•

 

 

๐Ÿ’กsolutions )

๐Ÿ’ฌ ๋กœ์ง์€ ํฌ๊ฒŒ ๋๋ง์ž‡๊ธฐ๊ฐ€ ์œ ํšจํ•œ์ง€ for๋ฌธ์„ ๋Œ๋ฉฐ ํ™•์ธ -> ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋Š” ์ด 2๊ฐ€์ง€(์ด๋ฏธ ๋งํ•œ ๋‹จ์–ด๋ฅผ ๋‹ค์‹œ ๋งํ•œ ๊ฒฝ์šฐ์™€ ์•ž๋‹จ์–ด์˜ ๋งˆ์ง€๋ง‰๊ณผ ๋’ท๋‹จ์–ด์˜ ์ฒซ ๋ฒˆ์งธ๊ฐ€ ๊ฐ™์ง€ ์•Š์€ ๊ฒฝ์šฐ)

* ๋ฒˆํ˜ธ์™€ ์ฐจ๋ก€๋ฅผ ๊ตฌํ•  ๋•Œ๋Š” ์•„๋žซ์ฒ˜๋Ÿผ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ์ •ํ™•ํ•œ ๊ฐ’์ด ๋‚˜์˜ค๋„๋ก ๋ง์…ˆ ์ฒ˜๋ฆฌ

๐Ÿ’ฌ ๋ฒˆํ˜ธ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” idx๋Š” i๋ฅผ n์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€์— +1๋ฅผ ํ•ด์ฃผ๊ธฐ

๐Ÿ’ฌ ์ฐจ๋ก€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” cnt๋Š” n์˜ ๋ฐฐ์ˆ˜ ๋ฒˆ์งธ์ธ์ง€ ์•„๋‹Œ ์ง€์— ๋”ฐ๋ผ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•จ

๐ŸŽซcode )

def solution(n, words):
    tmp = [words[0]] # ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๋Š” ๋จผ์ € ๋„ฃ๊ณ  ์‹œ์ž‘

    for i in range(1, len(words)): # ๋‘ ๋ฒˆ์งธ ๋‹จ์–ด๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋๋ง์ž‡๊ธฐ ์œ ํšจํ•œ์ง€ ํ™•์ธ
        if words[i - 1][-1] != words[i][0]: # ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ 1๋ฒˆ
            idx = i % n + 1
            cnt = i + 1
            break
        else:
            if words[i] not in tmp: # ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
                tmp.append(words[i])
            else:                   # ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ 2๋ฒˆ
                idx = i % n + 1
                if idx == 0:
                    idx = n
                cnt = i + 1
                break
    else: # ์ฃผ์–ด์ง„ ๋‹จ์–ด ๋ชจ๋‘ ํƒˆ๋ฝ์ž๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
        return [0, 0]

    if cnt % n == 0:
        cnt = cnt // n
    else:
        cnt = cnt // n + 1
    answer = [idx, cnt]
    return answer

 

๐Ÿ“Œ description )

๋ฌธ์ œ์ถœ์ฒ˜ : programmers.co.kr/learn/courses/30/lessons/12981?language=python3

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์˜์–ด ๋๋ง์ž‡๊ธฐ

3 [tank, kick, know, wheel, land, dream, mother, robot, tank] [3,3] 5 [hello, observe, effect, take, either, recognize, encourage, ensure, establish, hang, gather, refer, reference, estimate, executive] [0,0]

programmers.co.kr

๋ฌธ์ œ ์„ค๋ช…

1๋ถ€ํ„ฐ n๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด์žˆ๋Š” n๋ช…์˜ ์‚ฌ๋žŒ์ด ์˜์–ด ๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด ๋๋ง์ž‡๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

  1. 1๋ฒˆ๋ถ€ํ„ฐ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์‚ฌ๋žŒ์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ๋‹จ์–ด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
  2. ๋งˆ์ง€๋ง‰ ์‚ฌ๋žŒ์ด ๋‹จ์–ด๋ฅผ ๋งํ•œ ๋‹ค์Œ์—๋Š” ๋‹ค์‹œ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  3. ์•ž์‚ฌ๋žŒ์ด ๋งํ•œ ๋‹จ์–ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด๋ฅผ ๋งํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  4. ์ด์ „์— ๋“ฑ์žฅํ–ˆ๋˜ ๋‹จ์–ด๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  5. ํ•œ ๊ธ€์ž์ธ ๋‹จ์–ด๋Š” ์ธ์ •๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ 3๋ช…์ด ๋๋ง์ž‡๊ธฐ๋ฅผ ํ•˜๋Š” ์ƒํ™ฉ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

tank → kick → know → wheel → land → dream → mother → robot → tank

์œ„ ๋๋ง์ž‡๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

  • 1๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋ก€์— tank๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • 2๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋ก€์— kick์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • 3๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋ก€์— know๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • 1๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ๋‘ ๋ฒˆ์งธ ์ฐจ๋ก€์— wheel์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • (๊ณ„์† ์ง„ํ–‰)

๋๋ง์ž‡๊ธฐ๋ฅผ ๊ณ„์† ์ง„ํ–‰ํ•ด ๋‚˜๊ฐ€๋‹ค ๋ณด๋ฉด, 3๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์„ธ ๋ฒˆ์งธ ์ฐจ๋ก€์— ๋งํ•œ tank ๋ผ๋Š” ๋‹จ์–ด๋Š” ์ด์ „์— ๋“ฑ์žฅํ–ˆ๋˜ ๋‹จ์–ด์ด๋ฏ€๋กœ ํƒˆ๋ฝํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

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

์ œํ•œ ์‚ฌํ•ญ

  • ๋๋ง์ž‡๊ธฐ์— ์ฐธ์—ฌํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜ n์€ 2 ์ด์ƒ 10 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • words๋Š” ๋๋ง์ž‡๊ธฐ์— ์‚ฌ์šฉํ•œ ๋‹จ์–ด๋“ค์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด์ด๋ฉฐ, ๊ธธ์ด๋Š” n ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 50 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋๋ง์ž‡๊ธฐ์— ์‚ฌ์šฉ๋˜๋Š” ๋‹จ์–ด์˜ ๋œป(์˜๋ฏธ)์€ ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค.
  • ์ •๋‹ต์€ [ ๋ฒˆํ˜ธ, ์ฐจ๋ก€ ] ํ˜•ํƒœ๋กœ return ํ•ด์ฃผ์„ธ์š”.
  • ๋งŒ์•ฝ ์ฃผ์–ด์ง„ ๋‹จ์–ด๋“ค๋กœ ํƒˆ๋ฝ์ž๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค๋ฉด, [0, 0]์„ return ํ•ด์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ


์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
3๋ช…์˜ ์‚ฌ๋žŒ์ด ๋๋ง์ž‡๊ธฐ์— ์ฐธ์—ฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • 1๋ฒˆ ์‚ฌ๋žŒ : tank, wheel, mother
  • 2๋ฒˆ ์‚ฌ๋žŒ : kick, land, robot
  • 3๋ฒˆ ์‚ฌ๋žŒ : know, dream, tank

์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋ง์„ ํ•˜๊ฒŒ ๋˜๋ฉฐ, 3๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์„ธ ๋ฒˆ์งธ ์ฐจ๋ก€์— ๋งํ•œ tank๋ผ๋Š” ๋‹จ์–ด๊ฐ€ 1๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋ก€์— ๋งํ•œ tank์™€ ๊ฐ™์œผ๋ฏ€๋กœ 3๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์„ธ ๋ฒˆ์งธ ์ฐจ๋ก€๋กœ ๋ง์„ ํ•  ๋•Œ ์ฒ˜์Œ ํƒˆ๋ฝ์ž๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
5๋ช…์˜ ์‚ฌ๋žŒ์ด ๋๋ง์ž‡๊ธฐ์— ์ฐธ์—ฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • 1๋ฒˆ ์‚ฌ๋žŒ : hello, recognize, gather
  • 2๋ฒˆ ์‚ฌ๋žŒ : observe, encourage, refer
  • 3๋ฒˆ ์‚ฌ๋žŒ : effect, ensure, reference
  • 4๋ฒˆ ์‚ฌ๋žŒ : take, establish, estimate
  • 5๋ฒˆ ์‚ฌ๋žŒ : either, hang, executive

์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋ง์„ ํ•˜๊ฒŒ ๋˜๋ฉฐ, ์ด ๊ฒฝ์šฐ๋Š” ์ฃผ์–ด์ง„ ๋‹จ์–ด๋กœ๋งŒ์œผ๋กœ๋Š” ํƒˆ๋ฝ์ž๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [0, 0]์„ returnํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3
2๋ช…์˜ ์‚ฌ๋žŒ์ด ๋๋ง์ž‡๊ธฐ์— ์ฐธ์—ฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • 1๋ฒˆ ์‚ฌ๋žŒ : hello, even, now, draw
  • 2๋ฒˆ ์‚ฌ๋žŒ : one, never, world

์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋ง์„ ํ•˜๊ฒŒ ๋˜๋ฉฐ, 1๋ฒˆ ์‚ฌ๋žŒ์ด ์ž์‹ ์˜ ์„ธ ๋ฒˆ์งธ ์ฐจ๋ก€์— 'r'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด ๋Œ€์‹ , n์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” now๋ฅผ ๋งํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋•Œ ์ฒ˜์Œ ํƒˆ๋ฝ์ž๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•