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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณ ๋“์  Kit (2) ํ•ด์‹œ

๐ŸŽฎinspirer9 2023. 2. 9. 12:38
728x90
๋ฐ˜์‘ํ˜•

ํ•ด์‹œ

  • ์„ค๋ช… : Key-value์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ์•„๋ณด์„ธ์š”.
  • ์ถœ์ œ ๋นˆ๋„ : ๋†’์Œ
  • ํ‰๊ท  ์ ์ˆ˜ : ๋ณดํ†ต

ํฐ์ผ“๋ชฌ (Level 1, 29813๋ช… ์™„๋ฃŒ)

๋ฌธ์ œ ์„ค๋ช…

๋‹น์‹ ์€ ํฐ์ผ“๋ชฌ์„ ์žก๊ธฐ ์œ„ํ•œ ์˜ค๋žœ ์—ฌํ–‰ ๋์—, ํ™ ๋ฐ•์‚ฌ๋‹˜์˜ ์—ฐ๊ตฌ์‹ค์— ๋„์ฐฉํ–ˆ์Šต๋‹ˆ๋‹ค. ํ™ ๋ฐ•์‚ฌ๋‹˜์€ ๋‹น์‹ ์—๊ฒŒ ์ž์‹ ์˜ ์—ฐ๊ตฌ์‹ค์— ์žˆ๋Š” ์ด N ๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ ์ค‘์—์„œ N/2๋งˆ๋ฆฌ๋ฅผ ๊ฐ€์ ธ๊ฐ€๋„ ์ข‹๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ™ ๋ฐ•์‚ฌ๋‹˜ ์—ฐ๊ตฌ์‹ค์˜ ํฐ์ผ“๋ชฌ์€ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์—ฌ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์€ ๊ฐ™์€ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์—ฐ๊ตฌ์‹ค์— ์ด 4๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ์ด ์žˆ๊ณ , ๊ฐ ํฐ์ผ“๋ชฌ์˜ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ๊ฐ€ [3๋ฒˆ, 1๋ฒˆ, 2๋ฒˆ, 3๋ฒˆ]์ด๋ผ๋ฉด ์ด๋Š” 3๋ฒˆ ํฐ์ผ“๋ชฌ ๋‘ ๋งˆ๋ฆฌ, 1๋ฒˆ ํฐ์ผ“๋ชฌ ํ•œ ๋งˆ๋ฆฌ, 2๋ฒˆ ํฐ์ผ“๋ชฌ ํ•œ ๋งˆ๋ฆฌ๊ฐ€ ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด๋•Œ, 4๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ ์ค‘ 2๋งˆ๋ฆฌ๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด 6๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ์ฒซ ๋ฒˆ์งธ(3๋ฒˆ), ๋‘ ๋ฒˆ์งธ(1๋ฒˆ) ํฐ์ผ“๋ชฌ์„ ์„ ํƒ
  2. ์ฒซ ๋ฒˆ์งธ(3๋ฒˆ), ์„ธ ๋ฒˆ์งธ(2๋ฒˆ) ํฐ์ผ“๋ชฌ์„ ์„ ํƒ
  3. ์ฒซ ๋ฒˆ์งธ(3๋ฒˆ), ๋„ค ๋ฒˆ์งธ(3๋ฒˆ) ํฐ์ผ“๋ชฌ์„ ์„ ํƒ
  4. ๋‘ ๋ฒˆ์งธ(1๋ฒˆ), ์„ธ ๋ฒˆ์งธ(2๋ฒˆ) ํฐ์ผ“๋ชฌ์„ ์„ ํƒ
  5. ๋‘ ๋ฒˆ์งธ(1๋ฒˆ), ๋„ค ๋ฒˆ์งธ(3๋ฒˆ) ํฐ์ผ“๋ชฌ์„ ์„ ํƒ
  6. ์„ธ ๋ฒˆ์งธ(2๋ฒˆ), ๋„ค ๋ฒˆ์งธ(3๋ฒˆ) ํฐ์ผ“๋ชฌ์„ ์„ ํƒ

