【算法心得】When data range not large, try Bucket sort

https://leetcode.com/problems/maximum-number-of-coins-you-can-get/description/?envType=daily-question&envId=2023-11-24

I solve this problem by sorting piles first, and choose piles for(let i=1;i<(piles.length/3)*2;i+=2)

but:
【算法心得】When data range not large, try Bucket sort_第1张图片
o(≧口≦)o

Problem must lies in sort, nlogn is too slow

在这里插入图片描述
Notice that piles[i]'s range is surprisingly small, we can declare an int A[10000] without any mental burden

so why just try bucket sort (bucket size==1), and the Time Complexity can reach astonishing O(n)!

We dont even have to assemble the elements to be an array, and we just use the bucket, scan from the no.10000 to no.0, to solve this problem

你可能感兴趣的:(算法,算法)