動態規劃
文章平均质量分 53
0
ZustinLau
I am currently studying Economics at the Chinese University of Hong Kong and I am interested in data structure and algorithms.
展开
-
1201. Ugly Number III && 264. Ugly Number ll
First, we can enumerate different numbers as a candidate answer as long as there are n - 1's ugly numbers before it.So, right now we have three conditions here, the first one is all the ugly numbers should be smaller than the answer. The second condition i原创 2022-06-23 19:11:34 · 181 阅读 · 0 评论 -
1653. Minimum Deletions to Make String Balanced
Same problem with monotone fliping coin.the machanics is like this, if you encounter b in side some a, you need to flip it, so you store the times you encounter the b.but if you find out what if the whole array is b, or a, or a or b is monopoly in this arr原创 2022-06-06 06:00:27 · 76 阅读 · 0 评论 -
1105. Filling Bookcase Shelves(dp)
we set dp[i] as the min height of putting previous i's book.how can we update, we just need to take the previous book shelves book to put them in the new shelve (to ensure it wouldnt exceed the length of the book shelve), and we take the max value of book原创 2022-06-06 05:29:07 · 83 阅读 · 0 评论 -
1749. Maximum Absolute Sum of Any Subarray(DP)
Normally, you would try a O(N^2) brute force.but actually, you know we are finding a interval sum and take a absolute value.interval sum i, j = prefixsum[j] - prefixsum[i - 1];so if we want the max interval sum, we need to find the minimum prefixSum and ma原创 2022-06-06 17:55:32 · 53 阅读 · 0 评论 -
1911. Maximum Alternating Subsequence Sum(DP)(bb)
First, you would notice this question is quite annoying because odd and even indices would change while you choose different subsequences.And you would notice that other indices could be even or odd in a subsequence except for the first index.Here we w原创 2022-06-06 05:12:46 · 82 阅读 · 0 评论 -
1372. Longest ZigZag Path in a Binary Tree(DFS/DP)
simple dfs problem, if we change the direction, we add 1 to path length, otherwise reset the path length.of course, we need to know the last node is left or right to judge we have adjusted the direction or not.The hardest thing of this question is to think原创 2022-06-06 05:02:36 · 87 阅读 · 0 评论 -
1690. Stone Game VII (prefixSum and dp)
because we always need to query the interval sum. So, we make this operation to become O(1) using prefixSum.[1, 2, 3, 4] ^ ^ i i+1if we pick up the left stone, dp[i + 1][j], if we pick up the right stone, dp[i][j - 1].as both players would maximize th原创 2022-06-06 04:58:09 · 89 阅读 · 0 评论 -
1035. Uncrossed Lines(LCS based dp)
we know the line would not cross, so we can just directly use LCS's templete.原创 2022-06-06 04:53:23 · 69 阅读 · 0 评论 -
518. Coin Change 2 (knapsack problem)
Classical complete knapsack problem.every value we can pick inifite time as long as it didnt exceed the size of our bag.we would use the 1d array to implement it.we define, dp[i] is using previous i's coin how many ways to make up a certain amount.the outt原创 2022-06-06 04:51:10 · 84 阅读 · 0 评论 -
2110. Number of Smooth Descent Periods of a Stock (counting dp)
counting ~!原创 2022-06-06 04:42:00 · 54 阅读 · 0 评论 -
1048. Longest String Chain(DP)
nothing special LIS原创 2022-06-06 04:26:05 · 56 阅读 · 0 评论 -
P6146 [USACO20FEB]Help Yourself G (計數問題)
題目定義了兩個東西.第一個是 併, 併是指所有被至少一個線段覆蓋的點的集合.第二個是複雜度, 複雜度是指這些並所形成的連通塊數目.如何理解複雜度呢?你可以想象為 一開始有兩條不想交的線段, 他們明顯沒有形成一個連通塊, 而他們兩個自己就是連通塊, 所以開始複雜度為2, 如果這個時候加入一條線段, 他跟其中一個線段相交了, 那麼他們就屬於一個連通塊了. 所以總共還是只有兩個連通塊.那麼如何求出n條線段所有子集的複雜度之和呢?每個線段本身就是一個子集.我們把所有線段按左端點排個序, 這樣我們可以一個一個線段 慢原创 2022-06-03 16:07:53 · 45 阅读 · 0 评论 -
CF1673C - Palindrome Basis
背包問題原创 2022-06-03 14:44:23 · 113 阅读 · 0 评论 -
926. Flip String to Monotone Increasing(DP)
This is a typical dp question.Monotonic increasing only has three patterns, 000111, 0000 and 1111.In brute force, we just need to calculate a prefix sum for 0 and 1.We use a mid-point to find the minimum flip, the left-hand side of the mid-point shou原创 2022-05-28 05:21:33 · 62 阅读 · 0 评论 -
131. Palindrome Partitioning(DFS/DP)
We would dfs on the search tree and find all the patterns, we would also write a check method to check if the string is palindrome or not, if it is we would add it to the subset.class Solution {public: bool check(int l, int r, string &s){ .原创 2022-05-28 03:03:03 · 47 阅读 · 0 评论 -
1493. Longest Subarray of 1‘s After Deleting One Element(DP/sliding window)
Pretty straightforward!We use sliding window and when the 0 appears in that window more than once, we would break the window, otherwise we would keep update the answer.class Solution {public: int longestSubarray(vector<int>& nums) {原创 2022-05-28 02:28:52 · 55 阅读 · 0 评论 -
1578. Minimum Time to Make Rope Colorful(DP)
Pretty straightforward!We want to make the array colorful which does not allow the same color to appear consecutively.If the same color appears consecutively, we only want to reserve the biggest cost one and deleteothers, because it can guarantee our原创 2022-05-28 02:25:09 · 176 阅读 · 0 评论 -
1947. Maximum Compatibility Score Sum(DFS/DP)
This question is quite straightforward, Just need to find all combinations and calculate the score using dfs.This is a brute force, and remember the dfs method we need to pass the value by reference otherwise it will use the extra time to copy the vector原创 2022-05-28 02:13:36 · 100 阅读 · 0 评论 -
516. Longest Palindromic Subsequence (DP)
Here we have two solutions.Both are DP.The first solution is quite smart and difficult to think of.Basically, you just need to reverse the string, and then find the longest common subsequence.The reason is a palindromic string is the same as it is原创 2022-05-28 01:55:55 · 72 阅读 · 0 评论 -
1031. Maximum Sum of Two Non-Overlapping Subarrays(sliding window + DP)
This is an implicit sliding window problem. It is almost the same as buying and selling the stock 3.The idea is that we gonna set up a middle line, and the line would keep moving forward. When is it moving, we would see if there is any better option for原创 2022-05-28 00:59:22 · 72 阅读 · 0 评论 -
712. Minimum ASCII Delete Sum for Two Strings(DP)
This is classical matching DP. We can directly use the longest common subsequence solution's idea to solve this question.The question asks us to delete some characters to make two strings the same. Actually, it is the same as finding the longest common s原创 2022-05-27 23:37:59 · 64 阅读 · 0 评论 -
CF189A (Complete Knapsack DP)
Polycarpus has a ribbon, its length isn. He wants to cut the ribbon in a way that fulfils the following two conditions:After the cutting each ribbon piece should have lengtha,borc. After the cutting the number of ribbon pieces should be maximum.H...原创 2022-05-20 11:16:08 · 118 阅读 · 0 评论 -
241. Different Ways to Add Parentheses(DPS + DP)
Given a stringexpressionof numbers and operators, returnall possible results from computing all the different possible ways to group numbers and operators. You may return the answer inany order.Adding parentheses is like choosing an operator and ca...原创 2022-05-20 07:14:43 · 96 阅读 · 0 评论 -
1664. Ways to Make a Fair Array (Logic & DP)
Interesting problem原创 2022-05-20 06:10:30 · 93 阅读 · 0 评论 -
983. Minimum Cost For Tickets (DP)
You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer arraydays. Each day is an integer from1to365.Train tickets are sold inthree different ways:a1-daypass is sold forc...原创 2022-05-19 06:15:57 · 67 阅读 · 0 评论 -
1706. Where Will the Ball Fall (DP, IQ)
You have a 2-Dgridof sizem x nrepresenting a box, and you havenballs. The box is open on the top and bottom sides.Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.A b...原创 2022-05-19 02:21:57 · 203 阅读 · 0 评论 -
1130. Minimum Cost Tree From Leaf Values(DP)
Given an arrayarrof positive integers, consider all binary trees such that:Each node has either0or2children; The values ofarrcorrespond to the values of eachleafin an in-order traversal of the tree. The value of each non-leaf node is equal t...原创 2022-05-18 20:06:40 · 132 阅读 · 0 评论 -
1395. Count Number of Teams(dp)
There arensoldiers standing in a line. Each soldier is assigned auniqueratingvalue.You have to form a team of 3 soldiers amongst them under the following rules:Choose 3 soldiers with index (i,j,k) with rating (rating[i],rating[j],rating[k])....原创 2022-05-18 17:43:33 · 78 阅读 · 0 评论 -
1140. Stone Game II (DP, DFS)
The DFS solution is quite intuitive and more like an implementation.Try to think in this way, every time you can either choose X piles, and this X would affect the latter decisions. And as both Alice and Bobs would make decisions optimally. We need to re原创 2022-05-17 08:20:07 · 147 阅读 · 0 评论 -
1884. Egg Drop With 2 Eggs and N Floors & 887. Super Egg Drop (DP)
You are giventwo identicaleggs and you have access to a building withnfloors labeled from1ton.You know that there exists a floorfwhere0 <= f <= nsuch that any egg dropped at a floorhigherthanfwillbreak, and any egg droppedat or be...原创 2022-05-13 03:19:33 · 187 阅读 · 0 评论 -
1638. Count Substrings That Differ by One Character
A split is calledgoodif you can splitsinto two non-empty stringssleftandsrightwhere their concatenation is equal tos(i.e.,sleft+ sright= s) and the number of distinct letters insleftandsrightis the same.Returnthe number ofgood splits...原创 2022-05-13 02:12:54 · 152 阅读 · 0 评论 -
1277. Count Square Submatrices with All Ones
Given am * nmatrix of ones and zeros, return how manysquaresubmatrices have all ones.First, we can analyze a vector to find the pattern.the total amount of rectangle this vector can form is 1 + 1 + 2 + 3.1d matrix is easy to find the number ...原创 2022-05-11 18:25:17 · 110 阅读 · 0 评论 -
1641. Count Sorted Vowel Strings(DFS / DP)
DFS solutionActually, my first dfs solution time limit was exceeded.My thought is to do simple combinations dfs, just dfs on the search tree.class Solution {public: int x = 0; char arr[5] = {'a', 'e', 'i', 'o', 'u'}; void dfs(string原创 2022-05-11 17:47:13 · 117 阅读 · 0 评论 -
894. All Possible Full Binary Trees (DFS)
This question is very similar to other generation problems. For example, generate the perfect parentheses. The only different thing is this time we need to generate a node and tree.The hardest thing about using backtracing to generate different trees is原创 2022-05-11 17:19:17 · 99 阅读 · 0 评论 -
5. Longest Palindromic Substring (DP / Two Pointer)
Dp solutionThis question can use a very simple DP to solve. Let's set the DP[i][j] to represent an interval i to j form a palindromic string.There are only two situations we need to initialize, the first one is when i == j, it would be a single letter,原创 2022-05-11 16:43:43 · 118 阅读 · 0 评论 -
53. Maximum Subarray (sliding window)
We know the subarray is a consecutive part of an array and this leads us to think about the two-pointer and sliding windows sort of algorithms.As the question asks to find the maximum sum of the subarray. We consider using sliding windows which is a powe原创 2022-05-11 04:28:46 · 88 阅读 · 0 评论 -
746. Min Cost Climbing Stairs (dp)
You are given an integer arraycostwherecost[i]is the cost ofithstep on a staircase. Once you pay the cost, you can either climb one or two steps.You can either start from the step with index0, or the step with index1.Returnthe minimum cost to...原创 2022-05-11 03:33:05 · 100 阅读 · 0 评论 -
119. Pascal‘s Triangle II (DP)
Given an integerrowIndex, return therowIndexth(0-indexed) row of thePascal's triangle.Input: rowIndex = 3Output: [1,3,3,1]Input: rowIndex = 0Output: [1]The challenge of this question is to use O(rowIndex) memory to finish the question.Fro...原创 2022-05-11 02:07:34 · 77 阅读 · 0 评论 -
118. Pascal‘s Triangle (DP)
Given an integernumRows, return the first numRows ofPascal's triangle.we will use the top to bottom DP to do this question. First, from observation, we know that each row's first element and last element are 1.Other elements follow a rule that dp[...原创 2022-05-10 20:00:13 · 87 阅读 · 0 评论 -
2266. Count Number of Texts ( Simple DP)
In order toadda letter, Alice has topressthe key of the corresponding digititimes, whereiis the position of the letter in the key.For example, to add the letter's', Alice has to press'7'four times. Similarly, to add the letter'k', Alice has ...原创 2022-05-09 05:04:05 · 136 阅读 · 0 评论