์ด๋•Œ, ์ฒซ ๋ฒˆ์งธ(3๋ฒˆ) ํฐ์ผ“๋ชฌ๊ณผ ๋„ค ๋ฒˆ์งธ(3๋ฒˆ) ํฐ์ผ“๋ชฌ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํ•œ ์ข…๋ฅ˜(3๋ฒˆ ํฐ์ผ“๋ชฌ ๋‘ ๋งˆ๋ฆฌ)์˜ ํฐ์ผ“๋ชฌ๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋“ค์€ ๋ชจ๋‘ ๋‘ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„ ์˜ˆ์‹œ์—์„œ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ํฐ์ผ“๋ชฌ ์ข…๋ฅ˜ ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์€ 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
๋‹น์‹ ์€ ์ตœ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ๊ฐ€์ง€๊ธธ ์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ตœ๋Œ€ํ•œ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ํฌํ•จํ•ด์„œ N/2๋งˆ๋ฆฌ๋ฅผ ์„ ํƒํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. N๋งˆ๋ฆฌ ํฐ์ผ“๋ชฌ์˜ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด nums๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, N/2๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘, ๊ฐ€์žฅ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„, ๊ทธ๋•Œ์˜ ํฐ์ผ“๋ชฌ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • nums๋Š” ํฐ์ผ“๋ชฌ์˜ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด 1์ฐจ์› ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • nums์˜ ๊ธธ์ด(N)๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋ฉฐ, ํ•ญ์ƒ ์ง์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
  • ํฐ์ผ“๋ชฌ์˜ ์ข…๋ฅ˜ ๋ฒˆํ˜ธ๋Š” 1 ์ด์ƒ 200,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • ๊ฐ€์žฅ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€์ธ ๊ฒฝ์šฐ์—๋„, ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ํฐ์ผ“๋ชฌ ์ข…๋ฅ˜ ๊ฐœ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’ ํ•˜๋‚˜๋งŒ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

nums result
[3,1,2,3] 2
[3,3,3,2,2,4] 3
[3,3,3,2,2,2] 2

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

์ž…์ถœ๋ ฅ ์˜ˆ #1
๋ฌธ์ œ์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
6๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ์ด ์žˆ์œผ๋ฏ€๋กœ, 3๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ์„ ๊ณจ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ฐ€์žฅ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ๊ณ ๋ฅด๊ธฐ ์œ„ํ•ด์„œ๋Š” 3๋ฒˆ ํฐ์ผ“๋ชฌ ํ•œ ๋งˆ๋ฆฌ, 2๋ฒˆ ํฐ์ผ“๋ชฌ ํ•œ ๋งˆ๋ฆฌ, 4๋ฒˆ ํฐ์ผ“๋ชฌ ํ•œ ๋งˆ๋ฆฌ๋ฅผ ๊ณ ๋ฅด๋ฉด ๋˜๋ฉฐ, ๋”ฐ๋ผ์„œ 3์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3
6๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ์ด ์žˆ์œผ๋ฏ€๋กœ, 3๋งˆ๋ฆฌ์˜ ํฐ์ผ“๋ชฌ์„ ๊ณจ๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ฐ€์žฅ ๋งŽ์€ ์ข…๋ฅ˜์˜ ํฐ์ผ“๋ชฌ์„ ๊ณ ๋ฅด๊ธฐ ์œ„ํ•ด์„œ๋Š” 3๋ฒˆ ํฐ์ผ“๋ชฌ ํ•œ ๋งˆ๋ฆฌ์™€ 2๋ฒˆ ํฐ์ผ“๋ชฌ ๋‘ ๋งˆ๋ฆฌ๋ฅผ ๊ณ ๋ฅด๊ฑฐ๋‚˜, ํ˜น์€ 3๋ฒˆ ํฐ์ผ“๋ชฌ ๋‘ ๋งˆ๋ฆฌ์™€ 2๋ฒˆ ํฐ์ผ“๋ชฌ ํ•œ ๋งˆ๋ฆฌ๋ฅผ ๊ณ ๋ฅด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ตœ๋Œ€ ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ํฐ์ผ“๋ชฌ ์ข…๋ฅ˜์˜ ์ˆ˜๋Š” 2์ž…๋‹ˆ๋‹ค.

