๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ˜ผ์ž ๋†€๊ธฐ์˜ ๋‹ฌ์ธ

๐ŸŽฎinspirer9 2023. 2. 20. 02:32
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

ํ˜ผ์ž์„œ๋„ ์ž˜ ๋…ธ๋Š” ๋ฒ”ํฌ๋Š” ์–ด๋Š ๋‚  ๋ฐฉ๊ตฌ์„์— ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ ๋”๋ฏธ๋ฅผ ๋ณด๋”๋‹ˆ ํ˜ผ์ž ํ•  ์ˆ˜ ์žˆ๋Š” ์žฌ๋ฏธ์žˆ๋Š” ๊ฒŒ์ž„์„ ์ƒ๊ฐํ•ด๋ƒˆ์Šต๋‹ˆ๋‹ค.

์ˆซ์ž ์นด๋“œ ๋”๋ฏธ์—๋Š” ์นด๋“œ๊ฐ€ ์ด 100์žฅ ์žˆ์œผ๋ฉฐ, ๊ฐ ์นด๋“œ์—๋Š” 1๋ถ€ํ„ฐ 100๊นŒ์ง€ ์ˆซ์ž๊ฐ€ ํ•˜๋‚˜์”ฉ ์ ํ˜€์žˆ์Šต๋‹ˆ๋‹ค. 2 ์ด์ƒ 100 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ •ํ•ด ๊ทธ ์ˆ˜๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆซ์ž ์นด๋“œ๋“ค์„ ์ค€๋น„ํ•˜๊ณ , ์ค€๋น„ํ•œ ์นด๋“œ์˜ ์ˆ˜๋งŒํผ ์ž‘์€ ์ƒ์ž๋ฅผ ์ค€๋น„ํ•˜๋ฉด ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒŒ์ž„ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ค€๋น„๋œ ์ƒ์ž์— ์นด๋“œ๋ฅผ ํ•œ ์žฅ์”ฉ ๋„ฃ๊ณ , ์ƒ์ž๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์„ž์–ด ์ผ๋ ฌ๋กœ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. ์ƒ์ž๊ฐ€ ์ผ๋ ฌ๋กœ ๋‚˜์—ด๋˜๋ฉด ์ƒ์ž๊ฐ€ ๋‚˜์—ด๋œ ์ˆœ์„œ์— ๋”ฐ๋ผ 1๋ฒˆ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค.

๊ทธ ๋‹ค์Œ ์ž„์˜์˜ ์ƒ์ž๋ฅผ ํ•˜๋‚˜ ์„ ํƒํ•˜์—ฌ ์„ ํƒํ•œ ์ƒ์ž ์•ˆ์˜ ์ˆซ์ž ์นด๋“œ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์œผ๋กœ ํ™•์ธํ•œ ์นด๋“œ์— ์ ํžŒ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ์ƒ์ž๋ฅผ ์—ด์–ด ์•ˆ์— ๋‹ด๊ธด ์นด๋“œ์— ์ ํžŒ ์ˆซ์ž๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง„ ์ƒ์ž๋ฅผ ๊ณ„์†ํ•ด์„œ ์—ด์–ด๊ฐ€๋ฉฐ, ์—ด์–ด์•ผ ํ•˜๋Š” ์ƒ์ž๊ฐ€ ์ด๋ฏธ ์—ด๋ ค์žˆ์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์—ฐ ์ƒ์ž๋“ค์€ 1๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน์ž…๋‹ˆ๋‹ค. ์ด์ œ 1๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน์„ ๋‹ค๋ฅธ ์ƒ์ž๋“ค๊ณผ ์„ž์ด์ง€ ์•Š๋„๋ก ๋”ฐ๋กœ ๋‘ก๋‹ˆ๋‹ค. ๋งŒ์•ฝ 1๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน์„ ์ œ์™ธํ•˜๊ณ  ๋‚จ๋Š” ์ƒ์ž๊ฐ€ ์—†์œผ๋ฉด ๊ทธ๋Œ€๋กœ ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜๋ฉฐ, ์ด๋•Œ ํš๋“ํ•˜๋Š” ์ ์ˆ˜๋Š” 0์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋‚จ์€ ์ƒ์ž ์ค‘ ๋‹ค์‹œ ์ž„์˜์˜ ์ƒ์ž ํ•˜๋‚˜๋ฅผ ๊ณจ๋ผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ด๋ฏธ ์—ด๋ ค์žˆ๋Š” ์ƒ์ž๋ฅผ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ์ƒ์ž๋ฅผ ์—ฝ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์—ฐ ์ƒ์ž๋“ค์€ 2๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน์ž…๋‹ˆ๋‹ค.

1๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน์— ์†ํ•œ ์ƒ์ž์˜ ์ˆ˜์™€ 2๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน์— ์†ํ•œ ์ƒ์ž์˜ ์ˆ˜๋ฅผ ๊ณฑํ•œ ๊ฐ’์ด ๊ฒŒ์ž„์˜ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ƒ์ž ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์นด๋“œ ๋ฒˆํ˜ธ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด cards๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋ฒ”ํฌ๊ฐ€ ์ด ๊ฒŒ์ž„์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 2  cards์˜ ๊ธธ์ด ≤ 100
  • cards์˜ ์›์†Œ๋Š” cards์˜ ๊ธธ์ด ์ดํ•˜์ธ ์ž„์˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • cards์—๋Š” ์ค‘๋ณต๋˜๋Š” ์›์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • cards[i]๋Š” i + 1๋ฒˆ ์ƒ์ž์— ๋‹ด๊ธด ์นด๋“œ์— ์ ํžŒ ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

cards result
[8,6,3,7,2,5,1,4] 12

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

1, 4, 7, 8์ด ์†ํ•˜๋Š” ์ƒ์ž์˜ ๊ทธ๋ฃน๊ณผ 2, 5, 6์ด ์†ํ•˜๋Š” ์ƒ์ž์˜ ๊ทธ๋ฃน๊ณผ 3์ด ์†ํ•˜๋Š” ์ƒ์ž์˜ ๊ทธ๋ฃน์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 3๋ฒˆ ์ƒ์ž๋ฅผ ๊ณ ๋ฅด์ง€ ์•Š์•˜์„ ๋•Œ, ๋‘ ๋ฒˆ์˜ ์‹œํ–‰์—์„œ 3๊ณผ 4๋ฅผ ๊ธฐ๋กํ•˜๋ฉฐ ์ตœ๊ณ  ์ ์ˆ˜ 12๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ’€์ด

  • ์—ญ๋Œ€๊ธ‰ ์„ค๋ช…์ด ์ด์ƒํ•œ ๋ฌธ์ œ ์ค‘ ํ•˜๋‚˜... ใ…ก.ใ…ก?
  • ์„ค๋ช…๋Œ€๋กœ ์ตœ์†Œ ์ผ€์ด์Šค๋กœ ํ•ด๋ณด์ž.
    • 2์นด๋“œ๋ฅผ ๋ฝ‘์•˜๋‹ค.
    • ๊ทธ๋Ÿผ ์นด๋“œ๋Š” [1, 2], ์ƒ์ž๋Š” 2๊ฐœ
    • ์„ž์–ด๋ดค์ž [1, 2], [2,1]
    • ๋ฒˆํ˜ธ๋Š” 1, 2๋ถ™์ด๋ฉด ๋˜๊ณ ...
    • ๊ทธ ๋‹ค์Œ ์ž„์˜์˜ ์ƒ์ž๋ฅผ ํ•˜๋‚˜ ์„ ํƒํ•˜๋ฉด [1]์•„๋‹ˆ๋ฉด [2]
      • ๊ทธ๋ฆฌ๊ณ  [1]์ด๋ฉด [1]๋ฒˆ ์ƒ์ž๋ฅผ ๋˜ ์—ด์–ด์•ผ ๋˜๋Š”๋ฐ ๋ถˆ๊ฐ€๋Šฅ
      • [2]๋„ ๋งˆ์ฐฌ๊ฐ€์ง€. ๋.
    • ๊ทธ๋Ÿผ [1]๋ฒˆ ์—ด์—ˆ๋˜ ๊ทธ๋ฃน์ด 1๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน.
    • [2]๋Š” 2๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน.
    • ์ด๋ ‡๊ฒŒ ์„ž์ด์ง€ ์•Š๋„๋ก ๋”ฐ๋กœ ๋‘”๋‹ค.
      • ๋‚จ์€ ์ƒ์ž๊ฐ€ ์—†์œผ๋ฉด ๊ฒŒ์ž„ ์ข…๋ฃŒ.
      • ๊ทผ๋ฐ 1๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน๋งŒ ์žˆ์œผ๋ฉด ํš๋“ ์ ์ˆ˜๋Š” 0์ .
    • ๊ทธ๋ž˜์„œ 1๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน์— ์†ํ•œ ์ƒ์ž ์ˆ˜์™€ 2๋ฒˆ ์ƒ์ž ๊ทธ๋ฃน์— ์†์ƒ ์ƒ์ž์˜ ์ˆ˜๋ฅผ ๊ณฑํ•œ ๊ฐ’์ด ๊ฒŒ์ž„์˜ ์ ์ˆ˜.
      • ์ด๋Ÿฐ ๊ฒŒ์ž„์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๊ตฌํ•˜์‹œ์˜ค.
    • ๊ทผ๋ฐ ์ƒ์ž ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์นด๋“œ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด์ด cards.
      • ์นด๋“œ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ๊ฐ€ ์˜๋ฏธ๊ฐ€ ์žˆ๋‚˜? ๋ฌด์ž‘์œ„๋กœ ์„ž์–ด์„œ ๋‚˜์—ดํ•˜๋ผ๋ฉฐ?
      • ์•„... ๋ฌด์ž‘์œ„๋กœ ์„ž์—ฌ์„œ ๋‚˜์—ด๋œ ์ˆœ์„œ๊ฐ€ cards์ธ๊ฐ€?
    • ๊ทผ๋ฐ ๊ทธ ๋‹ค์Œ์— ๋˜ ์ž„์ด์˜ ์ƒ์ž๋ฅผ ์„ ํƒํ•˜๋ผ๋ฉฐ?
  • ๋ญ์ง€?? (ใ…ก.ใ…ก) ๊ตฌ๋ƒฅ ๋‹ค์‹œ ํ•ด๋ณด์ž...
    • ์ž…์ถœ๋ ฅ ์˜ˆ์—์„œ ๋งํ•˜๋Š” [8,6,3,7,2,5,1,4]๊ฐ€, ์ƒ์ž ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์นด๋“œ์˜ ์ˆœ์„œ์ด๊ณ .
      • [8],[6],[3],[7],[2],[5],[1],[4] ์ด๋ ‡๊ฒŒ 8๊ฐœ์˜ ์ƒ์ž๊ฐ€ ์žˆ๊ณ ,
      • ์ด์ค‘์— ํ•˜๋‚˜๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์„ ํƒํ•˜๋ฉด
        • 8 -> 4 -> 7 -> 1 -> 8
        • 6 -> 5 -> 2 -> 6 -> 5 ...
        • 3 -> 3...
        • 7 -> 1 -> 8 ...
        • 2 -> 6 -> 5...
        • 5 -> 2 -> 6...
        • 1 -> 8...
        • 4 -> 7...
      • ์•„.........  visted ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•˜๋Š”๊ตฌ๋‚˜?
    • ์•Œ์•˜๋‹ค.
  • ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ ๊ฐ€์ฆˆ์•„!
