如题
限制了最多乘两人,难度就大大降低了。直接点就是排序后双指针判断就完事
public static int numRescueBoats(int[] people, int limit) {
Arrays.sort(people); //排序
int start = 0;
int stop = people.length - 1;
int count =0;
while(start<=stop) {
if(people[start]+people[stop]<=limit) { //和值小于限制 小值index加1后移 否则仅仅是大值减1 个数加1
start+=1;
}
stop-=1;
count+=1;
}
return count;
}
十分直观的做法了,可以用动态规划来做,很显然有点大题小用了。结果