貪婪計數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%.
[codes on Leetcode]https://leetcode.com/problems/divide-players-into-teams-of-equal-skill/solutions/5866450/greedy-sort-frequency-count-2-pointers-14ms-beats-100/
[Leetcode Playlist]https://www.youtube.com/watch?v=B1GQlUN08lk&list=PLYRlUBnWnd5IdDHk2BjqXwesydU17z_xk
[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
沒有留言:
張貼留言
HTML 編輯器