herrDeng網內搜尋

自訂搜尋

Ads

2024年10月4日 星期五

貪婪計數Python C++解Leetcode 2491 Divide Players Into Teams of Equal Skill


貪婪計數Python C++解Leetcode 2491  Divide Players Into Teams of Equal Skill
貪婪演算+counting sort加上2-pointer這樣就完成打敗100趴的快速C++解答[Python code請進]
-----
Greedy algorithm+ counting sort + 2-pointer complete the quick C++ solution to defeat 100%.
[Python C++計數速解Leetcode 945 Minimum Increment to Make Array Unique]https://www.youtube.com/watch?v=tS-kunvQwcc
class Solution:
    def dividePlayers(self, skill: List[int]) -> int:
        freq=[0]*1001
        Sum, xMin, xMax=0, 1000, 1
        for x in skill:
            freq[x]+=1
            Sum+=x
            xMin=min(xMin, x)
            xMax=max(xMax, x)
        n_2=len(skill)//2
        if Sum%n_2!=0: return -1
        team_skill=Sum//n_2

        chemi=0
        l, r=xMin, xMax
        while l<r:
            fL, fR=freq[l], freq[r]
            if l+r!=team_skill or fL!=fR: return -1
            chemi+=fL*l*r
            l+=1
            r-=1
        if l==r and l*2==team_skill:
            chemi+=freq[l]//2*l*l
        return chemi
        

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章