import collections

def solution(cards):
    len_cards = len(cards)
    unvisited = [True] * len_cards
    scores = collections.Counter()
    for i in range(len_cards):
        if unvisited[i] == True:
            scores[i] = 0
            pos = i
            while True:
                unvisited[pos] = False
                pos = cards[pos] -1
                scores[i] += 1
                print(unvisited, scores, cards, pos)
                if unvisited[pos] == False:
                    break
    answer = scores.most_common(2)
    answer = answer[0][1] * answer[1][1]
    return answer
    
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	[8, 6, 3, 7, 2, 5, 1, 4]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	12
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	[False, True, True, True, True, True, True, True] Counter({0: 1}) [8, 6, 3, 7, 2, 5, 1, 4] 7
[False, True, True, True, True, True, True, False] Counter({0: 2}) [8, 6, 3, 7, 2, 5, 1, 4] 3
[False, True, True, False, True, True, True, False] Counter({0: 3}) [8, 6, 3, 7, 2, 5, 1, 4] 6
[False, True, True, False, True, True, False, False] Counter({0: 4}) [8, 6, 3, 7, 2, 5, 1, 4] 0
[False, False, True, False, True, True, False, False] Counter({0: 4, 1: 1}) [8, 6, 3, 7, 2, 5, 1, 4] 5
[False, False, True, False, True, False, False, False] Counter({0: 4, 1: 2}) [8, 6, 3, 7, 2, 5, 1, 4] 4
[False, False, True, False, False, False, False, False] Counter({0: 4, 1: 3}) [8, 6, 3, 7, 2, 5, 1, 4] 1
[False, False, False, False, False, False, False, False] Counter({0: 4, 1: 3, 2: 1}) [8, 6, 3, 7, 2, 5, 1, 4] 2
  • ์ œ์ถœ!
    • ์™“? ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 2 ์‹คํŒจ...
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
  • ์•„.... ์•„๊นŒ ๋ฌธ์ œ ์ฝ์„ ๋•Œ ์ฒดํฌํ•ด๋†“๊ณ  ์ฝ”๋”ฉ์„ ์•ˆํ–ˆ๋‹ค.
  • ์ƒ์ž๊ทธ๋ฃน์ด ํ•˜๋‚˜์ผ ๋•Œ๋Š” 0์ด๋‹ค.
