卜东坡算法-2021秋作业2-动态规划
2021秋作业2-动态规划
1. Money robbing
A robber is 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 system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
- Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
- What if all houses are arranged in a circle?
解法:
|
|
2. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si , Sj ) of elements in this subset satisfies: Si%Sj = 0 or Sj%Si = 0.
Please return the largest size of the subset.
Note: Si%Sj = 0 means that Si is divisible by Sj .
解法:
|
|
3. Unique Binary Search Trees
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?
Explanation: Given n = 3, there are a total of 5 unique BST’s:
|
|
4. Word Break
Given a string S and a dictionary of words, determine if the string S can be segmented into a space-separated sequence of one or more dictionary words.
Note: Each word in the dictionary may be reused multiple times in the segmentation. You can return TRUE if the string S is empty.
|
|
5. Distinct Sequences
Given two strings S and T, return the number of distinct subsequences of S which equals T.
A string’s subsequence is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the remaining characters’ relative positions. (i.e., ”ACE” is a subsequence of ”ABCDE” while ”AEC” is not).
Example 1:
|
|
Example 2:
|
|
|
|
6. Triangle
Description
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 +1on the next row.
Input
Line1:
The height of the triangle, and 1 <= triangle.height <= 2001<=triangl**e.heigh**t<=200.
Line2:
All the elements in the triangle, and split by some spaces(for each element,-10^4<= triangle[i][j] <=10^4). We are sure that the number of the elements satisfy:
Output
Print the minimum path sum from top to bottom.
Sample Input 1
|
|
Sample Output 1
|
|
Sample Input 2
|
|
Sample Output 2
|
|
Hint
Input:
4
2 3 4 6 5 7 4 1 8 3
Output:
11
Explanation: The triangle looks like:
2
34
657
4183
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (bolded above)
|
|
7. Maximum Alternating Subsequence Sum
Description
The alternating sum of a 0-indexed array is defined as the sum of the elements at even indices minus the sum of the elements at odd indices.
For example, the alternating sum of [4,2,5,3] is (4 + 5) - (2 + 3) = 4.
Given an array nums, return the maximum alternating sum of any subsequence of nums (after reindexing the elements of the subsequence).
A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements’ relative order.For example, [2,7,4] is a subsequence of [4,2,3,7,2,1,4] (the underlined elements), while [2,4,2] is not.
Input
Anarray.
1 <= nums.length <= 10^51<=num**s.lengt**h<=105
1 <= nums[i] <= 10^51<=num**s[i]<=105
Output
Maximum alternating sum.
Sample Input 1
|
|
Sample Output 1
|
|
Sample Input 2
|
|
Sample Output 2
|
|
Sample Input 3
|
|
Sample Output 3
|
|
|
|