Skip to content

Commit 4c4cfd0

Browse files
author
EternalChildren
committed
increase three solution
1 parent 01381fc commit 4c4cfd0

File tree

9 files changed

+161
-0
lines changed

9 files changed

+161
-0
lines changed

maxProfit/rust/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

maxProfit/rust/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
authors = ["EternalChildren <colin.zcn@outlook.com>"]
5+
edition = "2018"
6+
7+
[dependencies]

maxProfit/rust/src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// 0ms 2.2MB
2+
pub fn max_profit_fastly(prices: Vec<i32>) -> i32 {
3+
if prices.len() < 2 {
4+
return 0;
5+
}
6+
let mut ans = 0;
7+
let mut min = std::i32::MAX;
8+
9+
for p in prices {
10+
let profit = std::cmp::max(p - min, 0);
11+
if profit > ans {
12+
ans = profit;
13+
}
14+
min = std::cmp::min(min, p);
15+
}
16+
ans
17+
}
18+
19+
// 72ms 2.3MB
20+
pub fn max_profit(prices: Vec<i32>) -> i32 {
21+
if prices.len() < 2 {
22+
return 0;
23+
}
24+
let mut ans = 0;
25+
26+
for i in 0..prices.len() {
27+
for j in i + 1..prices.len() {
28+
let dif = prices[j] - prices[i];
29+
if dif > ans {
30+
ans = dif;
31+
}
32+
}
33+
}
34+
println!("res is {}", ans);
35+
ans
36+
}
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
41+
#[test]
42+
fn it_works() {
43+
let vec = vec![7, 1, 5, 3, 6, 4];
44+
assert_eq!(max_profit(vec), 5);
45+
let vec1 = vec![7, 6, 4, 3, 1];
46+
assert_eq!(max_profit(vec1), 0);
47+
}
48+
49+
#[test]
50+
fn quickly() {
51+
let vec = vec![7, 1, 5, 3, 6, 4];
52+
assert_eq!(max_profit_fastly(vec), 5);
53+
let vec1 = vec![7, 6, 4, 3, 1];
54+
assert_eq!(max_profit_fastly(vec1), 0);
55+
}
56+
}

maxProfitII/rust/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

maxProfitII/rust/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
authors = ["EternalChildren <colin.zcn@outlook.com>"]
5+
edition = "2018"
6+
7+
[dependencies]

maxProfitII/rust/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 0ms 2.2MB
2+
pub fn max_profit(prices: Vec<i32>) -> i32 {
3+
if prices.len() < 2 {
4+
return 0;
5+
}
6+
let mut ans = 0;
7+
for i in 0..prices.len() - 1 {
8+
if prices[i] < prices[i + 1] {
9+
ans += prices[i + 1] - prices[i];
10+
}
11+
}
12+
println!("ans is {}", ans);
13+
ans
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn it_works() {
22+
let vec = vec![7, 1, 5, 3, 6, 4];
23+
assert_eq!(max_profit(vec), 7);
24+
let vec1 = vec![1, 2, 3, 4, 5];
25+
assert_eq!(max_profit(vec1), 4);
26+
let vec2 = vec![7, 6, 4, 3, 1];
27+
assert_eq!(max_profit(vec2), 0);
28+
}
29+
}

maxProfitIII/rust/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

maxProfitIII/rust/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
authors = ["EternalChildren <colin.zcn@outlook.com>"]
5+
edition = "2018"
6+
7+
[dependencies]

maxProfitIII/rust/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 0ms 2.2MB 大佬代码 暂时还不太明白
2+
pub fn max_profit(prices: Vec<i32>) -> i32 {
3+
let mut max_profit_1 = 0;
4+
let mut max_profit_2 = 0;
5+
let mut lowest_buy_price_1 = std::i32::MIN;
6+
let mut lowest_buy_price_2 = std::i32::MIN;
7+
8+
for p in prices {
9+
lowest_buy_price_1 = std::cmp::max(lowest_buy_price_1, -p);
10+
max_profit_1 = std::cmp::max(max_profit_1, lowest_buy_price_1 + p);
11+
lowest_buy_price_2 = std::cmp::max(lowest_buy_price_2, max_profit_1 - p);
12+
max_profit_2 = std::cmp::max(max_profit_2, p + lowest_buy_price_2);
13+
}
14+
15+
max_profit_2
16+
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use super::*;
21+
22+
#[test]
23+
fn it_works() {
24+
let vec = vec![3, 3, 5, 0, 0, 3, 1, 4];
25+
assert_eq!(max_profit(vec), 6);
26+
let vec1 = vec![1, 2, 3, 4, 5];
27+
assert_eq!(max_profit(vec1), 4);
28+
let vec2 = vec![7, 6, 4, 3, 1];
29+
assert_eq!(max_profit(vec2), 0);
30+
}
31+
32+
#[test]
33+
fn last() {
34+
let vec = vec![1, 2, 4, 2, 5, 7, 2, 4, 9, 0];
35+
assert_eq!(max_profit(vec), 13);
36+
}
37+
}

0 commit comments

Comments
 (0)