ํ’€์ด

๋‚˜์˜ ํ’€์ด...

def solution(nums):
    answer = len(nums) // 2
    nums.sort() #๋ถˆํ•„์š”
    nums = list(set(nums)) #์—ฌ๊ธฐ์„œ set์„ ํ•˜๋ฉด sortํ•  ํ•„์š”๊ฐ€ ์—†์Œ.
    if answer > len(nums): #min() ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ ํ•œ์ค„๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ.
        answer = len(nums)
    return answer

๊ณ ์ˆ˜์˜ ์ง€๋ฆฌ๋Š” ํ’€์ด...

def solution(ls):
    return min(len(ls)/2, len(set(ls)))

์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜ (Level 1, 76150๋ช… ์™„๋ฃŒ)

๋ฌธ์ œ ์„ค๋ช…

์ˆ˜๋งŽ์€ ๋งˆ๋ผํ†ค ์„ ์ˆ˜๋“ค์ด ๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹จ ํ•œ ๋ช…์˜ ์„ ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋“  ์„ ์ˆ˜๊ฐ€ ๋งˆ๋ผํ†ค์„ ์™„์ฃผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด participant์™€ ์™„์ฃผํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด completion์ด ์ฃผ์–ด์งˆ ๋•Œ, ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ๋งˆ๋ผํ†ค ๊ฒฝ๊ธฐ์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜์˜ ์ˆ˜๋Š” 1๋ช… ์ด์ƒ 100,000๋ช… ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • completion์˜ ๊ธธ์ด๋Š” participant์˜ ๊ธธ์ด๋ณด๋‹ค 1 ์ž‘์Šต๋‹ˆ๋‹ค.
  • ์ฐธ๊ฐ€์ž์˜ ์ด๋ฆ„์€ 1๊ฐœ ์ด์ƒ 20๊ฐœ ์ดํ•˜์˜ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฐธ๊ฐ€์ž ์ค‘์—๋Š” ๋™๋ช…์ด์ธ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ


participant completion return
["leo", "kiki", "eden"] ["eden", "kiki"] "leo"
["marina", "josipa", "nikola", "vinko", "filipa"] ["josipa", "filipa", "marina", "nikola"] "vinko"
["mislav", "stanko", "mislav", "ana"] ["stanko", "ana", "mislav"] "mislav"

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

์˜ˆ์ œ #1

"leo"๋Š” ์ฐธ์—ฌ์ž ๋ช…๋‹จ์—๋Š” ์žˆ์ง€๋งŒ, ์™„์ฃผ์ž ๋ช…๋‹จ์—๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ์™„์ฃผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #2
"vinko"๋Š” ์ฐธ์—ฌ์ž ๋ช…๋‹จ์—๋Š” ์žˆ์ง€๋งŒ, ์™„์ฃผ์ž ๋ช…๋‹จ์—๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ์™„์ฃผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #3
"mislav"๋Š” ์ฐธ์—ฌ์ž ๋ช…๋‹จ์—๋Š” ๋‘ ๋ช…์ด ์žˆ์ง€๋งŒ, ์™„์ฃผ์ž ๋ช…๋‹จ์—๋Š” ํ•œ ๋ช…๋ฐ–์— ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ช…์€ ์™„์ฃผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ’€์ด

