1449. The sum of digital costs is the maximum number of target values
One question per day on June 12, 2021
Title Description
Give you an array of integers cost And an integer target . Please return the maximum integer that can be obtained by satisfying the following rules: Add a digit to the current result( i + 1)The cost of is cost[i] (cost Array subscripts start at 0). The total cost must be exactly equal to target . There is no digit 0 in the added digit. Since the answer may be large, please return it as a string. If you cannot get any integers according to the above requirements, please return "0" . Example 1: Input: cost = [4,3,2,5,6,7,2,5,5], target = 9 Output:"7772" Explanation: add digits '7' The cost of is 2, add digits '2' The cost of is 3. therefore "7772" The price is 2*3+ 3*1 = 9 . "977" It is also a number that meets the requirements, but "7772" Is a larger number. number cost 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5 Example 2: Input: cost = [7,6,5,5,5,6,8,7,8], target = 12 Output:"85" Explanation: add digits '8' The cost of is 7, adding digits '5' The cost of is 5."85" The cost of is 7 + 5 = 12 . Example 3: Input: cost = [2,4,6,2,4,6,4,4,4], target = 5 Output:"0" Explanation: the total cost is target Cannot generate any integers. Example 4: Input: cost = [6,10,15,40,40,40,40,40,40], target = 47 Output:"32211"
Source: LeetCode
Link: https://leetcode-cn.com/problems/form-largest-integer-with-digits-that-add-up-to-target
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.
thinking
Basically, it has been done all morning, and finally it has been done. How do you feel? Without that sense of joy, it may take too much time, but you can still learn something
At first, I thought like this:
First of all, it is clear that dynamic programming, and a number can be selected many times, completely knapsack
Define dp[i][j] as the current I-bit, which is called the maximum integer exactly j, and the state transition equation can be written easily
dp[i][j] = max(dp[i - 1][j], current value * multiple + dp[i][j - cost[i]])
Then, when writing code, I found that the current multiple is not easy to express, so I thought of traversing from back to front. First add the large number to the front, and then transfer it with dp[i][j] = max(dp[i - 1][j], dp[i][j- cost[i]] * 10 + i)
Then I wrote the general code of the two-dimensional complete knapsack I practiced two days ago, and reported an error. I found that this writing can not guarantee that it is exactly equal to the current cost j
Then I think about how to make the cost exactly equal. I think that all values are assigned to a very small number during initialization. Only j = 0 is assigned to 0, that is, dp[0] = 0; In this way, when transferring, if the previous transfer point is found to be a small number, the transfer will not occur, because the cost cannot be exactly equal to j, and 0 or other numbers can be transferred
This is first written in one-dimensional, execute the code, and pass through the first few use cases, but transfer to two-dimensional. I found it impossible. I studied how to assign initial values to two-dimensional for a while, and gave up two-dimensional without studying it
Then I submitted it and found that [1,1,1,1,1,1,1,3,2] 10 had not been passed. I didn't quite understand it, so I typed out the dp array, looked at it, deduced it carefully, and found that it was out of bounds, and 10 7 exceeded integer MAX_ VALUE;
Then use long to indicate that it is still out of bounds
No way, we can only use the string to judge. When judging the string, the initial value is changed from a small number to "- 1"
String comparison, 1 The number with large length is 2 Left to right character sizes of the same length
Then run and find that there is 0 in front of the output number. I soon think that when the transfer starts, there is 0 in front of the spliced string
Note that when transferring, the length of - 1 is 2. If there is no special treatment, the final result will be - 1
The final code is as follows:
class Solution { public String largestNumber(int[] cost, int target) { //At first glance, it is also the problem of dynamic programming //dp[i][j] is added to bit I, and then the cost is the maximum integer of j //dp[i][j] = max(dp[i - 1][j], res * 10 + dp[i][j - cost[i]]) //The maximum value indicates the current value * 10 + the previous maximum value //Each one can be selected many times, completely String[] dp = new String[target + 1]; //Initialization, dp[i][0] = 0; Arrays.fill(dp, "-1"); dp[0] = "0"; for(int i = 8; i >= 0; i--) { for (int j = cost[i]; j <= target; j++) { if(dp[j - cost[i]] != "-1"){ StringBuffer sb = new StringBuffer(dp[j - cost[i]]); if(sb.charAt(0) == '0') sb.deleteCharAt(0); sb.append((i + 1) + ""); int len = dp[j].length(); if(dp[j] == "-1") len = 1; if(sb.length() > len) dp[j] = sb.toString(); else if(sb.length() < len) dp[j] = dp[j]; else if(dp[j].compareTo(sb.toString()) < 0) dp[j] = sb.toString(); } } } if(dp[target] == "-1") return "0"; return String.valueOf(dp[target]); } }
After reading the solution of the following question, it is divided into two steps dp, the first step is to deal with the length, and the second step is to deal with the value according to the length. It's very ingenious and learned
class Solution { public String largestNumber(int[] cost, int t) { int[] f = new int[t + 1]; Arrays.fill(f, Integer.MIN_VALUE); f[0] = 0; //Maximum length at pretreatment for (int i = 1; i <= 9; i++) { int u = cost[i - 1]; for (int j = u; j <= t; j++) { f[j] = Math.max(f[j], f[j - u] + 1); } } if (f[t] < 0) return "0"; String ans = ""; for (int i = 9, j = t; i >= 1; i--) { int u = cost[i - 1]; while (j >= u && f[j] == f[j - u] + 1) { ans += String.valueOf(i); j -= u; } } return ans; } } Author: AC_OIer Link: https://leetcode-cn.com/problems/form-largest-integer-with-digits-that-add-up-to-target/solution/gong-shui-san-xie-fen-liang-bu-kao-lu-we-uy4y/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.
This question continues to arouse my thinking about the most and the right. Because the question of profit plan the day before yesterday, I thought I understood the difference between the most and the right. As a result, today's question confused me again; Then I went back and looked at the question. I found that the reason why the question can distinguish the most and just by the initial value is that when it is transferred, it is directly transferred from the previous number, and there is no transfer limit of max and min
Because of this limitation, this problem cannot be distinguished only by the initial value and needs special treatment!!
But is this right? It still has something to do with the complete backpack. I haven't thought too clearly!
Of course, this is only my understanding at this stage. I believe this dynamic month will make me fully understand!!!!!