今天1月1号刚进去LeetCode里面看见的哈哈哈,年末放假回家才开始的刷题之路。兄弟们一起加油呀!
int findContentChildren(vector<int>& g, vector<int>& s) {
int res=0;
sort(g.begin(),g.end());
sort(s.begin(),s.end());
for(int i=0,j=0;i<s.size()&&j<g.size();i++){
if(s[i]>=g[j]){
res++;
j++;
}
}
return res;
}
***376. 摆动序列***
int wiggleMaxLength(vector<int>& nums) {
if(nums.size()==1||(nums.size()==2&&nums[0]==nums[1])) return 1;
if(nums.size()==2) return 2;
int res=0;
for(int i=0;i<nums.size();i++){
if(!i) res++;
if(i&&nums[i]>nums[i-1]){
while(i+1<nums.size()&&nums[i]<=nums[i+1]) i++;
res++;
}
if(i&&nums[i]<nums[i-1]){
while(i+1<nums.size()&&nums[i]>=nums[i+1]) i++;
res++;
}
}
return res;
}
int maxSubArray(vector<int>& nums) {
int res=INT32_MIN;
int count=0;
for(int i=0;i<nums.size();i++){
count+=nums[i];
res=max(res,count);
if(count<0) count=0;
}
return res;
}
int maxProfit(vector<int>& prices) {
int res=0;
for(int i=0;i<prices.size();i++){
if(!i&&i+1<prices.size()&&prices[i+1]>=prices[i]){
int start=prices[i];
while(i+1<prices.size()&&prices[i+1]>prices[i]) i++;
int end=prices[i];
res+=(end-start);
}
if(i&&i+1<prices.size()&&prices[i-1]>=prices[i]&&prices[i+1]>prices[i]){
int start=prices[i];
while(i+1<prices.size()&&prices[i+1]>prices[i]) i++;
int end=prices[i];
res+=(end-start);
}
}
return res;
}
bool canJump(vector<int>& nums) {
int loc=0;
for(int i=0;i<nums.size();){
int num=0;
if(i+nums[i]>=nums.size()-1) return true;
for(int j=i;j<=nums[i]+i&&j<nums.size();j++){
if(nums[j]==0&&j<nums.size()-1) continue;
if(nums[j]!=0&&num<nums[j]+j){
num=nums[j]+j;
loc=j;
}
}
if(loc==i&&nums[loc]==0) return false;
else if(loc==i) loc=nums[i]+i;
i=loc;
}
return false;
}
int jump(vector<int>& nums) {
if(nums.size()==1) return 0;
int loc=0;
int res=0;
for(int i=0;i<nums.size();){
int num=0;
if(nums[i]+i>=nums.size()-1){
res++;
break;
}
for(int j=i;j<=nums[i]+i&&j<nums.size();j++){
if(num<nums[j]+j){
num=nums[j]+j;
loc=j;
}
}
i=loc;
res++;
}
return res;
}
int largestSumAfterKNegations(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
int loc=0;int sum=0;
for(int i=0;i<nums.size();i++){
if(nums[i]<0) loc=i;
if(k&&nums[i]<0){
nums[i]=-nums[i];
k--;
}
sum+=nums[i];
}
if(k&&k%2==1){
if(loc+1<nums.size()) sum-=2*min(nums[loc],nums[loc+1]);
else if(loc+1>=nums.size()) sum-=2*nums[loc];
}
return sum;
}
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int sum =0;
int loc =0;
int total =0;
for(int i=0;i<gas.size();i++){
total +=(gas[i]-cost[i]);
sum +=(gas[i]-cost[i]);
if(sum <0) {
loc=i+1;
sum =0;
}
}
if(total <0) return -1;
// if(curSum ==0) return 0;
return loc;
}
int candy(vector<int>& nums) {
int candy[nums.size()];
int res=0;
for(int i=0;i<nums.size();i++) candy[i]=1;
for(int i=0;i<nums.size();i++){
if(i+1<nums.size()&&nums[i+1]>nums[i]&&candy[i+1]<=candy[i]) candy[i+1]=candy[i]+1;
}
for(int i=nums.size()-1;i>=0;i--){
if(i>=1&&nums[i-1]>nums[i]&&candy[i-1]<=candy[i]) candy[i-1]=candy[i]+1;
}
for(int i=0;i<nums.size();i++) res+=candy[i];
return res;
}
bool lemonadeChange(vector<int>& bills) {
unordered_map<int,int> map;
for(int i=0;i<bills.size();i++){
if(bills[i]==5){
map[5]++;
continue;
}
if(bills[i]==10){
if(!map[5]) return false;
else{
map[5]--;
map[10]++;
}
}
if(bills[i]==20){
if(map[10]>=1&&map[5]>=1) {
map[10]--;map[5]--;
}else if(map[10]==0&&map[5]>=3) {
map[5]-=3;
}else {
return false;
}
}
}
return true;
}
static bool cmp(vector<int>& a,vector<int>& b){
if(a[0]==b[0]) return a[1]<b[1];
return a[0]>b[0];
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(),people.end(),cmp);
list<vector<int>> que;
for(int i=0;i<people.size();i++){
int loc=people[i][1];
auto cc=que.begin();
while(loc--) cc++;
que.insert(cc,people[i]);
}
return vector<vector<int>>(que.begin(),que.end());
}
static bool cmp(vector<int> &a,vector<int>& b){
return a[0]<b[0];
}
int findMinArrowShots(vector<vector<int>>& points) {
if(points.size()==0) return 0;
sort(points.begin(),points.end(),cmp);
int res=1;
int loc=0;
for(int i=1;i<points.size();i++){
if(points[i][0]>points[i-1][1]) res++;
else points[i][1]=min(points[i-1][1],points[i][1]);
}
return res;
}
static bool cmp(vector<int>& a,vector<int>&b){
if(a[0]==b[0]) return a[1]<b[1];
else return a[0]<b[0];
}
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if(intervals.size()<=1) return 0;
sort(intervals.begin(),intervals.end(),cmp);
int res=0;
for(int i=1;i<intervals.size();i++){
if(intervals[i-1][1]>intervals[i][0]){
res++;
intervals[i][1]=min(intervals[i-1][1],intervals[i][1]);
}
}
return res;
}
unordered_map<char,vector<int>>map;
vector<int> partitionLabels(string s) {
vector<int> res;
for(int i=0;i<s.size();i++){
if(map[s[i]].size()!=0) continue;
map[s[i]].push_back(i);
for(int j=s.size()-1;j>=i;j--){
if(s[j]==s[i]){
map[s[i]].push_back(j);
break;
}
}
}
int maxr=map[s[0]][1];
int start=0;
for(int i=0;i<s.size();i++){
if(map[s[i]][1]>maxr) maxr=map[s[i]][1];
if(i==maxr){
res.push_back(i-start+1);
// cout<<start<<" "<<i<<endl;
start=i+1;
}
}
return res;
}
这个题目比较卡常数,不能用erase去删除元素不然最后一个数据会超时!
static bool cmp(vector<int>& a,vector<int>& b){
if(a[0]==b[0]) return a[1]<b[1];
return a[0]<b[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> res;
vector<int>st(intervals.size(),1);
if(intervals.size()==1) return intervals;
sort(intervals.begin(),intervals.end(),cmp);
for(int i=1;i<intervals.size();i++){
if(intervals[i][0]<=intervals[i-1][1]){
intervals[i][0]=intervals[i-1][0];
intervals[i][1]=max(intervals[i-1][1],intervals[i][1]);
st[i-1]=0;
}
}
for(int i=0;i<intervals.size();i++)
if(st[i]==1) res.push_back(intervals[i]);
return res;
}
int monotoneIncreasingDigits(int n) {
int res=0;
string num=to_string(n);
int loc=-1;
for(int i=num.size()-1;i>=1;i--){
if(num[i-1]>num[i]){
num[i-1]--;
loc=i;
}
}
if(loc!=-1)
for(int i=loc;i<num.size();i++) num[i]='9';
return stoi(num);
}
int maxProfit(vector<int>& prices, int fee) {
int res=0;
int minPrice=0x3f3f3f3f;
if(prices.size()==1) return 0;
for(int i=0;i<prices.size();i++){
if(prices[i]<minPrice) minPrice=prices[i];
if(prices[i]-minPrice<=fee) continue;
if(prices[i]-minPrice>fee){
res+=prices[i]-minPrice-fee;
minPrice=prices[i]-fee;
}
}
return res;
}
int res=0;
int minCameraCover(TreeNode* root) {
if(root->left==NULL&&root->right==NULL) return 1;
if(xx(root)==0) res++;
return res;
}
int xx(TreeNode* root){
if(root==NULL) return 2;
int l=xx(root->left);
int r=xx(root->right);
if(l==2&&r==2) return 0;
if(l==0||r==0){
res++;
return 1;
}
if(l==1||r==1) return 2;
return 0;
}