Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

贪心算法,你入门了吗? #7067

Merged
merged 51 commits into from
May 13, 2020
Merged
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
afb007d
更新测试
chaingangway Mar 30, 2020
a261e82
Revert "更新测试"
chaingangway Mar 30, 2020
862cc57
8 个值得了解的树形数据结构 (#6804)
Amberlin1970 Mar 27, 2020
0632846
眼动跟踪和移动世界的最佳用户体验实践 (#6806)
Charlo-O Mar 27, 2020
a66e069
我并不讨厌箭头函数(#6610) (#6659)
TiaossuP Mar 27, 2020
28c79b4
掌握 JavaScript 面试:什么是纯函数? (#6828)
nia3y Mar 28, 2020
57bd9bd
fix:文章翻译问题 (#6833)
fireairforce Mar 28, 2020
95eec4a
用依赖注入来解耦你的代码 (#6823)
JalanJiang Mar 29, 2020
e4c0cac
Create generator-functions-in-javascript.md (#6841)
lsvih Mar 31, 2020
0465602
Create how-to-keep-your-dependencies-secure-and-up-to-date.md (#6843)
lsvih Mar 31, 2020
bc64328
Create deep-dive-into-react-fiber-internals.md (#6845)
lsvih Mar 31, 2020
e4759ff
Update deep-dive-into-react-fiber-internals.md
lsvih Mar 31, 2020
4e78a12
Create how-can-cloud-services-help-improve-your-businessess-efficienc…
lsvih Mar 31, 2020
ae7da28
Create should-you-learn-vim-as-a-developer-in-2020.md (#6849)
lsvih Mar 31, 2020
6738a6e
Go 发布新版 Protobuf API (#6827)
Mar 31, 2020
d884798
JSON.stringify() 的 5 个秘密特性 (#6793)
fireairforce Mar 31, 2020
d04aff3
Create high-speed-inserts-with-mysql.md (#6853)
lsvih Apr 1, 2020
85cc9e1
组合软件:书 (#6832)
fireairforce Apr 2, 2020
d8fc4d3
Create 6-best-javascript-frameworks-in-2020.md (#6861)
lsvih Apr 3, 2020
a8c74da
2020 年用各大前端框架构建的 RealWorld 应用对比 (#6851)
jasperxihuan Apr 4, 2020
77f1fb3
Create the-importance-of-why-docs.md (#6868)
lsvih Apr 6, 2020
8260ddc
不变性之道 (#6857)
nia3y Apr 6, 2020
548dfe6
Create polymorphic-react-components.md (#6870)
lsvih Apr 6, 2020
b8ba5b2
Create active-learning-in-machine-learning.md (#6872)
lsvih Apr 6, 2020
509ad30
Create kafka-vs-rabbitmq-why-use-kafka.md (#6874)
lsvih Apr 6, 2020
bc01bdc
Create i-built-an-app-that-uses-all-7-new-features-in-javascript-es20…
lsvih Apr 6, 2020
d278484
云服务如何帮助你提高业务效率? (#6859)
QinRoc Apr 7, 2020
d250ddb
NestJS 实现基本用户认证和会话 (#6731)
eizyc Apr 7, 2020
b1a3030
作为 2020 年的开发者,你应该学习 VIM 吗? (#6856)
chaingangway Apr 7, 2020
da77d74
MySQL 最佳实践—— 高效插入数据 (#6863)
Apr 8, 2020
5531303
Create combine-getting-started.md (#6883)
lsvih Apr 9, 2020
69ae6a6
Create how-to-be-a-good-remote-developer.md (#6885)
lsvih Apr 9, 2020
6c210ce
Create why-is-object-immutability-important.md (#6887)
lsvih Apr 9, 2020
e0b042e
Create what-on-earth-is-the-shadow-dom-and-why-it-matters.md
lsvih Apr 9, 2020
cc61cef
2020 年排名前 6 位的 JavaScript 框架 (#6867)
QinRoc Apr 9, 2020
53c6ba1
JavaScript 风格元素 (#6878)
febrainqu Apr 9, 2020
533bef9
怎样让依赖库保持安全和最新 (#6864)
chaingangway Apr 9, 2020
ff11c6e
Create 5-best-practices-to-prevent-git-leaks.md (#6894)
lsvih Apr 10, 2020
e3a066a
Create swiftui-3d-scroll-effect.md (#6896)
lsvih Apr 10, 2020
7153da6
Web 应用程序中的数据和 UI 分离 (#6794)
fireairforce Apr 10, 2020
e113faf
fix:typo (#6903)
fireairforce Apr 12, 2020
ce21ade
Merge remote-tracking branch 'upstream/master'
chaingangway Apr 15, 2020
2adbc9c
Merge remote-tracking branch 'upstream/master'
chaingangway Apr 22, 2020
b9ec935
Merge remote-tracking branch 'upstream/master'
chaingangway Apr 26, 2020
02177ea
Merge branch 'master' of https://github.com/xitu/gold-miner
chaingangway Apr 29, 2020
f16b66d
Merge remote-tracking branch 'upstream/master'
chaingangway May 2, 2020
a5be716
Merge remote-tracking branch 'upstream/master'
chaingangway May 6, 2020
fe5ce76
初译完成
chaingangway May 9, 2020
10281db
第一次校对
chaingangway May 11, 2020
0525960
校对完毕
chaingangway May 13, 2020
2d2229b
Merge branch 'master' into greedy
chaingangway May 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions article/2020/greedy-algorithms-101.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/article/2020/greedy-algorithms-101.md](https://github.com/xitu/gold-miner/blob/master/article/2020/greedy-algorithms-101.md)
> * 译者:[chaingangway](https://github.com/chaingangway)
> * 校对者:[PingHGao](https://github.com/PingHGao)
> * 校对者:[PingHGao](https://github.com/PingHGao)、[司徒公子](https://github.com/todaycoder001)


# 贪心算法,你入门了吗?

![](https://cdn-images-1.medium.com/max/2000/0*udmPDWYUmHDNJX5D)

贪心算法在大多数情况下都易于实现,在求解最优问题时,也是最常用的编码套路之一,而且它的资源消耗也比较低。

不过这个算法也有缺点,它不能保证每次都能找到最优解,有时候只能找到接近最优解的方案。不管怎样,在很多情况下,能找到接近最优解的方案已经绰绰有余了
不过这个算法也有缺点,它不能保证每次都能找到最优解,有时候只能找到接近最优解的方案。不管怎样,在很多情况下,接近最优解就足够了

这个算法一般是对规模为 “**n**” 的问题迭代 “**n**” 次,所以它的复杂度可能是 O(n),O(n × log(n)),但是不会超过 O(n²)。

Expand Down Expand Up @@ -43,7 +44,7 @@ public ArrayList greedy(ArrayList candidates) {
}
```

我首先给出一些在伪代码中用到的术语的定义
在解释代码之前,我先给出一些在伪代码中用到的术语的定义

1. **Candidates:** 所有可能的解集。它可以是任意的数据类型,但通常是可迭代的。在我们处理示例问题时,会加深对它的理解。现在请先记住结论 😁。
2. **Candidate:** 在解集中,我们当前选中的一个解。
Expand Down Expand Up @@ -168,7 +169,7 @@ ArrayList < Coin > greedySchema(ArrayList < Integer > values, int quantity) {

综上所述,即使对于简单的个人项目,贪心算法也能表现优异,它不需要你花费太多时间去思考,并且只消耗很少的资源。而且,使用贪心算法可以轻松解决很多面试问题。大多数时候,使用贪心或动态规划都可以满足内存和复杂度方面的要求,但这就是另一个话题了 😉。

感谢您的阅读,请多多评论哦 😄。
感谢您的阅读,欢迎评论 😄。

> 如果发现译文存在错误或其他需要改进的地方,欢迎到 [掘金翻译计划](https://github.com/xitu/gold-miner) 对译文进行修改并 PR,也可获得相应奖励积分。文章开头的 **本文永久链接** 即为本文在 GitHub 上的 MarkDown 链接。

Expand Down