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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ• ์ธ ํ–‰์‚ฌ

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

๋ฌธ์ œ ์„ค๋ช…

XYZ ๋งˆํŠธ๋Š” ์ผ์ •ํ•œ ๊ธˆ์•ก์„ ์ง€๋ถˆํ•˜๋ฉด 10์ผ ๋™์•ˆ ํšŒ์› ์ž๊ฒฉ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. XYZ ๋งˆํŠธ์—์„œ๋Š” ํšŒ์›์„ ๋Œ€์ƒ์œผ๋กœ ๋งค์ผ ํ•œ ๊ฐ€์ง€ ์ œํ’ˆ์„ ํ• ์ธํ•˜๋Š” ํ–‰์‚ฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ํ• ์ธํ•˜๋Š” ์ œํ’ˆ์€ ํ•˜๋ฃจ์— ํ•˜๋‚˜์”ฉ๋งŒ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•Œ๋œฐํ•œ ์ •ํ˜„์ด๋Š” ์ž์‹ ์ด ์›ํ•˜๋Š” ์ œํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰์ด ํ• ์ธํ•˜๋Š” ๋‚ ์งœ์™€ 10์ผ ์—ฐ์†์œผ๋กœ ์ผ์น˜ํ•  ๊ฒฝ์šฐ์— ๋งž์ถฐ์„œ ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ •ํ˜„์ด๊ฐ€ ์›ํ•˜๋Š” ์ œํ’ˆ์ด ๋ฐ”๋‚˜๋‚˜ 3๊ฐœ, ์‚ฌ๊ณผ 2๊ฐœ, ์Œ€ 2๊ฐœ, ๋ผ์ง€๊ณ ๊ธฐ 2๊ฐœ, ๋ƒ„๋น„ 1๊ฐœ์ด๋ฉฐ, XYZ ๋งˆํŠธ์—์„œ 15์ผ๊ฐ„ ํšŒ์›์„ ๋Œ€์ƒ์œผ๋กœ ํ• ์ธํ•˜๋Š” ์ œํ’ˆ์ด ๋‚ ์งœ ์ˆœ์„œ๋Œ€๋กœ ์น˜ํ‚จ, ์‚ฌ๊ณผ, ์‚ฌ๊ณผ, ๋ฐ”๋‚˜๋‚˜, ์Œ€, ์‚ฌ๊ณผ, ๋ผ์ง€๊ณ ๊ธฐ, ๋ฐ”๋‚˜๋‚˜, ๋ผ์ง€๊ณ ๊ธฐ, ์Œ€, ๋ƒ„๋น„, ๋ฐ”๋‚˜๋‚˜, ์‚ฌ๊ณผ, ๋ฐ”๋‚˜๋‚˜์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค. ์ฒซ์งธ ๋‚ ๋ถ€ํ„ฐ ์—ดํ˜ ๊ฐ„์—๋Š” ๋ƒ„๋น„๊ฐ€ ํ• ์ธํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ์งธ ๋‚ ์—๋Š” ํšŒ์›๊ฐ€์ž…์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‘˜์งธ ๋‚ ๋ถ€ํ„ฐ ์—ดํ˜ ๊ฐ„์—๋Š” ๋ฐ”๋‚˜๋‚˜๋ฅผ ์›ํ•˜๋Š” ๋งŒํผ ํ• ์ธ๊ตฌ๋งคํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋‘˜์งธ ๋‚ ์—๋„ ํšŒ์›๊ฐ€์ž…์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์…‹์งธ ๋‚ , ๋„ท์งธ ๋‚ , ๋‹ค์„ฏ์งธ ๋‚ ๋ถ€ํ„ฐ ๊ฐ๊ฐ ์—ดํ˜์€ ์›ํ•˜๋Š” ์ œํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰์ด ์ผ์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์…‹ ์ค‘ ํ•˜๋ฃจ์— ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์ •ํ˜„์ด๊ฐ€ ์›ํ•˜๋Š” ์ œํ’ˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด want์™€ ์ •ํ˜„์ด๊ฐ€ ์›ํ•˜๋Š” ์ œํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด number, XYZ ๋งˆํŠธ์—์„œ ํ• ์ธํ•˜๋Š” ์ œํ’ˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด discount๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ํšŒ์›๋“ฑ๋ก์‹œ ์ •ํ˜„์ด๊ฐ€ ์›ํ•˜๋Š” ์ œํ’ˆ์„ ๋ชจ๋‘ ํ• ์ธ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํšŒ์›๋“ฑ๋ก ๋‚ ์งœ์˜ ์ด ์ผ์ˆ˜๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์‹œ์˜ค. ๊ฐ€๋Šฅํ•œ ๋‚ ์ด ์—†์œผ๋ฉด 0์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ want์˜ ๊ธธ์ด = number์˜ ๊ธธ์ด ≤ 10
    • 1 ≤ number์˜ ์›์†Œ ≤ 10
    • number[i]๋Š” want[i]์˜ ์ˆ˜๋Ÿ‰์„ ์˜๋ฏธํ•˜๋ฉฐ, number์˜ ์›์†Œ์˜ ํ•ฉ์€ 10์ž…๋‹ˆ๋‹ค.
  • 10 ≤ discount์˜ ๊ธธ์ด ≤ 100,000
  • want์™€ discount์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • 1 ≤ want์˜ ์›์†Œ์˜ ๊ธธ์ด, discount์˜ ์›์†Œ์˜ ๊ธธ์ด ≤ 12

์ž…์ถœ๋ ฅ ์˜ˆ

want number discount result
["banana", "apple", "rice", "pork", "pot"] [3, 2, 2, 2, 1] ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"] 3
["apple"] [10] ["banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"] 0

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

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

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

  • ์‚ฌ๊ณผ๊ฐ€ ํ• ์ธํ•˜๋Š” ๋‚ ์ด ์—†์œผ๋ฏ€๋กœ 0์„ return ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด

  • ๋ ˆ๋ฒจ 2 ๋ฌธ์ œ๋“ค์€ ๋ฌด์ง€์„ฑ์œผ๋กœ ํ’€์–ด๋„ ๋ฌธ์ œ ์—†๋‹ค.
  • ์šฐ์„  ๋ฌผํ’ˆ์ด ์žˆ๋‚˜ ์—†๋‚˜ ์ฒดํฌํ•ด์„œ ์—†์œผ๋ฉด 0 ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ ๋ถ€ํ„ฐ ๋งŒ๋“ค์ž.
  • set()์œผ๋กœ ๋น„๊ตํ•˜๋ฉด ๋˜๊ฒ ์ง€...
def solution(want, number, discount):
    answer = 0
    print(set(want), set(discount))
    return answer
    
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	["banana", "apple", "rice", "pork", "pot"], [3, 2, 2, 2, 1], ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	3
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ 0์ด ๊ธฐ๋Œ“๊ฐ’ 3๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	{'apple', 'rice', 'pork', 'pot', 'banana'} {'apple', 'rice', 'pork', 'pot', 'chicken', 'banana'}

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	["apple"], [10], ["banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	{'apple'} {'banana'}
  • ํŒŒ์ด์ฌ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ด ์žˆ์—ˆ๋Š”๋ฐ...
    • ๋‹ค์‹œ ๊ณต๋ถ€ ใ…‹
a = {1,2,3}
b = {3,4,5}

c = set.union(a, b)
print(a,b,"|",c) # ํ•ฉ์ง‘ํ•ฉ
d = set.intersection(a, b)
print(a,b,"&",d) # ๊ต์ง‘ํ•ฉ
e = set.difference(a, b)
print(a,b,"-",e) # ์ฐจ์ง‘ํ•ฉ
f = set.symmetric_difference(a, b)
print(a,b,"^",f) # ๋Œ€์นญ์ฐจ์ง‘ํ•ฉ (XOR)
g = set.issubset(a, b)
print(a,b,"<",g) # ๋ถ€๋ถ„์ง‘ํ•ฉ
h = set.issuperset(a, b)
print(a,b,"<=",g) # ์ง„๋ถ€๋ถ„์ง‘ํ•ฉ
i = a > b
print(a,b,">",i) # ์ƒ์œ„์ง‘ํ•ฉ
i = a > b
print(a,b,">=",i) # ์ง„์ƒ์œ„์ง‘ํ•ฉ
    
์ถœ๋ ฅ
{1, 2, 3} {3, 4, 5} | {1, 2, 3, 4, 5}
{1, 2, 3} {3, 4, 5} & {3}
{1, 2, 3} {3, 4, 5} - {1, 2}
{1, 2, 3} {3, 4, 5} ^ {1, 2, 4, 5}
{1, 2, 3} {3, 4, 5} < False
{1, 2, 3} {3, 4, 5} <= False
{1, 2, 3} {3, 4, 5} > False
  • a,b์˜ ์ฐจ์ง‘ํ•ฉ์ด { }๋ฉด ํ• ์ธํ’ˆ๋ชฉ์— ์›ํŠธ๊ฐ€ ์—†๋Š” ๊ฑฐ๋‹ˆ๊นŒ 0 ๋ฆฌํ„ดํ•˜๋ฉด ๋จ.
  • ๋‹ค๋ฅผ ๋•Œ๋Š” ํ•˜๋‚˜์”ฉ ํ•ด๋ด์•ผ์ง€...
def solution(want, number, discount):
    answer = 0
    set_want, set_discout = set(want), set(discount)
    set_want -= set_discout
    if len(set_want) == 0:
        return 0
    return answer
    
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	["banana", "apple", "rice", "pork", "pot"], [3, 2, 2, 2, 1], ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	3
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ 0์ด ๊ธฐ๋Œ“๊ฐ’ 3๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	["apple"], [10], ["banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • discount ๋ฐฐ์—ด์—์„œ ์ธ๋ฑ์Šค ์ด๋™ํ•˜๋ฉด์„œ, 10๊ฐœ์”ฉ ์นด์šดํ„ฐ ๋งŒ๋“ค์–ด์„œ ์ˆซ์ž ์„ธ๊ณ , ์…€ ํ•„์š”์—†์ด set์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋น„๊ตํ•˜๋ฉด?
    • ์•„๋‹ˆ๊ตฌ๋‚˜ ์‚ฌ์•ผ๋˜๋Š” ๊ฐฏ์ˆ˜๊ฐ€ ์žˆ๊ตฌ๋‚˜.
    • ์นด์šดํ„ฐ๋„ ์ง‘ํ•ฉ ์—ฐ์‚ฐ๋˜๋‚˜?
      • ๋˜๋„ค? ใ…Ž.ใ…Ž
from collections import Counter

def solution(want, number, discount):
    answer = 0
    Counter_want = Counter(want)
    Counter_discout = Counter(discount)
    Counter_want_minus_discount = Counter_want - Counter_discout
    if len(Counter_want_minus_discount) == 0:
        return 0
    return answer
    

ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	["banana", "apple", "rice", "pork", "pot"], [3, 2, 2, 2, 1], ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	3
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ 0์ด ๊ธฐ๋Œ“๊ฐ’ 3๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	["apple"], [10], ["banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿผ ๋‚˜๋จธ์ง€๋„ ์ž‘์„ฑํ•ด๋ณผ๊นŒ?
  • ์ž˜๋˜๋Š” ๋“ฏ... ์ข€ ๋‹ค๋“ฌ์–ด์•ผ ๋˜๋Š”๋ฐ ๊ท€์ฐฎ๋‹ค.
    • for๋ฌธ์—์„œ ์ธ๋ฑ์Šค ์˜ฎ๊ฒจ๋‹ค๋‹ˆ๋ฉฐ, 10๊ฐœ ์งœ๋ฆฌ ๋ฐฐ์—ด์˜ ์•ž์— ๊ฑด ๋นผ๊ณ , ๋’ค์— ๊ฑด ์ถ”๊ฐ€ํ•˜๋Š” ์‹์œผ๋กœ ํ•ด์•ผํ•˜๋Š”๋ฐ...
    • ๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ ๊ธธ์–ด์ง€๋ฉด ์นด์šดํ„ฐ ๊ด€๋ฆฌํ•˜๋Š” ๊ฑธ๋กœ ๊ณ ์น˜๊ณ , ์ง€๊ธˆ์€ ๊ทธ๋ƒฅ ์ œ์ถœ GO!
from collections import Counter

def solution(want, number, discount):
    answer = 0
    Counter_want = Counter()
    for i in range(len(want)):
        Counter_want[want[i]] = number[i]
    Counter_discout = Counter(discount)
    Counter_want_minus_discount = Counter_want - Counter_discout

    if len(Counter_want_minus_discount) == 0:
        for i in range(len(discount)-9):
            Counter_discount_10day = Counter(discount[i:i+10])
            print(i, Counter_want - Counter_discount_10day)
            if len(Counter_want - Counter_discount_10day) == 0:
                answer += 1
        
    return answer
    

ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	["banana", "apple", "rice", "pork", "pot"], [3, 2, 2, 2, 1], ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	3
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
0 Counter({'banana': 1, 'pot': 1})
1 Counter({'banana': 1})
2 Counter()
3 Counter()
4 Counter()

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	["apple"], [10], ["banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ํ†ต๊ณผ! ํ•˜์ง€๋งŒ ์—ญ์‹œ ์†๋„ ๋ฌธ์ œ ์žˆ๊ตฌ๋‚˜.
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (59.81ms, 10.6MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (375.70ms, 14.7MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (49.17ms, 11.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (332.22ms, 15.5MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (141.58ms, 12.9MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (34.64ms, 10.5MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (78.47ms, 11.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (342.15ms, 17.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (53.02ms, 11.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (166.96ms, 13.7MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (28.50ms, 10.6MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด๋ฅผ ๋ณผ๊นŒ๋‚˜~
  • ํ—... ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•œ ๊ฑธ...
from collections import Counter
def solution(want, number, discount):
    answer = 0
    dic = {}
    for i in range(len(want)):
        dic[want[i]] = number[i]

    for i in range(len(discount)-9):
        if dic == Counter(discount[i:i+10]): 
            answer += 1

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (15.31ms, 10.6MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (95.51ms, 14.7MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (21.79ms, 11MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (113.82ms, 15.8MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (66.84ms, 12.9MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (10.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (30.27ms, 11.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (150.26ms, 17.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (25.96ms, 11.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (76.56ms, 13.7MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (15.90ms, 10.6MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
  • ์™€ ๋น ๋ฅด๋‹ค.
def solution(want, number, discount):
    answer = 0
    wList = dict()
    s = 0
    e = 9

    if len(want) == 1:
        if want[0] not in discount:
            return 0 

    while e < len(discount):
        for i in range(len(want)):
            wList[want[i]] = number[i]
        for i in range(s,e+1):
            if discount[i] not in wList:
                s = i+1
                e = s+9
                break
            else:
                wList[discount[i]] -= 1
        else:
            for w in wList.values():
                if w > 0:
                    break
            else:
                answer +=1
            s +=1
            e = s+9
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (4.79ms, 10.5MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (39.69ms, 14.6MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (5.97ms, 11.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (27.59ms, 15.5MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (17.39ms, 12.8MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (2.96ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (16.82ms, 11.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (62.10ms, 17.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (11.63ms, 11MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (32.03ms, 13.7MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (25.75ms, 10.5MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ๋‚˜๋„ ๊ณ ์ณ๋ด์•ผ์ง€... ใ…ก.ใ…ก
from collections import Counter

def solution(want, number, discount):
    answer = 0
    
    Counter_want = Counter()
    for i in range(len(want)):
        Counter_want[want[i]] = number[i]
    
    Counter_discout = Counter(discount)
    Counter_want_minus_discount = Counter_want - Counter_discout

    if len(Counter_want_minus_discount) == 0:
        Counter_discount_10day = Counter(discount[0:10])
        if len(Counter_want - Counter_discount_10day) == 0:
            answer += 1
        print(0, Counter_discount_10day, answer)
        for i in range(1, len(discount)-9):
            ๋นผ๊ธฐ = discount[i-1]
            ์ถ”๊ฐ€ = discount[i+9]
            print(๋นผ๊ธฐ, ์ถ”๊ฐ€)
            Counter_discount_10day[๋นผ๊ธฐ] -= 1
            Counter_discount_10day[์ถ”๊ฐ€] += 1
            print(i, Counter_discount_10day, answer)
            if len(Counter_want - Counter_discount_10day) == 0:
                answer += 1
        
    return answer
    
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	["banana", "apple", "rice", "pork", "pot"], [3, 2, 2, 2, 1], ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	3
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	0 Counter({'apple': 3, 'banana': 2, 'rice': 2, 'pork': 2, 'chicken': 1}) 0
chicken pot
1 Counter({'apple': 3, 'banana': 2, 'rice': 2, 'pork': 2, 'pot': 1, 'chicken': 0}) 0
apple banana
2 Counter({'banana': 3, 'apple': 2, 'rice': 2, 'pork': 2, 'pot': 1, 'chicken': 0}) 0
apple apple
3 Counter({'banana': 3, 'apple': 2, 'rice': 2, 'pork': 2, 'pot': 1, 'chicken': 0}) 1
banana banana
4 Counter({'banana': 3, 'apple': 2, 'rice': 2, 'pork': 2, 'pot': 1, 'chicken': 0}) 2
ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	["apple"], [10], ["banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	0
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ์ œ์ถœ!
from collections import Counter

def solution(want, number, discount):
    answer = 0
    
    Counter_want = Counter()
    for i in range(len(want)):
        Counter_want[want[i]] = number[i]
    
    Counter_discout = Counter(discount)
    Counter_want_minus_discount = Counter_want - Counter_discout

    if len(Counter_want_minus_discount) == 0:
        Counter_discount_10day = Counter(discount[0:10])
        if len(Counter_want - Counter_discount_10day) == 0:
            answer += 1
        for i in range(1, len(discount)-9):
            ๋นผ๊ธฐ = discount[i-1]
            ์ถ”๊ฐ€ = discount[i+9]
            Counter_discount_10day[๋นผ๊ธฐ] -= 1
            Counter_discount_10day[์ถ”๊ฐ€] += 1
            if len(Counter_want - Counter_discount_10day) == 0:
                answer += 1
        
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (46.43ms, 10.9MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (315.19ms, 14.9MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (64.02ms, 11.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (291.38ms, 15.7MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (163.12ms, 13.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (26.79ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (83.12ms, 11.6MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (458.11ms, 17.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (61.31ms, 11.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (191.51ms, 13.9MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (17.31ms, 10.8MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
  • ๋ณ„๋กœ ์•ˆ ๋น ๋ฆ„? ใ…‹ใ…‹ใ…‹
  • ์นด์šดํ„ฐ ๋ผ๋ฆฌ ์—ฐ์‚ฐํ•˜๋Š” ๋ถ€๋ถ„์ด ๋Š๋ฆฐ ๊ฑฐ์˜€๊ตฌ๋‚˜...? ๊ฒŒ๋‹ค๊ฐ€ ๋“ค์ญ‰๋‚ ์ญ‰ํ•˜๋‹ค.

  • ์—ฌ๊ธฐ์„œ ๊ทธ๋งŒ~
728x90
๋ฐ˜์‘ํ˜•