๋‚˜์˜ ํ’€์ด, ์ตœ๊ทผ์— ์•Œ๊ฒŒ๋œ Counter๋กœ ๋‹ค์‹œ ํ’€์–ด๋ณด์•˜๋‹ค. ์‰ฝ๋„ค? ใ…ก.ใ…ก;
from collections import Counter

def solution(participant, completion):
    participant_counter = Counter(participant)
    completion_counter = Counter(completion)
    not_completion = participant_counter - completion_counter
    return list(not_completion)[0]

๊ณ ์ˆ˜์˜ ํ’€์ด ๋ณด๋‹ˆ... 1์ค„ ๋ณ€ํƒœ๋“ค๋„ ์žˆ๋„ค;

from collections import Counter

def solution(participant, completion):
    return list(Counter(participant) - Counter(completion))[0]

์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก (Level 2, 45861๋ช… ์™„๋ฃŒ)

๋ฌธ์ œ ์„ค๋ช…

์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ ์ค‘, ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.
์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ, ๊ตฌ์กฐ๋Œ€ ์ „ํ™”๋ฒˆํ˜ธ๋Š” ์˜์„์ด์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค.

  • ๊ตฌ์กฐ๋Œ€ : 119
  • ๋ฐ•์ค€์˜ : 97 674 223
  • ์ง€์˜์„ : 11 9552 4421

์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด phone_book ์ด solution ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์–ด๋–ค ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฉด false๋ฅผ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด true๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • phone_book์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 1,000,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • ๊ฐ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • ๊ฐ™์€ ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ์ค‘๋ณตํ•ด์„œ ๋“ค์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

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


phone_book return
["119", "97674223", "1195524421"] false
["123","456","789"] true
["12","123","1235","567","88"] false

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

์ž…์ถœ๋ ฅ ์˜ˆ #1
์•ž์—์„œ ์„ค๋ช…ํ•œ ์˜ˆ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ธ ๊ฒฝ์šฐ๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ๋‹ต์€ true์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3
์ฒซ ๋ฒˆ์งธ ์ „ํ™”๋ฒˆํ˜ธ, “12”๊ฐ€ ๋‘ ๋ฒˆ์งธ ์ „ํ™”๋ฒˆํ˜ธ “123”์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ต์€ false์ž…๋‹ˆ๋‹ค.

์•Œ๋ฆผ

2021๋…„ 3์›” 4์ผ, ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ์ด์ „์— ํ†ต๊ณผํ•˜๋˜ ์ฝ”๋“œ๊ฐ€ ๋” ์ด์ƒ ํ†ต๊ณผํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ’€์ด

C++๋กœ๋Š” ์ด๋ ‡๊ฒŒ ํ’€์—ˆ์—ˆ๋Š”๋ฐ...

#include <string>
#include <vector>
#include <iostream>
#include <set>

using namespace std;

bool solution(vector<string> phone_book) {
    set<string> tmp;
    
    for (auto& number : phone_book)
        tmp.insert(number);
    
    for (auto it1 = tmp.begin(); it1 != tmp.end(); ++it1)
    {
        for (auto it2 = it1; it2 != tmp.end(); ++it2)
        {
            if(it1!=it2)
                if (it2->find(*it1) == 0)
                    return false;
                else
                    break;
        }
    }
    return true;
}

ํŒŒ์ด์ฌ์œผ๋กœ๋Š” ์ด๋ ‡๊ฒŒ ๋ฐ”๊พธ๋ฉด ๊น”๋”ํ•˜๋‹ค.

def solution(phone_book):
    phone_book.sort()
    for i in range(len(phone_book)-1):
        if phone_book[i+1].startswith(phone_book[i]):
            return False
    return True

๊ณ ์ˆ˜์˜ ์ •์„ ํ’€์ด ใ…ก.ใ…ก;;;;

def solution(phone_book):
    answer = True
    hash_map = {}
    for phone_number in phone_book:
        hash_map[phone_number] = 1
    for phone_number in phone_book:
        temp = ""
        for number in phone_number:
            temp += number
            if temp in hash_map and temp != phone_number:
                answer = False
    return answer

