贪心篇
用最少的箭引爆气球
int cmp(const void*a,const void*b)
{
return ((*((int**)a))[0] > (*((int**)b))[0]);
}
int findMinArrowShots(int** points, int pointsSize, int* pointsColSize)
{if(pointsSize==0)return 0;
qsort(points, pointsSize, sizeof(points[0]),cmp);
int ret=1;
for(int i=1;i<pointsSize;i++)
{
if(points[i-1][1]<points[i][0])
ret++;
else
{points[i][1]=points[i-1][1]>points[i][1]?points[i][1]:points[i-1][1];
}
}
return ret;
}
无重叠区间
int cmp(const void *a,const void *b)
{
return ((*((int**)a))[0] > (*((int**)b))[0]);
}
int eraseOverlapIntervals(int** intervals, int intervalsSize, int* intervalsColSize) {
qsort(intervals,intervalsSize,sizeof(intervals[0]),cmp);
int cnt=0;
for(int i=1;i<intervalsSize;i++)
{if(intervals[i-1][1]<=intervals[i][0])
{}
else
{
cnt++;
intervals[i][1]=intervals[i][1]>intervals[i-1][1]?intervals[i-1][1]:intervals[i][1];
}
}
return cnt;
}
划分字母区间
int* partitionLabels(char* s, int* returnSize) {
int hash[27]={0};
int len=strlen(s);
for(int i=0;i<len;i++)
{hash[s[i]-'a']=i;
}
int k=0;
* returnSize=len;
int*a=(int*)malloc(*returnSize*sizeof(int));
int left=0;int right=0;
for(int i=0;i<len;i++)
{right=right>hash[s[i]-'a']?right:hash[s[i]-'a'];
if(i==right)
{a[k++]=right-left+1;
left=i+1;}
}
* returnSize=k;
return a;
}
单调递增的数字
class Solution {
public:
int monotoneIncreasingDigits(int n) {
string st=to_string(n);
int flag=st.size();
for(int i=st.size()-1;i>0;i--)
{if(st[i-1]>st[i])
{st[i-1]--;
flag=i;}
}
for(int i=flag;i<=st.size()-1;i++)
{st[i]='9';
}
return stoi(st);
}
};
分发糖果
int candy(int* ratings, int ratingsSize) {
int*coke=(int*)malloc(sizeof(int)*ratingsSize);
for(int i=0;i<ratingsSize;i++)
{coke[i]=1;}
for(int i=1;i<ratingsSize;i++)
{if(ratings[i]>ratings[i-1])
{coke[i]=coke[i-1]+1;}
}
for(int i=ratingsSize-2;i>=0;i--)
{if(ratings[i]>ratings[i+1])
coke[i]=coke[i+1]+1>coke[i]?coke[i+1]+1:coke[i];
}
int num=0;
for(int i=0;i<ratingsSize;i++)
{num+=coke[i];}
return num;
free(coke);
}
摆动序列
int wiggleMaxLength(int* nums, int numsSize){
if(numsSize==1)return 1;
int right=0;
int left=0;
int ret=1;
for(int i=0;i<numsSize-1;i++)
{right=nums[i+1]-nums[i];
if((left<=0&&right>0)||(left>=0&&right<0))
{ret++;
left=right;
}
}
return ret;
}
分发饼干
void swap(int* p, int* q)
{
int tmp = *p;
*p = *q;
*q = tmp;
}
int findContentChildren(int* g, int gSize, int* s, int sSize) {
for(int i=0;i<gSize-1;i++)
{for(int j=0;j<gSize-1-i;j++)
{if(g[j+1]<g[j])
{ swap(&g[j + 1],&g[j]);}
}
}
for(int i=0;i<sSize-1;i++)
{for(int j=0;j<sSize-1-i;j++)
{if(s[j+1]<s[j])
{swap(&s[j + 1], &s[j]);}
}
}
int count=sSize-1;
int num=0;
for(int i=gSize-1;i>=0;i--)
{if(count>=0&&s[count]>=g[i])
{count--;
num++;
}
}
return num;
}
最大子数组和
int maxSubArray(int* nums, int numsSize) {
int num=INT_MIN;
int count=0;
for(int i=0;i<numsSize;i++)
{count+=nums[i];
if(count>num)num=count;
if(count<0)count=0;
}
return num;
}
买卖股票的最佳时机2
int maxProfit(int* prices, int pricesSize) {
int num=0;
for(int i=1;i<pricesSize;i++)
{int ret=prices[i]-prices[i-1]>0?prices[i]-prices[i-1]:0;
num+=ret;
}
return num;
}
跳跃游戏
bool canJump(int* nums, int numsSize) {
if(numsSize==1)return true;
int cover=0;
for(int i=0;i<=cover;i++)
{cover=i+nums[i]>cover?i+nums[i]:cover;
if(cover>=numsSize-1)
return true;
}
return false;
}
跳跃游戏2
int jump(int* nums, int numsSize) {
int next=0;
int cur=0;
int count=0;
for(int i=0;i<numsSize;i++)
{next=i+nums[i]>next?i+nums[i]:next;
if(cur==i)
{if(cur!=numsSize-1)
{count++;
cur=next;
if(cur>=numsSize-1)break;
}
else break;
}
}
return count;
}
k次取反后最大化数组和
void swap(int*p,int*q)
{int tmp=*p;
*p=*q;
*q=tmp;}
int largestSumAfterKNegations(int* nums, int numsSize, int k) {
for(int i=0;i<numsSize-1;i++)
{for(int j=0;j<numsSize-1-i;j++)
{if(abs(nums[j])<abs(nums[j+1]))
{swap(&nums[j],&nums[j+1]);}
}
}
for(int i=0;i<numsSize;i++)
{if(nums[i]<0&&k>0)
{nums[i]*=-1;
k--;}
}
if(k%2==1)nums[numsSize-1]*=-1;
int count=0;
for(int i=0;i<numsSize;i++)
{count+=nums[i];}
return count;
}
加油站
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
int sum=0;
int addsum=0;
int start=0;
for(int i=0;i<gasSize;i++)
{sum+=gas[i]-cost[i];
addsum+=gas[i]-cost[i];
if(sum<0)
{sum=0;
start=i+1;}
}
if(addsum<0)return -1;
return start;
}
柠檬水找零
bool lemonadeChange(int* bills, int billsSize) {
int five=0;
int ten=0;
int twenty=0;
for(int i=0;i<billsSize;i++)
{if(bills[i]==5)five++;
if(bills[i]==10)
{if(five>0)
{five--;ten++;}
else return false;
}
if(bills[i]==20)
{if(ten>0&&five>0)
{ten--;five--;twenty++;}
else if(five>=3)
{five-=3;twenty++;}
else
return false;
}
}
return true;
}