You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class22;
import java.util.HashMap;
// 本题测试链接 : https://leetcode.com/problems/tallest-billboard/
public class Code05_TallestBillboard {
public int tallestBillboard(int[] rods) {
// key 集合对的某个差
// value 满足差值为key的集合对中最好的一对较小集合的累加和
// 较大 -> value + key
HashMap<Integer, Integer> dp = new HashMap<>(), cur;
dp.put(0, 0);// 空集 和 空集
for (int num : rods) {
if (num != 0) {
// cur 内部数据完全和dp一样
cur = new HashMap<>(dp); // 考虑x之前的集合差值状况拷贝下来
for (int d : cur.keySet()) {
int diffMore = cur.get(d); // 最好的一对,较小集合的累加和
// x决定放入比较大的那个
dp.put(d + num, Math.max(diffMore, dp.getOrDefault(num + d, 0)));
// x决定放入比较小的那个
// 新的差值 Math.abs(x - d)
// 之前差值为Math.abs(x - d),的那一对,就要和这一对,决策一下
// 之前那一对较小集合的累加和diffXD
int diffXD = dp.getOrDefault(Math.abs(num - d), 0);
if (d >= num) { // x决定放入比较小的那个, 但是放入之后,没有超过这一对较大的那个
dp.put(d - num, Math.max(diffMore + num, diffXD));
} else { // x决定放入比较小的那个, 但是放入之后,没有超过这一对较大的那个
dp.put(num - d, Math.max(diffMore + d, diffXD));
}
}
}
}
return dp.get(0);
}
}