O(N) ํ’€์ด. zip์œผ๋กœ ํ•˜๋ฉด O(N)์ด๋ผ๊ณ  ํ•œ๋‹ค.

def solution(phoneBook):
    phoneBook = sorted(phoneBook)

    for p1, p2 in zip(phoneBook, phoneBook[1:]):
        if p2.startswith(p1):
            return False
    return True

์œ„์žฅ (Level 2, 41255๋ช… ์™„๋ฃŒ)

๋ฌธ์ œ ์„ค๋ช…

์ŠคํŒŒ์ด๋“ค์€ ๋งค์ผ ๋‹ค๋ฅธ ์˜ท์„ ์กฐํ•ฉํ•˜์—ฌ ์ž…์–ด ์ž์‹ ์„ ์œ„์žฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜ท์ด ์•„๋ž˜์™€ ๊ฐ™๊ณ  ์˜ค๋Š˜ ์ŠคํŒŒ์ด๊ฐ€ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ธด ์ฝ”ํŠธ, ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ ๋ฅผ ์ž…์—ˆ๋‹ค๋ฉด ๋‹ค์Œ๋‚ ์€ ์ฒญ๋ฐ”์ง€๋ฅผ ์ถ”๊ฐ€๋กœ ์ž…๊ฑฐ๋‚˜ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ ๋Œ€์‹  ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค๋ฅผ ์ฐฉ์šฉํ•˜๊ฑฐ๋‚˜ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ข…๋ฅ˜ ์ด๋ฆ„
์–ผ๊ตด ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค
์ƒ์˜ ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ 
ํ•˜์˜ ์ฒญ๋ฐ”์ง€
๊ฒ‰์˜ท ๊ธด ์ฝ”ํŠธ

์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ๋“ค์ด ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด clothes๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์„œ๋กœ ๋‹ค๋ฅธ ์˜ท์˜ ์กฐํ•ฉ์˜ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • clothes์˜ ๊ฐ ํ–‰์€ [์˜์ƒ์˜ ์ด๋ฆ„, ์˜์ƒ์˜ ์ข…๋ฅ˜]๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ์˜ ์ˆ˜๋Š” 1๊ฐœ ์ด์ƒ 30๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์˜์ƒ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • clothes์˜ ๋ชจ๋“  ์›์†Œ๋Š” ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ด๊ณ  ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž ๋˜๋Š” '_' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŒŒ์ด๋Š” ํ•˜๋ฃจ์— ์ตœ์†Œ ํ•œ ๊ฐœ์˜ ์˜์ƒ์€ ์ž…์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ


