LEETCODE punch dp articles

Simple type

1

70. Climbing stairs

Suppose you are climbing stairs. It takes n steps to reach the top of the building.

You can climb 1 or 2 steps at a time. How many different ways can you climb to the top of the building?

Note: The given n is a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top of the building.

  1. Tier 1 + Tier 1
  2. 2nd order
    Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top of the building.
3. Level 1 + Level 1 + Level 1
4. Level 1 + Level 2
5. Level 2 + Level 1

class Solution {
    public int climbStairs(int n) {
        if(n<3)
        return n;
        int one = 1;
        int two = 2;
        int next = 0;
        for(int i=2;i<n;i++){
            next=one+two;
            one=two;
            two=next;
        }
        return next;
    }
}

2

198. Fighting at home

You are a professional thief and plan to steal houses along the street. There is a certain amount of cash hidden in each room. The only limiting factor that affects your theft is that the neighboring houses are equipped with an interconnected anti-theft system. If two neighboring houses are hacked into the same night, the system will automatically call .

Given a non-negative integer array representing the amount of money stored in each house, calculate the maximum amount you can steal without touching the alarm device.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Theft of house 1 (amount = 1), then theft of house 3 (amount = 3).
The maximum amount stolen = 1 + 3 = 4.
Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Theft of house 1 (amount = 2), theft of house 3 (amount = 9), and then theft of house 5 (amount = 1).
The maximum amount stolen = 2 + 9 + 1 = 12.

class Solution {
    public int rob(int[] nums) {
        if(nums.length==0)
        return 0;
        if(nums.length==1)
        return nums[0];
        int one = nums[0];
        int two = Math.max(nums[0],nums[1]);
        int next = Math.max(one,two);
        for(int i=2;i<nums.length;i++){
            next = Math.max(one+nums[i],two);
            one=two;
            two=next;
        }
        return next;
    }
}

3

213. Family Fight II

You are a professional thief and plan to steal houses along the street, each room contains a certain amount of cash. All houses in this place are in a circle, which means that the first house and the last house are next to each other. At the same time, the neighboring houses are equipped with an interconnected anti-theft system. If two neighboring houses are broken into by a thief on the same night, the system will automatically call the police.

Given a non-negative integer array representing the amount of money stored in each house, calculate the maximum amount you can steal without touching the alarm device.

Example 1:

Input: [2,3,2]
Output: 3
Explanation: You cannot steal house 1 (amount = 2) first, and then house 3 (amount = 2) because they are adjacent.
Example 2:

Input: [1,2,3,1]
Output: 4
Explanation: You can steal house 1 (amount = 1) first, and then house 3 (amount = 3).
The maximum amount stolen = 1 + 3 = 4.

class Solution {
    public int rob(int[] nums) {
        int len = nums.length;
        if(len==0)
        return 0;
        if(len==1)
        return nums[0];
        int one1=nums[0];
        int two1=Math.max(nums[0],nums[1]);
        int next1=Math.max(one1,two1);
        for(int i=2;i<len-1;i++){
            next1=Math.max(one1+nums[i],two1);
            one1=two1;
            two1=next1;
        }
        int next2=nums[1];
        if(len>3){
          int one2=nums[1];
        int two2=Math.max(nums[1],nums[2]);
         next2=Math.max(one2,two2);
        for(int i=3;i<len;i++){
            next2=Math.max(one2+nums[i],two2);
            one2=two2;
            two2=next2;
        }
        }
        return Math.max(next1,next2);

    }
}

4

Published 4 original articles · Likes0 · Visits15

Guess you like

Origin blog.csdn.net/smile_study1/article/details/105612688