Dynamic Programming(easy)

20. Valid Parentheses

Input: "{[]}"
Output: true

class Solution {
public:
    bool isValid(string s) {
        vector<char> stack;
        for(int i=0; i<s.length(); i++){
            if(s[i]=='(' || s[i]=='{' || s[i]=='[') stack.push_back(s[i]);
            else{
                if(stack.empty()) return false;
                char last = stack[stack.size()-1];
                if(match(last, s[i])) stack.pop_back();
                else return false;
            }  
        }
        return stack.empty();
    }
    bool match(char c1, char c2){
        if(c1=='(' && c2 ==')'){return true;}
        else if(c1=='{' && c2 =='}'){return true;}
        else if(c1=='[' && c2 ==']'){return true;}
        else{return false;}
    }
};

  

53. Maximum Subarray

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

int maxSubArray(vector<int>& nums) {
        int result=nums[0];
        int temp=nums[0];
        for(int i=1;i<nums.size();i++){
            temp=temp+nums[i]>nums[i]?temp+nums[i]:nums[i];
            result=result>temp?result:temp;
        }
        return result;
    }

 70. Climbing Stairs

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

int climbStairs(int n) {
       int all=0;
        int oneprev=1;
        int twoprev=1;
        if(n==0) return 0;
        if(n==1) return 1;
        if(n==2) return 2;
        for(;n>1;n--){
            all=oneprev+twoprev;
            twoprev=oneprev;
            oneprev=all;
        }
        return all;
    }

 

121. Best Time to Buy and Sell Stock

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6)

int maxProfit(vector<int>& prices) {
        if((prices.size())<=0||(prices.size())==1) return 0;
        int profit=0;
        int min=prices[0];
        for(int price:prices){
            if(price<min) min=price;
            else profit=max(price-min,profit);
        }
        return profit;
    }

198. House Robber

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.

int rob(vector<int>& nums) {
        int rob_prev=0;
        int notrob_prev=0;
        if(nums.size()==0) return 0;
        if(nums.size()==1) return nums[0];
        for(int money:nums){
            int temp=max(rob_prev,notrob_prev);
            rob_prev=notrob_prev+money;
            notrob_prev=temp;
        }
        return max(rob_prev,notrob_prev);
    }

 

746. Min Cost Climbing Stairs

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

 

int minCostClimbingStairs(vector<int>& cost) {
        int size = cost.size();
        int dp[size+1];
        dp[0] = 0;
        dp[1] = 0;
        for(int i = 2 ; i<size+1; i++)
            dp[i] = min((dp[i-1]+cost[i-1]),(dp[i-2]+cost[i-2]));
        return dp[size];
    }

 

1025. Divisor Game

 

Initially, there is a number N on the chalkboard.  On each player's turn, that player makes a move consisting of:

  • Choosing any x with 0 < x < N and N % x == 0.
  • Replacing the number N on the chalkboard with N - x.

Also, if a player cannot make a move, they lose the game.

Return True if and only if Alice wins the game, assuming both players play optimally.

 

return N%2==0;

  

posted @ 2019-07-27 14:49  weisman  阅读(157)  评论(0编辑  收藏  举报