860. 柠檬水找零
这道题的思路十分简单,就是根据自己的钱进行找零,
什么时候贪心
——就是二十块找零,有两种策略,第一种是一张十块,一张五块,第二种是三张五块,因为五块的可使用率更高,所以说局部最优是第一种策略,然后再到第二种
bool lemonadeChange(int* bills, int billsSize) {int fiveCount = 0;int tenCount = 0;int i;for(i = 0; i < billsSize; ++i){switch(bills[i]){case 5:fiveCount++;break;case 10:if(fiveCount == 0)return false;fiveCount--;tenCount++;break;case 20:if(fiveCount > 0 && tenCount > 0){fiveCount--;tenCount--;}else if(fiveCount >= 3)fiveCount -= 3;elsereturn false;break;}} return true;
}
406. 根据身高重建队列
这里和分饼干一样,要进行分开贪心,是先对身高h进行排序,然后再对前面的人数k进行排序
——为什么先对身高进行排序
因为身高是按照从大到小排序,因为动的数字是后面的数字,一定比前面小的
k其实可以当作调动位置来看
/*** Return an array of arrays of size *returnSize.* The sizes of the arrays are returned as *returnColumnSizes array.* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().*/
int cmp(const void *p1, const void* p2){int *pp1 = *(int**)p1;int *pp2 = *(int**)p2;return pp1[0] == pp2[0] ? pp1[1] - pp2[1] : pp2[0] - pp1[0];
}
void moveBack(int** pepole, int peopleSize, int start, int end){int i;for(i = end; i > start; i--){pepole[i] = pepole[i - 1];}
}
int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes) {int i;qsort(people, peopleSize, sizeof(int*), cmp);for(i = 0; i < peopleSize; ++i){int position = people[i][1];int *temp = people[i];moveBack(people, peopleSize, position, i);people[position] = temp;}*returnSize = peopleSize;*returnColumnSizes = (int*)malloc(sizeof(int) * peopleSize);for(i = 0; i < peopleSize; ++i) {(*returnColumnSizes)[i] = 2;}return people;
}
452. 用最少数量的箭引爆气球
局部最优就是尽可能将重叠的气球放在一起
——对每个子数组的左孩子进行排序,按从小到大排
这个cmp该如何理解?
——其实是看返回值,如果返回值为正数,那么要进行交换位置,否则反之
箭的数量什么时候加加
——当i的左孩子大于i-1的右孩子时,说明两个气球并不重叠
如何理解重叠和这部分该如何处理?
——要每个重叠的气球两两互相重叠,才可以用一个箭引爆气球,所以每次将重叠的气球中的最小右孩子记录下来
int cmp(const void *a,const void *b)
{return ((*((int**)a))[0] > (*((int**)b))[0]);
} int findMinArrowShots(int** points, int pointsSize, int* pointsColSize){//将points数组作升序排序qsort(points, pointsSize, sizeof(points[0]),cmp);int arrowNum = 1;int i = 1;for(i = 1; i < pointsSize; i++) {//若前一个气球与当前气球不重叠,证明需要增加箭的数量if(points[i][0] > points[i-1][1])arrowNum++;else//若前一个气球与当前气球重叠,判断并更新最小的x_endpoints[i][1] = points[i][1] > points[i-1][1] ? points[i-1][1] : points[i][1];}return arrowNum;
}