clothes return
[["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]] 5
[["crow_mask", "face"], ["blue_sunglasses", "face"], ["smoky_makeup", "face"]] 3

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

์˜ˆ์ œ #1
headgear์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด yellow_hat, green_turban์ด๊ณ  eyewear์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด blue_sunglasses์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 5๊ฐœ์˜ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

1. yellow_hat 
2. blue_sunglasses 
3. green_turban 
4. yellow_hat + blue_sunglasses 
5. green_turban + blue_sunglasses

์˜ˆ์ œ #2
face์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด crow_mask, blue_sunglasses, smoky_makeup์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 3๊ฐœ์˜ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

1. crow_mask
2. blue_sunglasses
3. smoky_makeup

ํ’€์ด

C++๋กœ ํ’€ ๋•Œ๋Š” ์ •์„์œผ๋กœ ํ’€์—ˆ์ง€๋งŒ Python์œผ๋กœ ํ•˜๋‹ˆ๊นŒ ์•ผ๋งค๋กœ ํ•˜๊ฒŒ ๋˜๋Š” ๋Š๋‚Œ...

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

int solution(vector<vector<string>> clothes) {
    int answer = 1;
    
    map<string,int> hashmap;
    map<string,int>::iterator iter;
    
    for(int i=0;i<clothes.size();i++)
    {
        if ( hashmap.find(clothes[i][1]) == hashmap.end() ) {
            hashmap[clothes[i][1]] = 1;
        } else {
            hashmap[clothes[i][1]]++;
        }
    }

    for(iter=hashmap.begin();iter!=hashmap.end();iter++) {
        cout << iter->first << " " << iter->second << endl;
        answer *= (iter->second+1);
    }    
    
    return answer - 1;
}

๊ทธ๋Œ€๋กœ ์˜ฎ๊ฒจ๋ณด๋ฉด... ์™€ ์†Œ์Šค์ฝ”๋“œ ๊ธธ์ด ๋Œ€๋ฐ•!

def solution(clothes):
    answer = 1
    hashmap = {}
    
    for i in clothes:
        if i[1] in hashmap:
            hashmap[i[1]] += 1
        else:
            hashmap[i[1]] = 1
    
    for i in hashmap.values():
        answer *= (i + 1)
         
    return answer - 1

๋ฒ ์ŠคํŠธ์•จ๋ฒ” (Level 3, 25659๋ช… ์™„๋ฃŒ)

๋ฌธ์ œ ์„ค๋ช…

์ŠคํŠธ๋ฆฌ๋ฐ ์‚ฌ์ดํŠธ์—์„œ ์žฅ๋ฅด ๋ณ„๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์žฌ์ƒ๋œ ๋…ธ๋ž˜๋ฅผ ๋‘ ๊ฐœ์”ฉ ๋ชจ์•„ ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์„ ์ถœ์‹œํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋ž˜๋Š” ๊ณ ์œ  ๋ฒˆํ˜ธ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉฐ, ๋…ธ๋ž˜๋ฅผ ์ˆ˜๋กํ•˜๋Š” ๊ธฐ์ค€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์†ํ•œ ๋…ธ๋ž˜๊ฐ€ ๋งŽ์ด ์žฌ์ƒ๋œ ์žฅ๋ฅด๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.
  2. ์žฅ๋ฅด ๋‚ด์—์„œ ๋งŽ์ด ์žฌ์ƒ๋œ ๋…ธ๋ž˜๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.
  3. ์žฅ๋ฅด ๋‚ด์—์„œ ์žฌ์ƒ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™์€ ๋…ธ๋ž˜ ์ค‘์—์„œ๋Š” ๊ณ ์œ  ๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ๋…ธ๋ž˜๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.

๋…ธ๋ž˜์˜ ์žฅ๋ฅด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด genres์™€ ๋…ธ๋ž˜๋ณ„ ์žฌ์ƒ ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด plays๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์— ๋“ค์–ด๊ฐˆ ๋…ธ๋ž˜์˜ ๊ณ ์œ  ๋ฒˆํ˜ธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • genres[i]๋Š” ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ i์ธ ๋…ธ๋ž˜์˜ ์žฅ๋ฅด์ž…๋‹ˆ๋‹ค.
  • plays[i]๋Š” ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ i์ธ ๋…ธ๋ž˜๊ฐ€ ์žฌ์ƒ๋œ ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • genres์™€ plays์˜ ๊ธธ์ด๋Š” ๊ฐ™์œผ๋ฉฐ, ์ด๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์žฅ๋ฅด ์ข…๋ฅ˜๋Š” 100๊ฐœ ๋ฏธ๋งŒ์ž…๋‹ˆ๋‹ค.
  • ์žฅ๋ฅด์— ์†ํ•œ ๊ณก์ด ํ•˜๋‚˜๋ผ๋ฉด, ํ•˜๋‚˜์˜ ๊ณก๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์žฅ๋ฅด๋Š” ์žฌ์ƒ๋œ ํšŸ์ˆ˜๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

genres plays return
["classic", "pop", "classic", "classic", "pop"] [500, 600, 150, 800, 2500] [4, 1, 3, 0]

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

classic ์žฅ๋ฅด๋Š” 1,450ํšŒ ์žฌ์ƒ๋˜์—ˆ์œผ๋ฉฐ, classic ๋…ธ๋ž˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๊ณ ์œ  ๋ฒˆํ˜ธ 3: 800ํšŒ ์žฌ์ƒ
  • ๊ณ ์œ  ๋ฒˆํ˜ธ 0: 500ํšŒ ์žฌ์ƒ
  • ๊ณ ์œ  ๋ฒˆํ˜ธ 2: 150ํšŒ ์žฌ์ƒ

pop ์žฅ๋ฅด๋Š” 3,100ํšŒ ์žฌ์ƒ๋˜์—ˆ์œผ๋ฉฐ, pop ๋…ธ๋ž˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๊ณ ์œ  ๋ฒˆํ˜ธ 4: 2,500ํšŒ ์žฌ์ƒ
  • ๊ณ ์œ  ๋ฒˆํ˜ธ 1: 600ํšŒ ์žฌ์ƒ

๋”ฐ๋ผ์„œ pop ์žฅ๋ฅด์˜ [4, 1]๋ฒˆ ๋…ธ๋ž˜๋ฅผ ๋จผ์ €, classic ์žฅ๋ฅด์˜ [3, 0]๋ฒˆ ๋…ธ๋ž˜๋ฅผ ๊ทธ๋‹ค์Œ์— ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.

  • ์žฅ๋ฅด ๋ณ„๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์žฌ์ƒ๋œ ๋…ธ๋ž˜๋ฅผ ์ตœ๋Œ€ ๋‘ ๊ฐœ๊นŒ์ง€ ๋ชจ์•„ ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์„ ์ถœ์‹œํ•˜๋ฏ€๋กœ 2๋ฒˆ ๋…ธ๋ž˜๋Š” ์ˆ˜๋ก๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

โ€ป ๊ณต์ง€ - 2019๋…„ 2์›” 28์ผ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

ํ’€์ด

๋‚˜์˜ C++ ํ’€์ด...

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <tuple>

using namespace std;

#define TUP tuple<int,int,int,int,int>

vector<int> solution(vector<string> genres, vector<int> plays) {
    vector<int> answer;
    
    map<string, TUP> genres_map;
    vector<pair<int, string>> v;
    
    for(int i=0;i<genres.size();i++)
    {
        if(genres_map.find(genres[i]) == genres_map.end() )
        {
            genres_map[genres[i]] = make_tuple(plays[i],plays[i],i,-1,-1);
        }
        else
        {
            get<0>(genres_map[genres[i]]) += plays[i];
            
            if(plays[i] > get<1>(genres_map[genres[i]]) && plays[i] > get<3>(genres_map[genres[i]]))
            {
                if(get<1>(genres_map[genres[i]]) < get<3>(genres_map[genres[i]]))
                {
                    get<1>(genres_map[genres[i]]) = plays[i];    
                    get<2>(genres_map[genres[i]]) = i;    
                }
                else
                {
                    get<3>(genres_map[genres[i]]) = plays[i];    
                    get<4>(genres_map[genres[i]]) = i;    
                }
            }
            else
            {
                if(plays[i] > get<1>(genres_map[genres[i]]))
                {
                    get<1>(genres_map[genres[i]]) = plays[i];    
                    get<2>(genres_map[genres[i]]) = i;    
                }
                
                if(plays[i] > get<3>(genres_map[genres[i]]))
                {
                    get<3>(genres_map[genres[i]]) = plays[i];    
                    get<4>(genres_map[genres[i]]) = i;    
                }
            }
        }
    }
    for(map<string, TUP>::iterator itr = genres_map.begin(); itr != genres_map.end(); ++itr)
    {
        v.push_back(make_pair(get<0>(itr->second),itr->first));
    }
    
    sort(v.begin(), v.end(), greater<>());
    
    for(vector<pair<int, string>>::iterator iter = v.begin(); iter != v.end(); iter++)
    {   
        if(get<1>(genres_map[iter->second]) < get<3>(genres_map[iter->second]))
        {
            answer.push_back( get<4>(genres_map[iter->second]) );
            answer.push_back( get<2>(genres_map[iter->second]) );
        }
        else if(get<1>(genres_map[iter->second]) > get<3>(genres_map[iter->second]))
        {
            answer.push_back( get<2>(genres_map[iter->second]) );
            if(get<3>(genres_map[iter->second]) != -1)
                answer.push_back( get<4>(genres_map[iter->second]) );
        }
        else
        {
            if(get<2>(genres_map[iter->second]) < get<4>(genres_map[iter->second]))
            {
                answer.push_back( get<2>(genres_map[iter->second]) );
                answer.push_back( get<4>(genres_map[iter->second]) );   
            }
            else
            {
                answer.push_back( get<4>(genres_map[iter->second]) );
                answer.push_back( get<2>(genres_map[iter->second]) );   
            }
        }
    }
    
    return answer;
}

์ €๊ฑธ ํŒŒ์ด์ฌ์œผ๋กœ ์˜ฎ๊ธด... ๋‚˜์˜ ํ’€์ด

from collections import Counter

def solution(genres, plays):
    answer = []
    
    genres_total_play = {} # ์žฅ๋ฅด ํ† ํƒˆ ํ”Œ๋ ˆ์ด์‹œ๊ฐ„ = ์ธ๊ธฐ ์žฅ๋ฅด ์ˆœ์œ„
    genres_song_count = {} # ์žฅ๋ฅด๋ณ„ ๊ณก 2๊ฐœ์”ฉ ์ €์žฅ... ๊ทธ๋ƒฅ ๋‹ค ์ €์žฅํ•˜๊ณ  2๊ณก์”ฉ ๋ฝ‘์•„์จ์•ผ...
    
    for i in range(len(genres)):
        if genres[i] in genres_total_play:
            genres_total_play[genres[i]] += plays[i]
            genres_song_count[genres[i]].append((i, plays[i]))
        else:
            genres_total_play[genres[i]] = plays[i]
            genres_song_count[genres[i]] = [(i, plays[i])]
    
    popular_genre_list = sorted(genres_total_play.items(), key=lambda x:x[1], reverse=True)
    
    for i in popular_genre_list: # ์ด ์ˆœ์„œ๋Œ€๋กœ 2๊ณก์”ฉ
        genre_song_list = genres_song_count[i[0]]
        best_song_list = sorted(genre_song_list, key = lambda x:-x[1] )
        for ii in range(len(best_song_list)):
            #print(i[0], best_song_list[ii])
            answer.append(best_song_list[ii][0])
            if ii == 1:
                break

    return answer

๊ณ ์ˆ˜์˜ ํŒŒ์ด์ฌ ํ’€์ด

def solution(genres, plays):
    answer = []
    d = {e:[] for e in set(genres)}
    for e in zip(genres, plays, range(len(plays))):
        d[e[0]].append([e[1] , e[2]])
    genreSort =sorted(list(d.keys()), key= lambda x: sum( map(lambda y: y[0],d[x])), reverse = True)
    for g in genreSort:
        temp = [e[1] for e in sorted(d[g],key= lambda x: (x[0], -x[1]), reverse = True)]
        answer += temp[:min(len(temp),2)]
    return answer

ํ˜„ํƒ€ ์˜ค์ง€๊ฒŒ ์˜จ๋‹ค. ใ…‹ใ…‹ใ…‹

ํŒŒ์ด์ฌ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๋” ์ž์œ ๋กญ๊ฒŒ ์“ธ ์ˆ˜ ์žˆ์–ด์•ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ๋” ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ...

728x90
๋ฐ˜์‘ํ˜•