comments | difficulty | edit_url | rating | source | tags | ||
---|---|---|---|---|---|---|---|
true |
困难 |
1927 |
第 26 场双周赛 Q4 |
|
给你一个整数数组 cost
和一个整数 target
。请你返回满足如下规则可以得到的 最大 整数:
- 给当前结果添加一个数位(
i + 1
)的成本为cost[i]
(cost
数组下标从 0 开始)。 - 总成本必须恰好等于
target
。 - 添加的数位中没有数字 0 。
由于答案可能会很大,请你以字符串形式返回。
如果按照上述要求无法得到任何整数,请你返回 "0" 。
示例 1:
输入:cost = [4,3,2,5,6,7,2,5,5], target = 9 输出:"7772" 解释:添加数位 '7' 的成本为 2 ,添加数位 '2' 的成本为 3 。所以 "7772" 的代价为 2*3+ 3*1 = 9 。 "977" 也是满足要求的数字,但 "7772" 是较大的数字。 数字 成本 1 -> 4 2 -> 3 3 -> 2 4 -> 5 5 -> 6 6 -> 7 7 -> 2 8 -> 5 9 -> 5
示例 2:
输入:cost = [7,6,5,5,5,6,8,7,8], target = 12 输出:"85" 解释:添加数位 '8' 的成本是 7 ,添加数位 '5' 的成本是 5 。"85" 的成本为 7 + 5 = 12 。
示例 3:
输入:cost = [2,4,6,2,4,6,4,4,4], target = 5 输出:"0" 解释:总成本是 target 的条件下,无法生成任何整数。
示例 4:
输入:cost = [6,10,15,40,40,40,40,40,40], target = 47 输出:"32211"
提示:
cost.length == 9
1 <= cost[i] <= 5000
1 <= target <= 5000
我们定义
考虑
如果
否则,我们需要从
具体地,在状态转移时,如果
最后,我们定义
时间复杂度
class Solution:
def largestNumber(self, cost: List[int], target: int) -> str:
f = [[-inf] * (target + 1) for _ in range(10)]
f[0][0] = 0
g = [[0] * (target + 1) for _ in range(10)]
for i, c in enumerate(cost, 1):
for j in range(target + 1):
if j < c or f[i][j - c] + 1 < f[i - 1][j]:
f[i][j] = f[i - 1][j]
g[i][j] = j
else:
f[i][j] = f[i][j - c] + 1
g[i][j] = j - c
if f[9][target] < 0:
return "0"
ans = []
i, j = 9, target
while i:
if j == g[i][j]:
i -= 1
else:
ans.append(str(i))
j = g[i][j]
return "".join(ans)
class Solution {
public String largestNumber(int[] cost, int target) {
final int inf = 1 << 30;
int[][] f = new int[10][target + 1];
int[][] g = new int[10][target + 1];
for (var e : f) {
Arrays.fill(e, -inf);
}
f[0][0] = 0;
for (int i = 1; i <= 9; ++i) {
int c = cost[i - 1];
for (int j = 0; j <= target; ++j) {
if (j < c || f[i][j - c] + 1 < f[i - 1][j]) {
f[i][j] = f[i - 1][j];
g[i][j] = j;
} else {
f[i][j] = f[i][j - c] + 1;
g[i][j] = j - c;
}
}
}
if (f[9][target] < 0) {
return "0";
}
StringBuilder sb = new StringBuilder();
for (int i = 9, j = target; i > 0;) {
if (j == g[i][j]) {
--i;
} else {
sb.append(i);
j = g[i][j];
}
}
return sb.toString();
}
}
class Solution {
public:
string largestNumber(vector<int>& cost, int target) {
const int inf = 1 << 30;
vector<vector<int>> f(10, vector<int>(target + 1, -inf));
vector<vector<int>> g(10, vector<int>(target + 1));
f[0][0] = 0;
for (int i = 1; i <= 9; ++i) {
int c = cost[i - 1];
for (int j = 0; j <= target; ++j) {
if (j < c || f[i][j - c] + 1 < f[i - 1][j]) {
f[i][j] = f[i - 1][j];
g[i][j] = j;
} else {
f[i][j] = f[i][j - c] + 1;
g[i][j] = j - c;
}
}
}
if (f[9][target] < 0) {
return "0";
}
string ans;
for (int i = 9, j = target; i;) {
if (g[i][j] == j) {
--i;
} else {
ans += '0' + i;
j = g[i][j];
}
}
return ans;
}
};
func largestNumber(cost []int, target int) string {
const inf = 1 << 30
f := make([][]int, 10)
g := make([][]int, 10)
for i := range f {
f[i] = make([]int, target+1)
g[i] = make([]int, target+1)
for j := range f[i] {
f[i][j] = -inf
}
}
f[0][0] = 0
for i := 1; i <= 9; i++ {
c := cost[i-1]
for j := 0; j <= target; j++ {
if j < c || f[i][j-c]+1 < f[i-1][j] {
f[i][j] = f[i-1][j]
g[i][j] = j
} else {
f[i][j] = f[i][j-c] + 1
g[i][j] = j - c
}
}
}
if f[9][target] < 0 {
return "0"
}
ans := []byte{}
for i, j := 9, target; i > 0; {
if g[i][j] == j {
i--
} else {
ans = append(ans, '0'+byte(i))
j = g[i][j]
}
}
return string(ans)
}
function largestNumber(cost: number[], target: number): string {
const inf = 1 << 30;
const f: number[][] = Array(10)
.fill(0)
.map(() => Array(target + 1).fill(-inf));
const g: number[][] = Array(10)
.fill(0)
.map(() => Array(target + 1).fill(0));
f[0][0] = 0;
for (let i = 1; i <= 9; ++i) {
const c = cost[i - 1];
for (let j = 0; j <= target; ++j) {
if (j < c || f[i][j - c] + 1 < f[i - 1][j]) {
f[i][j] = f[i - 1][j];
g[i][j] = j;
} else {
f[i][j] = f[i][j - c] + 1;
g[i][j] = j - c;
}
}
}
if (f[9][target] < 0) {
return '0';
}
const ans: number[] = [];
for (let i = 9, j = target; i; ) {
if (g[i][j] === j) {
--i;
} else {
ans.push(i);
j = g[i][j];
}
}
return ans.join('');
}