動態規劃

動態規劃題型的解題教學,10 題。先定義狀態,再寫轉移式——狀態定義錯了後面全錯,所以每篇都會先把狀態講清楚。

House Robber II 解題教學示意圖|LeetCode 213

House Robber II 解題教學:3 種解法完整比較|LeetCode 213 Best Practice

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system 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.

House Robber II 解題教學:3 種解法完整比較|LeetCode 213 Best Practice Read More »

Leetcode, 技術文章, , ,
Word Break II 解題教學示意圖|LeetCode 140

Word Break II 解題教學:3 種解法完整比較|LeetCode 140 Best Practice

Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order.

Note that the same word in the dictionary may be reused multiple times in the segmentation.

Word Break II 解題教學:3 種解法完整比較|LeetCode 140 Best Practice Read More »

Leetcode, 技術文章, , , ,
Word Break 解題教學示意圖|LeetCode 139

Word Break 解題教學:3 種解法完整比較|LeetCode 139 Best Practice

Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused multiple times in the segmentation

Word Break 解題教學:3 種解法完整比較|LeetCode 139 Best Practice Read More »

Leetcode, 技術文章, , , ,
Coin Change 解題教學示意圖|LeetCode 322

Coin Change 解題教學:3 種解法完整比較|LeetCode 322 Best Practice

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

You may assume that you have an infinite number of each kind of coin.

Coin Change 解題教學:3 種解法完整比較|LeetCode 322 Best Practice Read More »

Leetcode, 技術文章, , ,
Unique Paths 解題教學示意圖|LeetCode 62

Unique Paths 解題教學:3 種解法完整比較|LeetCode 62 Best Practice

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m – 1][n – 1]). The robot can only move either down or right at any point in time.

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The test cases are generated so that the answer will be less than or equal to 2 * 109.

Unique Paths 解題教學:3 種解法完整比較|LeetCode 62 Best Practice Read More »

Leetcode, 技術文章, , ,
Decode Ways 解題教學示意圖|LeetCode 91

Decode Ways 解題教學:3 種解法完整比較|LeetCode 91 Best Practice

A message containing letters from A-Z can be encoded into numbers using the following mapping:

‘A’ -> “1”
‘B’ -> “2”
…
‘Z’ -> “26”
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, “11106” can be mapped into:

“AAJF” with the grouping (1 1 10 6)
“KJF” with the grouping (11 10 6)
Note that the grouping (1 11 06) is invalid because “06” cannot be mapped into ‘F’ since “6” is different from “06”.

Given a string s containing only digits, return the number of ways to decode it.

The test cases are generated so that the answer fits in a 32-bit integer.

Decode Ways 解題教學:3 種解法完整比較|LeetCode 91 Best Practice Read More »

Leetcode, 技術文章, , ,
House Robber 解題教學示意圖|LeetCode 198

House Robber 解題教學:3 種解法完整比較|LeetCode 198 Best Practice

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.

House Robber 解題教學:3 種解法完整比較|LeetCode 198 Best Practice Read More »

Leetcode, 技術文章, , ,
Longest Common Subsequence 解題教學示意圖|LeetCode 1143

Longest Common Subsequence 解題教學:3 種解法完整比較|LeetCode 1143 Best Practice

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

For example, “ace” is a subsequence of “abcde”.
A common subsequence of two strings is a subsequence that is common to both strings.

Longest Common Subsequence 解題教學:3 種解法完整比較|LeetCode 1143 Best Practice Read More »

Leetcode, 技術文章, , ,
Climbing Stairs 解題教學示意圖|LeetCode 70

Climbing Stairs 解題教學:3 種解法完整比較|LeetCode 70 Best Practice

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?

Climbing Stairs 解題教學:3 種解法完整比較|LeetCode 70 Best Practice Read More »

Leetcode, 技術文章, , ,