bool lemonadeChange(int* bills, int billsSize){
int five = 0;
int ten = 0;
for (int i=0; i<billsSize; i++)
{
if (bills[i] == 5) five++;
if (bills[i] == 10)
{
if (five > 0)
{
ten++;
five--;
}else {
return false;
}
}
if (bills[i] == 20)
{
if (ten > 0 && five > 0)
{
ten--;
five--;
}else if (five >= 3)
{
five -= 3;
}else{
return false;
}
}
}
return true;
}
bool lemonadeChange(int* bills, int billsSize){
// 分别记录五元、十元的数量(二十元不用记录,因为不会用到20元找零)
int fiveCount = 0; int tenCount = 0;
int i;
for(i = 0; i < billsSize; ++i) {
// 分情况讨论每位顾客的付款
switch(bills[i]) {
// 情况一:直接收款五元
case 5:
fiveCount++;
break;
// 情况二:收款十元
case 10:
// 若没有五元找零,返回false
if(fiveCount == 0)
return false;
// 收款十元并找零五元
fiveCount--;
tenCount++;
break;
// 情况三:收款二十元
case 20:
// 若可以,优先用十元和五元找零(因为十元只能找零20,所以需要尽量用掉。而5元能找零十元和二十元)
if(fiveCount > 0 && tenCount > 0) {
fiveCount--;
tenCount--;
}
// 若没有十元,但是有三张五元。用三张五元找零
else if(fiveCount >= 3)
fiveCount-=3;
// 无法找开,返回false
else
return false;
break;
}
}
// 全部可以找开,返回true
return true;
}
int cmp(const void* p1, const void* p2)
{
int *pp1 = *(int**)p1;
int *pp2 = *(int**)p2;
// 若身高相同,则按照k从小到大排列
// 若身高不同,按身高从大到小排列
return pp1[0] == pp2[0] ? pp1[1] - pp2[1] : pp2[0] - pp1[0];
}
int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes){
qsort(people, peopleSize, sizeof(int*), cmp);
for (int i=0; i<peopleSize; i++)
{
int position = people[i][1];
int *temp = people[i];
for (int j = i; j > position; j--)
{
people[j] = people[j-1];
}
people[position] = temp;
}
*returnSize = peopleSize;
*returnColumnSizes = (int*)malloc(sizeof(int) * peopleSize);
for (int i = 0; i < peopleSize; i++)
{
(*returnColumnSizes)[i] = 2;
}
return people;
}
int cmp(const void *p1, const void *p2) {
int *pp1 = *(int**)p1;
int *pp2 = *(int**)p2;
// 若身高相同,则按照k从小到大排列
// 若身高不同,按身高从大到小排列
return pp1[0] == pp2[0] ? pp1[1] - pp2[1] : pp2[0] - pp1[0];
}
// 将start与end中间的元素都后移一位
// start为将要新插入元素的位置
void moveBack(int **people, int peopleSize, int start, int end) {
int i;
for(i = end; i > start; i--) {
people[i] = people[i-1];
}
}
int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes){
int i;
// 将people按身高从大到小排列(若身高相同,按k从小到大排列)
qsort(people, peopleSize, sizeof(int*), cmp);
for(i = 0; i < peopleSize; ++i) {
// people[i]要插入的位置
int position = people[i][1];
int *temp = people[i];
// 将position到i中间的元素后移一位
// 注:因为已经排好序,position不会比i大。(举例:排序后people最后一位元素最小,其可能的k最大值为peopleSize-2,小于此时的i)
moveBack(people, peopleSize, position, i);
// 将temp放置到position处
people[position] = temp;
}
// 设置返回二维数组的大小以及里面每个一维数组的长度
*returnSize = peopleSize;
*returnColumnSizes = (int*)malloc(sizeof(int) * peopleSize);
for(i = 0; i < peopleSize; ++i) {
(*returnColumnSizes)[i] = 2;
}
return people;
}
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;
for (int i=1; i<pointsSize; i++)
{
if (points[i][0] > points[i-1][1])
{
arrowNum++;
}else {
points[i][1] = points[i][1] > points[i-1][1] ? points[i-1][1] : points[i][1];
}
}
return arrowNum;
}
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_end
points[i][1] = points[i][1] > points[i-1][1] ? points[i-1][1] : points[i][1];
}
return arrowNum;
}