
Dynamic Programming
文章平均质量分 80
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Distinct Subsequences II
Given a string s, returnthe number ofdistinct non-empty subsequencesofs. Since the answer may be very large, return itmodulo109+ 7.Asubsequenceof a string is a new string that is formed from the original string by deleting some (can be none) of ...原创 2022-04-06 14:20:01 · 177 阅读 · 0 评论 -
Two City Scheduling
A company is planning to interview2npeople. Given the arraycostswherecosts[i] = [aCosti, bCosti],the cost of flying theithperson to cityaisaCosti, and the cost of flying theithperson to citybisbCosti.Returnthe minimum cost to fly every p...原创 2022-04-06 10:38:12 · 181 阅读 · 0 评论 -
Coin Change 2
You are given an integer arraycoinsrepresenting coins of different denominations and an integeramountrepresenting a total amount of money.Returnthe number of combinations that make up that amount. If that amount of money cannot be made up by any com...原创 2022-04-04 02:29:15 · 394 阅读 · 0 评论 -
Best Time to Buy and Sell Stock with Transaction Fee
You are given an arraypriceswhereprices[i]is the price of a given stock on theithday, and an integerfeerepresenting a transaction fee.Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay ...原创 2022-03-21 11:46:35 · 235 阅读 · 0 评论 -
Stickers to Spell Word
We are givenndifferent types ofstickers. Each sticker has a lowercase English word on it.You would like to spell out the given stringtargetby cutting individual letters from your collection of stickers and rearranging them. You can use each sticker ...原创 2022-03-19 12:06:58 · 325 阅读 · 0 评论 -
Domino and Tromino Tiling
You have two types of tiles: a2 x 1domino shape and a tromino shape. You may rotate these shapes.Given an integer n, returnthe number of ways to tile an2 x nboard. Since the answer may be very large, return itmodulo109+ 7.In a tiling, every s...原创 2022-03-15 14:32:57 · 460 阅读 · 0 评论 -
Minimum Time to Remove All Cars Containing Illegal Goods
You are given a0-indexedbinary stringswhich represents a sequence of train cars.s[i] = '0'denotes that theithcar doesnotcontain illegal goods ands[i] = '1'denotes that theithcar does contain illegal goods.As the train conductor, you would l...原创 2022-02-06 12:49:42 · 307 阅读 · 0 评论 -
Maximum Number of Points with Cost
You are given anm x ninteger matrixpoints(0-indexed). Starting with0points, you want tomaximizethe number of points you can get from the matrix.To gain points, you must pick one cell ineach row. Picking the cell at coordinates(r, c)willaddpo...原创 2022-02-06 07:50:57 · 396 阅读 · 0 评论 -
Backpack III
DescriptionGivennkinds of items, and each kind of item has an infinite number available. Thei-thitem has sizeA[i]and valueV[i].Also given a backpack with sizem. What is the maximum value you can put into the backpack?You cannot divide item ...原创 2021-12-30 09:13:40 · 255 阅读 · 0 评论 -
Best Team With No Conflicts
You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is thesumof scores of all the players in the team.However, the basketball team is not allowed to ha..原创 2020-10-18 13:18:28 · 426 阅读 · 0 评论 -
Form Largest Integer With Digits That Add up to Target
Given an array of integerscostand an integertarget. Return themaximuminteger you can paintunder the following rules:The cost of painting adigit (i+1) is given bycost[i](0 indexed). The total cost used mustbe equal totarget. Integer does not ...原创 2020-09-21 02:39:21 · 275 阅读 · 0 评论 -
Maximum Product Subarray
Given an integer arraynums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation:[2,3] has the largest product 6.Example 2:Input: [-2,0,-1..原创 2020-05-28 14:31:58 · 157 阅读 · 0 评论 -
Maximum Non Negative Product in a Matrix
You are given arows x colsmatrixgrid.Initially, youare located at the top-leftcorner(0, 0),and in each step, you can onlymove rightordownin the matrix.Among all possible paths starting from the top-left corner(0, 0)and ending in the bottom-...原创 2020-09-20 13:20:39 · 271 阅读 · 0 评论 -
Number of Dice Rolls With Target Sum
You haveddice, and each die hasffaces numbered1, 2, ..., f.Return the number of possible ways (out offdtotal ways)modulo10^9 + 7to roll the dice so the sum of the face up numbers equalsta...原创 2020-04-28 09:41:33 · 268 阅读 · 0 评论 -
DP总结
区间型DP坐标性DP原创 2020-01-20 05:52:17 · 517 阅读 · 0 评论 -
Minimum Cost to Cut a Stick
Given a wooden stick of lengthnunits. The stick is labelled from0ton. For example, a stick of length6is labelled as follows:Given an integer arraycutswherecuts[i]denotes a position you should perform a cut at.You should perform the cuts in...原创 2020-08-24 13:11:38 · 408 阅读 · 0 评论 -
Stone Game V
There are several stonesarranged in a row, and each stone has an associatedvalue which is an integer given in the arraystoneValue.In each round of the game, Alice divides the row intotwo non-empty rows(i.e. left row and right row), then Bob calculat...原创 2020-08-23 13:20:07 · 308 阅读 · 0 评论 -
Number of Substrings With Only 1s
Given a binary strings(a string consisting only of '0' and '1's).Return the number of substrings with all characters 1's.Since the answermay be too large,return it modulo10^9 + 7.Example 1:Input: s = "0110111"Output: 9Explanation: There are...原创 2020-07-12 13:34:06 · 243 阅读 · 0 评论 -
Maximum Sum Circular Subarray
Given acirculararrayCof integers represented byA, find the maximum possible sum of a non-empty subarray ofC.Here, acirculararraymeans the end of the array connects to the beginning of the array. (Formally,C[i] = A[i]when0 <= i < A.leng...原创 2020-06-29 10:47:55 · 327 阅读 · 0 评论 -
Minimum Insertion Steps to Make a String Palindrome
Given a strings. In one step you can insert any character at any index of the string.Returnthe minimum number of stepsto makespalindrome.APalindrome Stringis one that reads the same backward as well as forward.Example 1:Input: s = "zzazz"O...原创 2020-06-29 04:00:24 · 507 阅读 · 0 评论 -
Maximum Profit in Job Scheduling
We havenjobs, where every jobis scheduled to be done fromstartTime[i]toendTime[i], obtaining a profitofprofit[i].You're given thestartTime,endTimeandprofitarrays,you need to output the maximum profit you can take such that there are no 2 j...原创 2020-06-28 05:46:15 · 377 阅读 · 0 评论 -
Best Time to Buy and Sell Stock IV
Say you have an array for which thei-thelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mostktransactions.Note:You may not engage in multiple transactions at the same time (ie, you ...原创 2020-06-27 13:31:24 · 261 阅读 · 0 评论 -
Tiling a Rectangle with the Fewest Squares
Given a rectangle of sizenxm, find the minimum number of integer-sided squares that tile the rectangle.Example 1:Input: n = 2, m = 3Output: 3Explanation: 3 squares are necessary to cover the rectangle.2(squares of 1x1)1(square of 2x2)Example ...原创 2020-06-26 14:03:29 · 350 阅读 · 0 评论 -
Stone Game III
Alice and Bob continue theirgames with piles of stones. There are several stonesarranged in a row, and each stone has an associatedvalue which is an integer given in the arraystoneValue.Alice and Bob take turns, withAlicestarting first. On each pla...原创 2020-06-26 12:17:59 · 197 阅读 · 0 评论 -
Odd Even Jump
You are given an integer arrayA. Fromsome starting index, you can make a series of jumps. The (1st, 3rd, 5th, ...)jumps in the series are calledodd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are calledeven numbered jumps.You ...原创 2020-06-25 11:56:59 · 287 阅读 · 0 评论 -
Minimum Distance to Type a Word Using Two Fingers
You have a keyboard layout as shown above in the XY plane, where each English uppercase letter is located at some coordinate, for example, the letterAis located at coordinate(0,0), the letterBis located at coordinate(0,1), the letterPis located a...原创 2020-06-25 07:12:47 · 268 阅读 · 0 评论 -
Encode String with Shortest Length
Given anon-emptystring, encode the string such that its encoded length is the shortest.The encoding rule is:k[encoded_string], where theencoded_stringinside the square brackets is being repeated exactlyktimes.Note:kwill be a positive integer ...原创 2020-06-23 06:41:59 · 256 阅读 · 0 评论 -
Maximum Points You Can Obtain from Cards
There are several cardsarranged in a row, and each card has an associated number of pointsThe points are given in the integer arraycardPoints.In one step, you can take one card from the beginning or from the end of the row. You have to take exactlyk...原创 2020-06-19 11:32:17 · 489 阅读 · 0 评论 -
Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows:I pick a number from1ton. You have to guess which number I picked.Every time you guess wrong, I'll tell you whether the number I picked is higher or lower.However, when you guess a particular n...原创 2020-06-17 02:06:19 · 212 阅读 · 0 评论 -
Cherry Pickup II
Given arows x colsmatrixgridrepresenting a field of cherries.Each cell ingridrepresents the number of cherries that you can collect.You have tworobots that can collect cherries for you, Robot #1 is located at the top-left corner (0,0) , and Robot...原创 2020-06-16 14:12:07 · 498 阅读 · 0 评论 -
New 21 Game
Alice plays the following game, loosely based on the card game "21".Alice starts with0points, and draws numbers while she has less thanKpoints. During each draw, she gains an integer number of points randomly from the range[1, W], whereWis an int...原创 2020-06-15 12:44:29 · 240 阅读 · 0 评论 -
Sentence Screen Fitting
Given arows x colsscreen and a sentence represented by a list ofnon-emptywords, findhow many timesthe given sentence can be fitted on the screen.Note:A word cannot be split into two lines. The order of words in the sentence must remain unchanged...原创 2020-06-15 09:57:48 · 246 阅读 · 0 评论 -
Longest String Chain
Given a list of words, each word consists of English lowercase letters.Let's sayword1is a predecessor ofword2if and only if we can add exactly one letter anywhere inword1to make it equal toword2. For example,"abc"is a predecessor of"abac".A...原创 2020-06-15 02:42:44 · 315 阅读 · 0 评论 -
Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.Given a list of stones' positions (in units) in sorted ascending order, d原创 2020-06-13 12:56:18 · 590 阅读 · 0 评论 -
N-th Tribonacci Number
The Tribonacci sequence Tnis defined as follows:T0= 0, T1= 1, T2= 1, and Tn+3= Tn+ Tn+1+ Tn+2for n >= 0.Givenn, return the value of Tn.Example 1:Input: n = 4Output: 4Explanation:T_3 = 0 + 1 + 1 = 2T_4 = 1 + 1 + 2 = 4Example 2:...原创 2020-06-11 14:04:40 · 204 阅读 · 0 评论 -
Stone Game II
Alexand Lee continue theirgames with piles of stones. There are a number ofpilesarranged in a row, and each pile has a positive integer number of stonespiles[i]. The objective of the game is to end with the moststones.Alex and Lee take turns, wi...原创 2020-06-11 07:47:35 · 252 阅读 · 0 评论 -
Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next原创 2020-06-11 04:03:45 · 212 阅读 · 0 评论 -
[leetcode] Stone Game
Alex and Lee play a game with piles of stones. There are an even number ofpilesarranged in a row, and each pile has a positive integer number of stonespiles[i].The objective of the game is to end with the moststones. The total number of stones is o...原创 2020-06-11 03:30:17 · 342 阅读 · 0 评论 -
Two City Scheduling
There are2Npeople a company is planning to interview. The cost of flying thei-th person to cityAiscosts[i][0], and the cost of flying thei-th person to cityBiscosts[i][1].Return the minimum cost to fly every person to a city such that exactlyN...原创 2020-06-08 08:45:50 · 267 阅读 · 0 评论 -
Longest Arithmetic Sequence
Given an arrayAof integers, return thelengthof the longest arithmetic subsequence inA.Recall that asubsequenceofAis a listA[i_1], A[i_2], ..., A[i_k]with0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequenceBisarith...原创 2020-06-03 12:14:07 · 207 阅读 · 0 评论