chapter_greedy/fractional_knapsack_problem/ #636
Replies: 10 comments 1 reply
-
讲的很清楚,一遍就看懂了 |
Beta Was this translation helpful? Give feedback.
-
Sort 不算时间复杂度吗 |
Beta Was this translation helpful? Give feedback.
-
HELLO world |
Beta Was this translation helpful? Give feedback.
-
清晰易懂,感谢k神! |
Beta Was this translation helpful? Give feedback.
-
感谢K神的付出,文章内容质量确实非常好!但请原谅我的强迫症:“容量”给人的第一印象应该不是指“重量”,而是是指“体积” |
Beta Was this translation helpful? Give feedback.
-
通俗易懂(๑•̀ㅂ•́)و✧ |
Beta Was this translation helpful? Give feedback.
-
C# 的计算结果有出入,如果v, w是int类型, item.v / item.w 从高到低进行排序和预想的不一样,最后结果也不一样 |
Beta Was this translation helpful? Give feedback.
-
kotlin 的排序写错了吧 应该是 sortByDescending |
Beta Was this translation helpful? Give feedback.
-
图15-6数形结合秒变小学算术题。 |
Beta Was this translation helpful? Give feedback.
-
类似的leetCode算法题 class Solution {
public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {
// 创建物品列表,包含两个属性:总量、已使用量
Item[] items = new Item[capacity.length];
for (int i = 0; i < capacity.length; i++) {
items[i] = new Item(capacity[i], rocks[i]);
}
// 剩余量最少的背包排在最前面, 这样用最少石头舔更多背包
Arrays.sort(items, Comparator.comparingInt(item -> item.total - item.used));
int res = 0;
for (Item item : items) {
//获得背包剩余量
int left = item.total - item.used;
//已经满了的背包就不用添加了
if (left == 0) {
res++;
//放置的额外石头的left个数, 使其背包填满
}else if(left <= additionalRocks) {
additionalRocks -= left;
res++;
}
}
return res;
}
}
class Item {
int total; //总量
int used; //已使用量
public Item(int total, int used){
this.total = total;
this.used = used;
}
} |
Beta Was this translation helpful? Give feedback.
-
chapter_greedy/fractional_knapsack_problem/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/
Beta Was this translation helpful? Give feedback.
All reactions