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
  1. class Solution:
  2. def dividePlayers(self, skill: List[int]) -> int:
  3. freq=[0]*1001
  4. Sum, xMin, xMax=0, 1000, 1
  5. for x in skill:
  6. freq[x]+=1
  7. Sum+=x
  8. xMin=min(xMin, x)
  9. xMax=max(xMax, x)
  10. n_2=len(skill)//2
  11. if Sum%n_2!=0: return -1
  12. team_skill=Sum//n_2
  13.  
  14. chemi=0
  15. l, r=xMin, xMax
  16. while l<r:
  17. fL, fR=freq[l], freq[r]
  18. if l+r!=team_skill or fL!=fR: return -1
  19. chemi+=fL*l*r
  20. l+=1
  21. r-=1
  22. if l==r and l*2==team_skill:
  23. chemi+=freq[l]//2*l*l
  24. return chemi

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章