Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
higboys
javascript-algorithms
提交
1c1b244d
J
javascript-algorithms
项目概览
higboys
/
javascript-algorithms
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
javascript-algorithms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1c1b244d
编写于
12月 23, 2020
作者:
O
Oleksii Trekhleb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add dynamic programming version.
上级
79cf9eb4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
73 addition
and
0 deletion
+73
-0
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dpBestTimeToBuySellStocks.test.js
...y-sell-stocks/__tests__/dpBestTimeToBuySellStocks.test.js
+48
-0
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/dpBestTimeToBuySellStocks.js
...best-time-to-buy-sell-stocks/dpBestTimeToBuySellStocks.js
+25
-0
未找到文件。
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/__tests__/dpBestTimeToBuySellStocks.test.js
0 → 100644
浏览文件 @
1c1b244d
import
dpBestTimeToBuySellStocks
from
'
../dpBestTimeToBuySellStocks
'
;
describe
(
'
dpBestTimeToBuySellStocks
'
,
()
=>
{
it
(
'
should find the best time to buy and sell stocks
'
,
()
=>
{
let
visit
;
expect
(
dpBestTimeToBuySellStocks
([
1
,
5
])).
toEqual
(
4
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
([
1
],
visit
)).
toEqual
(
0
);
expect
(
visit
).
toHaveBeenCalledTimes
(
1
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
([
1
,
5
],
visit
)).
toEqual
(
4
);
expect
(
visit
).
toHaveBeenCalledTimes
(
2
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
([
5
,
1
],
visit
)).
toEqual
(
0
);
expect
(
visit
).
toHaveBeenCalledTimes
(
2
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
([
1
,
5
,
10
],
visit
)).
toEqual
(
9
);
expect
(
visit
).
toHaveBeenCalledTimes
(
3
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
([
10
,
1
,
5
,
20
,
15
,
21
],
visit
)).
toEqual
(
25
);
expect
(
visit
).
toHaveBeenCalledTimes
(
6
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
([
7
,
1
,
5
,
3
,
6
,
4
],
visit
)).
toEqual
(
7
);
expect
(
visit
).
toHaveBeenCalledTimes
(
6
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
([
1
,
2
,
3
,
4
,
5
],
visit
)).
toEqual
(
4
);
expect
(
visit
).
toHaveBeenCalledTimes
(
5
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
([
7
,
6
,
4
,
3
,
1
],
visit
)).
toEqual
(
0
);
expect
(
visit
).
toHaveBeenCalledTimes
(
5
);
visit
=
jest
.
fn
();
expect
(
dpBestTimeToBuySellStocks
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
,
15
,
16
,
17
,
18
,
19
,
20
],
visit
,
)).
toEqual
(
19
);
expect
(
visit
).
toHaveBeenCalledTimes
(
20
);
});
});
src/algorithms/uncategorized/best-time-to-buy-sell-stocks/dpBestTimeToBuySellStocks.js
0 → 100644
浏览文件 @
1c1b244d
/**
* Finds the maximum profit from selling and buying the stocks.
* DYNAMIC PROGRAMMING APPROACH.
*
* @param {number[]} prices - Array of stock prices, i.e. [7, 6, 4, 3, 1]
* @param {function(): void} visit - Visiting callback to calculate the number of iterations.
* @return {number} - The maximum profit
*/
const
dpBestTimeToBuySellStocks
=
(
prices
,
visit
=
()
=>
{})
=>
{
visit
();
let
lastBuy
=
-
prices
[
0
];
let
lastSold
=
0
;
for
(
let
day
=
1
;
day
<
prices
.
length
;
day
+=
1
)
{
visit
();
const
curBuy
=
Math
.
max
(
lastBuy
,
lastSold
-
prices
[
day
]);
const
curSold
=
Math
.
max
(
lastSold
,
lastBuy
+
prices
[
day
]);
lastBuy
=
curBuy
;
lastSold
=
curSold
;
}
return
lastSold
;
};
export
default
dpBestTimeToBuySellStocks
;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录