注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Input: array a, int b
Output: int c
Requirement: separate a into c groups while the max differences in every group is less than b
Examples: [1,2,3], the max difference is 3-1=2
解法:
Greedy algorithm
Sort array a,
add number n in a group if it satisfys the difference<b. Otherwise, add n to a new group
证明:
Induction:
Basic sst g groups among first m-2 element.
am+1 cannot stay in the same group with any element ak while k<=m-2. THerefore, there are at least g+1 groups.
That completes the proof
如果可以的话,帮我加个大米支持一下吧 |