chapter_greedy/greedy_algorithm/ #634
Replies: 14 comments 12 replies
-
能把最后几个典型问题再写写吗?Dijkstra 现在大二大三都在学和用的 |
Beta Was this translation helpful? Give feedback.
-
/**
* 根据key-value创建huffman编码树
* 和之前的一样不过加入的data
*
* @param counter 节点信息
*/
public void createCodingTree(HashMap<Byte, Integer> counter)
{
// 管理节点
ArrayList<HuffmanNode> huffmanNodes = new ArrayList<>();
// 创建节点
for (Byte key : counter.keySet())
{
huffmanNodes.add(new HuffmanNode(key, counter.get(key)));
}
// 最后会剩下一个根节点
while (huffmanNodes.size() > 1)
{
// 1.按照权值对节点排序
huffmanNodes.sort(new Comparator<HuffmanNode>()
{
@Override
public int compare(HuffmanNode o1, HuffmanNode o2)
{
return o1.weight - o2.weight;
}
});
// 2.构建二叉树
// 2.1.获取权值最小的两个节点
HuffmanNode first = huffmanNodes.get(0);
HuffmanNode second = huffmanNodes.get(1);
// 2.2.创建父节点
HuffmanNode parent = new HuffmanNode(first.weight + second.weight);
// 2.3.构建二叉树
parent.left = first;
parent.right = second;
// 3.删除两个节点并将父节点加入
huffmanNodes.remove(0);
huffmanNodes.remove(0);
huffmanNodes.add(parent);
}
this.root = huffmanNodes.get(0);
} |
Beta Was this translation helpful? Give feedback.
-
可以加一些有关贪心问题的证明吗,这真的令人很头疼 |
Beta Was this translation helpful? Give feedback.
-
硬币那个也可以整除实现吧 |
Beta Was this translation helpful? Give feedback.
-
时间复杂度为 上面的“提升了一个数量级”应该是“降低了一个数量级”吧 |
Beta Was this translation helpful? Give feedback.
-
有一个小疑问,贪心策略是每次选取最接近目标金额的硬币,要达成这样的操作步骤,需要coins是有序的吧 |
Beta Was this translation helpful? Give feedback.
-
用贪心解零钱兑换是不是存在问题? |
Beta Was this translation helpful? Give feedback.
-
无法适配LaTeX |
Beta Was this translation helpful? Give feedback.
-
正例 :在该硬币组合下,给定任意 ,贪心算法都可以找到最优解。 挺好奇人民币也正巧是这些面额的纸币是不是和这个有关系,就是令人找零/凑钱的时候永远可以从大额到小额一次考虑。 |
Beta Was this translation helpful? Give feedback.
-
老学究用 图一乐,莫喷哈哈。 |
Beta Was this translation helpful? Give feedback.
-
k神请问贪心算法再解决最小里程数时我应该如何的优化代码量呢是Python哦 |
Beta Was this translation helpful? Give feedback.
-
想问一下,最后这四种算法,分治,回溯,动态,贪心这么排版有什么特殊的想法吗?以前我记得都先学贪心的。这四种算法的学习顺序有没有什么讲究? |
Beta Was this translation helpful? Give feedback.
-
运行零钱兑换的代码,有些测试用例通不过,有人遇到嘛? |
Beta Was this translation helpful? Give feedback.
-
chapter_greedy/greedy_algorithm/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_greedy/greedy_algorithm/
Beta Was this translation helpful? Give feedback.
All reactions