算法 day4
【leetcode】最大宽度坡
class Solution { public: int maxWidthRamp(vector<int>& A) { stack<int> ss; int ans = 0; ss.push(0); for(int i=1;i<A.size();i++) { if(A[i]<A[ss.top()]) { ss.push(i); } } for(int i=A.size()-1;i>=0;i--) { while(!ss.empty()&&A[i]>=A[ss.top()]) { ans = max(i-ss.top(),ans); ss.pop(); } } return ans; } };
【leetcode】表现良好的最长时间段
class Solution { public: int longestWPI(vector<int>& hours) { int presum[hours.size()+1]; memset(presum,0,sizeof(presum)); presum[0] = 0; for(int i=0;i<hours.size();i++) { if(hours[i]>8)hours[i] = 1; else hours[i] = -1; presum[i+1] += hours[i]+presum[i]; } int ans = 0; //寻找presum[j] - presum[i]>0 最长的j-i stack<int>ss; ss.push(0); for(int i=1;i<hours.size()+1;i++) { if(presum[i]<presum[ss.top()]) { ss.push(i); } } for(int i=hours.size();i>=0;i--) { while(!ss.empty()&&presum[i]>presum[ss.top()]) { ans = max(ans,i-ss.top()); ss.pop(); } } return ans; } };
【leetcode】颜色分类
class Solution { public: void sortColors(vector<int>& nums) { int i = 0; int p0 = 0; int p1 = 0; for(i=0;i<nums.size();i++) { if(nums[i] == 0) { swap(nums[i],nums[p0]); if(p0<p1) swap(nums[p1],nums[i]); p0++; p1++; } else if(nums[i] == 1) { swap(nums[i],nums[p1]); p1++; } } } };
【leetcode】最大子序和
class Solution { public: int maxSubArray(vector<int>& nums) { int sum = 0; int ans = nums[0]; for(int i=0;i<nums.size();i++) { if(sum>0) sum += nums[i]; else sum = nums[i]; ans = max(ans,sum); } return ans; } };
【leetcode】接雨水
双指针
class Solution { public: int trap(vector<int>& height) { int n = height.size(); if(n==0) return 0; int l_max = height[0]; int r_max = height[n-1]; int ans = 0; int left = 0; int right = n-1; while(left<=right) { l_max = max(l_max,height[left]); r_max = max(r_max,height[right]); if(l_max<r_max) { ans += (l_max - height[left]); left++; } else { ans += (r_max - height[right]); right--; } } return ans; } };
【leetcode】反转字符串
class Solution { public: void reverseString(vector<char>& s) { int n = s.size(); int left = 0; int right = n-1; while(left<right) { char tmp = s[left]; s[left] = s[right]; s[right] = tmp; left++; right--; } } };
【leetcode】旋转图像
不能使用额外的空间 先转置后镜像对称
class Solution { public: void rotate(vector<vector<int>>& matrix) { for(int i=0;i<matrix.size();i++) { for(int j=i;j<matrix[i].size();j++) { swap(matrix[i][j],matrix[j][i]); } } for(int i=0;i<matrix.size();i++) { int left = 0; int right = matrix[i].size()-1; while(left<right) { swap(matrix[i][left],matrix[i][right]); left++; right--; } } } };
【leetcode】abc消消乐
class Solution { public: bool isValid(string s) { stack<char> ss; for(int i=0;i<s.length();i++) { if(s[i] == 'a') ss.push(s[i]); else if(!ss.empty()&&s[i] == 'b') { if(ss.top() == 'a') ss.push('b'); else return false; } else if(!ss.empty()&&s[i] == 'c') { if(ss.top() == 'b') { ss.pop(); ss.pop(); } else return false; } else return false; } if(!ss.empty()) return false; else return true; } };
【leetcode】零钱兑换1-2
class Solution { public: int coinChange(vector<int>& coins, int amount) { int dp[amount+1]; dp[0] = 0; for(int i=1;i<amount+1;i++) dp[i] = amount+1; for(int i=0;i<amount+1;i++) { for(int j=0;j<coins.size();j++) { if(i-coins[j]<0)continue; dp[i] = min(dp[i],dp[i-coins[j]]+1); //printf("i: %d %d\n",i,dp[i]); } } if(dp[amount] == amount+1) return -1; else return dp[amount]; } };
class Solution { public: int change(int amount, vector<int>& coins) { int dp[amount+1]; memset(dp,0,sizeof(dp)); dp[0] = 1; for(int j=0;j<coins.size();j++) { for(int i=coins[j];i<amount+1;i++) { dp[i] += dp[i-coins[j]]; } } return dp[amount]; } };
【leetcode】爬楼梯
class Solution { public: int climbStairs(int n) { int dp[n+1]; memset(dp,0,sizeof(dp)); int steps[2] = {1,2}; dp[0] = 1; for(int i=0;i<n+1;i++) { for(int j=0;j<2;j++) { if(i-steps[j] <0) continue; dp[i] += dp[i-steps[j]]; } } return dp[n]; } };