import collections

def solution(cards):
    len_cards = len(cards)
    unvisited = [True] * len_cards
    scores = collections.Counter()
    for i in range(len_cards):
        if unvisited[i] == True:
            scores[i] = 0
            pos = i
            while True:
                unvisited[pos] = False
                pos = cards[pos] -1
                scores[i] += 1
                if unvisited[pos] == False:
                    break
    answer = scores.most_common(2)
    if len(answer) == 1:
        return 0
    else:
        return (answer[0][1] * answer[1][1])
        
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด.
    • ๊ทธ๋ƒฅ ๋ฆฌ์ŠคํŠธ๋กœ ํ’€์–ด๋„ ๋˜๋Š” ๊ฑฐ์˜€๋‚˜?
    • ์†๋„๊ฐ€ ๋น ๋ฅด์ง€ ์•Š๋‹ค.
def solution(cards):
    answer = []
    for i in range(len(cards)):
        tmp = []
        while cards[i] not in tmp:
            tmp.append(cards[i])
            i = cards[i] - 1
        answer.append([] if sorted(tmp) in answer else sorted(tmp))
    answer.sort(key=len)

    return len(answer[-1]) * len(answer[-2])
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (7.19ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (2.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.61ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.23ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.3MB)
  • ๋””ํ๋ฅผ ์“ด๊ฒŒ ๋น ๋ฅด๋„ค.
    • ์ •๋ ฌ์„ ํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ ...
    • ๋”•์…”๋„ˆ๋ฆฌ ๋ณด๋‹จ ๋””ํ๊ฐ€ ๋” ๋น ๋ฅธ...
import heapq
def solution(cards):
    answer = 1
    alist = []
    visit = [0]*len(cards)
    for i in range(len(cards)):
        cnt = 0
        a = cards[i]-1
        while True:
            if visit[a] == 1:
                break
            else:
                visit[a] = 1
                a = cards[a]-1
                cnt += 1
        heapq.heappush(alist,-cnt)

    a = heapq.heappop(alist)
    b = heapq.heappop(alist)
    answer = a*b

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
  • ํ˜น์‹œ๋‚˜ ํ•ด์„œ while sum( ) > 0: ์œผ๋กœ ๋ฐ”๊พธ์–ด๋ณด์•˜๋Š”๋ฐ ๋ณ„๋กœ?
import collections

def solution(cards):
    len_cards = len(cards)
    unvisited = [True] * len_cards
    scores = collections.Counter()
    while sum(unvisited) > 0:
        i = unvisited.index(True)
        scores[i] = 0
        pos = i
        while True:
            unvisited[pos] = False
            pos = cards[pos] -1
            scores[i] += 1
            if unvisited[pos] == False:
                break
    answer = scores.most_common(2)
    if len(answer) == 1:
        return 0
    else:
        return (answer[0][1] * answer[1][1])
        
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 9.99MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.3MB)
728x90
๋ฐ˜์‘ํ˜•