Logo
Hyunsu Blog
this

๐Ÿ“†Published :Mar 24, 2024 โ€ข

๐Ÿ“†Updated :Mar 24, 2024 โ€ข

โ˜•๏ธ1min

leetcode 846. Hand of Straights

๋ฌธ์ œ ์ถœ์ฒ˜ : 846. Hand of Straights

๋ฌธ์ œ

Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.
Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
Example 2:
Input: hand = [1,2,3,4,5], groupSize = 4
Output: false
Explanation: Alice's hand can not be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 104
0 <= hand[i] <= 109
1 <= groupSize <= hand.length

๋ฌธ์ œ ์ ‘๊ทผ๋ฒ•

์ด ๋ฌธ์ œ์—์„œ ๊ผญ ์ง€์ผœ์•ผ ํ•  ๋‘๊ฐ€์ง€ ๊ฐ€ ์žˆ๋‹ค.

  1. ํ•˜๋‚˜์˜ ์นด๋“œ ๊ทธ๋ฃน์— groupSize ๋งŒํผ ์นด๋“œ๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๊ณ ,
  2. ์นด๋“œ๋“ค์€ ์—ฐ์†์ ์ธ ์ˆซ์ž์—ฌ์•ผ ํ•œ๋‹ค.

์ด ๋‘๊ฐ€์ง€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ ์ƒํƒœ์—์„œ ๊ทธ๋ฃจํ•‘์ด ํ•œ๊ฐœ๋„ ๋น ์ง์—†์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์•„๋‹ˆ๋ผ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

ํ•˜๋‚˜์˜ ์นด๋“œ ๊ทธ๋ฃน์— groupSize๋งŒํผ์˜ ์นด๋“œ์™€ ์—ฐ์†๋œ ์ˆซ์ž๋ฅผ ๊ฐ€์ง„ ์นด๋“œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„  ์ค‘๋ณต๋œ ์ˆซ์ž ์นด๋“œ๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„์„ ์ฒ˜๋ฆฌ ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ์ฆ‰ ๊ฐ ๊ทธ๋ฃน์— ๋“ค์–ด๊ฐ„ ์นด๋“œ๋ฅผ ํ˜„์žฌ ๋‚จ์€ ์นด๋“œ์—์„œ ๋นผ์ฃผ๋Š” ๋ฐฉ์‹์„ ํƒํ•œ๋‹ค. ์ด ๋ฌธ์ œ์—์„œ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š” ๋‚จ์€ ์ˆซ์ž ์นด๋“œ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž ์นด๋“œ๋ฅผ ์„ ํƒํ•˜๊ณ  ์„ ํƒํ•œ ์ˆซ์ž๋กœ ๋ถ€ํ„ฐ ์—ฐ์†๋œ ์ˆซ์ž๋ฅผ groupSize๋งŒํผ ์ผ๋‹จ ์ง€์ •ํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์—ฐ์†๋œ ์ˆซ์ž๋ฅผ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ˆซ์ž๋ฅผ ๋ฏธ๋ฆฌ ์ง€์ •ํ•œ ๋‹ค์Œ, ๊ทธ ์ˆซ์ž๊ฐ€ ํ˜„์žฌ ๋‚จ์€ ์นด๋“œ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์•ฝ ์—†๋‹ค๋ฉด, False๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์žˆ๋‹ค๋ฉด ๊ทธ ์ˆซ์ž๋ฅผ ๋นผ์ฃผ๊ณ , ๋‹ค์Œ ์ˆซ์ž๋ฅผ ํ™•์ธํ•œ๋‹ค. ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๋ชจ๋“  ์นด๋“œ๋ฅผ ๊ทธ๋ฃนํ•‘ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(nlogn)์ด๋‹ค. (์ •๋ ฌ ํ•œ ๋ณต์žก๋„๊ฐ€ ๊ฐ€์žฅ ํฌ๋‹ค.)

์†Œ์Šค ์ฝ”๋“œ

# ๋ฌธ์ œ์˜ ์กฐ๊ฑด์—์„œ ๋ชจ๋“  ์›์†Œ๋“ค์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. # ๊ทธ๋Ÿผ ๊ทธ ์กฐ๊ฑด์— ๋‹ค ๋งž์ถ”๋ ค๋ฉด, ์‚ฌ์šฉํ•˜๊ณ  ๋‚จ์€ ์ˆ˜์ค‘์— ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์ƒˆ๋กœ์šด ๊ทธ๋ฃน์˜ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๋„ฃ๊ณ  ๊ทธ ๊ทธ๋ฃน์— ํ•ด๋‹นํ•˜๋Š” ์—ฐ์†์ ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜๋ฅผ ๋ฐฐ์—ด์—์„œ ์ฐพ๋Š”๋‹ค. #๊ฐ ํ‚ค๋ฅผ ๋Œ๋ฉด์„œ, # ๊ทธ ํ‚ค๊ฐ€ mapper์— ์žˆ๋Š”์ง€ ํ™•์ธ, # ๋งŒ์•ฝ ์ž‡๋‹ค๋ฉด (์ด๊ฒŒ ์ตœ์†Ÿ๊ฐ’์ด๋‹ค.) # ์ตœ์†Ÿ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์—ฐ์†๋œ ๊ฐ’์„ ์ •ํ•ด # ๊ทธ ๊ฐ’๋“ค์ด ์•ˆ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. # ์—†๋‹ค๋ฉด, ๋ฆฌํ„ดํ•œ๋‹ค. def isNStraightHand(self, hand, groupSize): """ :type hand: List[int] :type groupSize: int :rtype: bool """ if(len(hand)%groupSize != 0): return False #dictionary๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์ˆซ์ž์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค. freq = {} # ๊ฐ ์ˆซ์ž์˜ ๋นˆ๋„์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค. for h in hand: freq[h] = (freq.get(h) or 0) +1 # ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž ์นด๋“œ๋ฅผ ๋ฝ‘๊ธฐ ์œ„ํ•ด ์ •๋ ฌ ํ›„ ์ˆœํšŒํ•œ๋‹ค. for f in sorted(freq.keys()): # ๋งŒ์•ฝ ๋‚จ์€ ์นด๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด while( freq[f] > 0): # ํ•ด๋‹น ์นด๋“œ๋ฅผ ๋ฝ‘๋Š”๋‹ค. freq[f]-=1 # ์ด ์•„์ด๋””์–ด๊ฐ€ํ•ต์‹ฌํฌ์ธํŠธ for i in range(f+1, f+groupSize): #๊ทธ๋ ‡๊ฒŒ ์ง€์ •๋œ ์ˆซ์ž๊ฐ€ if( i in freq and freq[i]>0 and i>0): freq[i]-=1 else: return False return True

Hi, I'm Hyunsu ๐Ÿ‘‹

Profile Image

์•ˆ๋…•ํ•˜์„ธ์š”. ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ฃผํ˜„์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ฐœ๋ฐœ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ํ’๋ถ€ํ•˜๊ณ  ๊ฐ€์น˜ ์žˆ๋Š” ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜๋Š” ์ผ์— ๋ฟŒ๋“ฏํ•จ์„ ๋Š๋‚๋‹ˆ๋‹ค.

์˜ต์‹œ๋””์–ธ(Obsidian)์—์„œ ํ˜„์žฌ ๋ธ”๋กœ๊ทธ๋กœ ํ•˜๋‚˜์”ฉ ๊ธ€์„ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์— ์žˆ์–ด์š”. โ˜•๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป โ›ท

Github on ViewReach Me Out