Design a data structure to store the strings’ count with the ability to return the strings with minimum and maximum counts.
Implement the AllOne class:
AllOne() Initializes the object of the data structure.
inc(String key) Increments the count of the string key by 1. If key does not exist in the data structure, insert it with count 1. dec(String key) Decrements the count of the string key by 1. If the count of key is 0 after the decrement, remove it from the data structure. It is guaranteed that key exists in the data structure before the decrement.
getMaxKey() Returns one of the keys with the maximal count. If no element exists, return an empty string "". getMinKey() Returns one of the keys with the minimum count. If no element exists, return an empty string "".
Constraints:1 <= key.length <= 10 key consists of lowercase English letters.
It is guaranteed that for each call to dec, key is existing in the data structure. At most `5 104calls will be made toinc,dec,getMaxKey, andgetMinKey`.
dp[i][j] = max{dp[i][m] + nums[i]×nums[m]×nums[j] + dp[m][j]}, i < m < j
多状态型DP(DP有多个终止状态)
1 2 3 4 5
dp = dp if s[i] = '0' = dp + 1 if s[i] = '1' dp2 = min(dp2 + 1, dp + 1) if s[i] = '0' = min(dp2, dp) if s[i] = '1'
解题步骤:
DP五部曲
[初始化]dp数组n+1
dp初始值也就是基本条件
[实现]定义状态和递归式
[答案] 如dp[-1]
[优化]是否可以用滚动内存优化空间
单序列或匹配型DP模板
实现注意事项(5点):
[多1][初始化]初始化矩阵,dp是原长度加1。因为令边界计算方便. 遍历从多少开始,取决于前状态多少个,若递归式含dp[i-1],从1开始,如果dp[i-2]就从2开始.数值型DP,如硬币个数DP长度是数值amount,不是数组长度。Python用dp = [[0 for _ in range(M)] for _ in range(N)], M为col数,再row数
longest, res = dp[-1][-1], '' while m >= 0and n >= 0: if dp[m - 1][n] == longest: m -= 1 elif dp[m][n - 1] == longest: n -= 1 else: res += text1[m - 1] longest -= 1 m -= 1 n -= 1
Given an integer array nums, return the length of the longest strictly increasing subsequence.
A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
Example 1:
Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
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.
Example 1:
Input: text1 = “abcde”, text2 = “ace” Output: 3 Explanation: The longest common subsequence is “ace” and its length is 3.
Example 2:
Input: text1 = “abc”, text2 = “abc” Output: 3 Explanation: The longest common subsequence is “abc” and its length is 3.
Example 3:
Input: text1 = “abc”, text2 = “def” Output: 0 Explanation: There is no such common subsequence, so the result is 0.
Constraints:1 <= text1.length, text2.length <= 1000 * text1 and text2 consist of only lowercase English characters.
longest, res = dp[-1][-1], '' while m >= 0and n >= 0: if dp[m - 1][n] == longest: m -= 1 elif dp[m][n - 1] == longest: n -= 1 else: res += text1[m - 1] longest -= 1 m -= 1 n -= 1 return res[::-1]
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.