算法刷题--动态规划

Code 1 : climbing Stairs

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Example 1

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2

Input: n = 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

Solution

class Solution {
    
    
public:
    int climbStairs(int n) {
    
    
		int first=0;
		int second=0;
		int ans=1;
		for (int i=0;i<n;i++) {
    
    
			first=second;
			second=ans;
			ans=first+second;
		}
    }
};

n阶台阶的步数为第n-1阶和n-2阶的和。

Code 2 : House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Example 1

Input: nums = [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.

Example 2

Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.

Solution

class Solution {
    
    
public:
    int rob(vector<int>& nums) {
    
    
			vector<int> ans;
			ans.push_back(0);
			ans.push_back(nums[0]);
			for(int i=2;i<=nums.size();i++){
    
    
				ans.push_back(max(ans[i-1],nums[i-1]+ans[i-2]));
			}
			return ans[nums.size()];
    }
};

第n个的最大值=max(第n-1个最大值,第n-2个最大值+第n个数)

Code 3 : Triangle

Given a triangle array, return the minimum path sum from top to bottom.

For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.

Example 1

Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
Output: 11
Explanation: The triangle looks like:
   2
  3 4
 6 5 7
4 1 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).

Example 2

Input: triangle = [[-10]]
Output: -10

Solution

普通动态规划

class Solution {
    
    
public:
    int minimumTotal(vector<vector<int>>& triangle) {
    
    
		//由下到上遍历
    int n=triangle.size();
    for(int i=1;i<n;i++){
    
    
    	triangle[i][0]+=triangle[i-1][0];
    	for(int j=1;j<triangle[i].size()-1;j++){
    
    
    		triangle[i][j]+=min(triangle[i-1][j],triangle[i-1][j-1]);
		}
		triangle[i][triangle[i].size()-1]+=triangle[i-1][triangle[i-1].size()-1];
	}
	return *min_element(triangle[n-1].begin(),triangle[n-1].end());
    }
};

从上到下遍历,每次本节点的值更新为本节点的值加上上一层可以到达本节点的节点值中的最小值。注意对于首尾节点来说只有一个能够到达,考虑边界问题。最后要选择最后一层中最短的路径返回。

优化的动态规划

class Solution {
    
    
public:
    int minimumTotal(vector<vector<int>>& triangle) {
    
    
		//由下到上遍历
       
		int n=triangle.size();

		for(int i=n-2;i>=0;i--){
    
    
			for(int j=0;j<triangle[i].size();j++){
    
    
				triangle[i][j]
                    =min(triangle[i][j]+triangle[i+1][j],triangle[i][j]+triangle[i+1][j+1]);
			}
        }
		return triangle[0][0];
    }
};

这里是从下到上的更新三角形 ,一是巧妙地规避了边界问题,二是不用选择最小值返回,直接返回三角形的顶点。

猜你喜欢

转载自blog.csdn.net/yxyxxxyyyy/article/details/120301268