All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0846_hand_of_straights.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0801_0900.s0846_hand_of_straights

// #Medium #Array #Hash_Table #Sorting #Greedy
// #2023_03_29_Time_306_ms_(96.15%)_Space_39.4_MB_(46.15%)

class Solution {
    fun isNStraightHand(hand: IntArray, groupSize: Int): Boolean {
        val len = hand.size
        if (len % groupSize != 0) {
            return false
        }
        hand.sort()
        val map: MutableMap = HashMap()
        for (num in hand) {
            var cnt = map.getOrDefault(num, 0)
            map[num] = ++cnt
        }
        for (num in hand) {
            var cnt = map[num]!!
            if (cnt <= 0) {
                continue
            }
            map[num] = --cnt
            var loop = 1
            while (loop < groupSize) {
                var curCnt = map.getOrDefault(num + loop, 0)
                if (curCnt <= 0) {
                    return false
                }
                map[num + loop] = --curCnt
                ++loop
            }
        }
        return true
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy