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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์นด๋“œ ๋ญ‰์น˜

๐ŸŽฎinspirer9 2023. 2. 16. 17:03
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์ฝ”๋‹ˆ๋Š” ์˜์–ด ๋‹จ์–ด๊ฐ€ ์ ํžŒ ์นด๋“œ ๋ญ‰์น˜ ๋‘ ๊ฐœ๋ฅผ ์„ ๋ฌผ๋กœ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์ฝ”๋‹ˆ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ์นด๋“œ์— ์ ํžŒ ๋‹จ์–ด๋“ค์„ ์‚ฌ์šฉํ•ด ์›ํ•˜๋Š” ์ˆœ์„œ์˜ ๋‹จ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

  • ์›ํ•˜๋Š” ์นด๋“œ ๋ญ‰์น˜์—์„œ ์นด๋“œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์žฅ์”ฉ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ํ•œ ๋ฒˆ ์‚ฌ์šฉํ•œ ์นด๋“œ๋Š” ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ์นด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ์นด๋“œ๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ๊ธฐ์กด์— ์ฃผ์–ด์ง„ ์นด๋“œ ๋ญ‰์น˜์˜ ๋‹จ์–ด ์ˆœ์„œ๋Š” ๋ฐ”๊ฟ€ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ฒซ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์— ์ˆœ์„œ๋Œ€๋กœ ["i", "drink", "water"], ๋‘ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์— ์ˆœ์„œ๋Œ€๋กœ ["want", "to"]๊ฐ€ ์ ํ˜€์žˆ์„ ๋•Œ ["i", "want", "to", "drink", "water"] ์ˆœ์„œ์˜ ๋‹จ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์—์„œ "i"๋ฅผ ์‚ฌ์šฉํ•œ ํ›„ ๋‘ ๋ฒˆ์งธ ์นด๋“œ ๋ญ‰์น˜์—์„œ "want"์™€ "to"๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์ฒซ ๋ฒˆ์งธ ์นด๋“œ๋ญ‰์น˜์— "drink"์™€ "water"๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ์ˆœ์„œ์˜ ๋‹จ์–ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด cards1, cards2์™€ ์›ํ•˜๋Š” ๋‹จ์–ด ๋ฐฐ์—ด goal์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, cards1๊ณผ cards2์— ์ ํžŒ ๋‹จ์–ด๋“ค๋กœ goal๋ฅผ ๋งŒ๋“ค ์žˆ๋‹ค๋ฉด "Yes"๋ฅผ, ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค๋ฉด "No"๋ฅผ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 1 ≤ cards1์˜ ๊ธธ์ด, cards2์˜ ๊ธธ์ด ≤ 10
    • 1 ≤ cards1[i]์˜ ๊ธธ์ด, cards2[i]์˜ ๊ธธ์ด ≤ 10
    • cards1๊ณผ cards2์—๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋‹จ์–ด๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • 2 ≤ goal์˜ ๊ธธ์ด ≤ cards1์˜ ๊ธธ์ด + cards2์˜ ๊ธธ์ด
    • 1 ≤ goal[i]์˜ ๊ธธ์ด ≤ 10
    • goal์˜ ์›์†Œ๋Š” cards1๊ณผ cards2์˜ ์›์†Œ๋“ค๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • cards1, cards2, goal์˜ ๋ฌธ์ž์—ด๋“ค์€ ๋ชจ๋‘ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

cards1 cards2 goal result
["i", "drink", "water"] ["want", "to"] ["i", "want", "to", "drink", "water"] "Yes"
["i", "water", "drink"] ["want", "to"] ["i", "want", "to", "drink", "water"] "No"

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

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ณธ๋ฌธ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • cards1์—์„œ "i"๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  cards2์—์„œ "want"์™€ "to"๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "i want to"๊นŒ์ง€๋Š” ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ "water"๊ฐ€ "drink"๋ณด๋‹ค ๋จผ์ € ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ฌธ์žฅ์„ ์™„์„ฑ์‹œํ‚ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ "No"๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด

๋ ˆ๋ฒจ 1 ๊ตฌํ˜„ ๋ฌธ์ œ.

  • ์ƒˆ๋กœ ์˜ฌ๋ผ์˜จ ๋ฌธ์ œ์ธ๋ฐ ๋ ˆ๋ฒจ 1์ด๋ผ ์‰ฝ๊ตฌ๋‚˜.
def solution(cards1, cards2, goal):
    len_cards1, len_cards2 = len(cards1), len(cards2)
    index1, index2 = 0, 0
    for s in goal:
        if index1 < len_cards1 and s == cards1[index1]:
            index1 += 1
        elif index2 < len_cards2 and s == cards2[index2]:
            index2 += 1
        else:
            return 'No'
    return 'Yes'
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.00ms, 9.97MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.00ms, 9.97MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.4MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.00ms, 10MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.3MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.00ms, 10MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)

๋””ํ๋กœ ํ‘ธ์‹  ๋ถ„์ด ์žˆ๋„ค... ๋‚˜๋„ ์ฒ˜์Œ์— ๋””ํ๋กœ ํ’€๊นŒ ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ...

from collections import deque

def solution(cards1, cards2, goal):
    card1_q = deque(list(cards1))
    card2_q = deque(list(cards2))

    for target in goal:
        if card1_q and card1_q[0] == target:
            card1_q.popleft()
        elif card2_q and card2_q[0] == target:
            card2_q.popleft()
        else:
            return "No"

    return "Yes"
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.00ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.01ms, 9.93MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.01ms, 9.96MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.01ms, 9.97MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.01ms, 10MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.01ms, 9.93MB)

 

728x90
๋ฐ˜์‘ํ˜•