-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
"cabac", | ||
"camelcase", | ||
"Ccodegen", | ||
"choco", | ||
"coverallsapp", | ||
"Coverflow", | ||
"Cpanic", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 购买两块巧克力 | ||
// https://leetcode.cn/problems/buy-two-chocolates | ||
// INLINE ../../images/sort/buy_two_chocolates.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn buy_choco(prices: Vec<i32>, money: i32) -> i32 { | ||
let mut fi = i32::MAX; | ||
let mut se = i32::MAX; | ||
for p in prices { | ||
if p < fi { | ||
se = fi; | ||
fi = p; | ||
} else if p < se { | ||
se = p; | ||
} | ||
} | ||
if money < fi + se { | ||
money | ||
} else { | ||
money - fi - se | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use rust_practice::sort::buy_two_chocolates::Solution; | ||
|
||
#[test] | ||
fn buy_choco() { | ||
// 示例 1: | ||
// 输入:prices = [1,2,2], money = 3 | ||
// 输出:0 | ||
// 解释:分别购买价格为 1 和 2 的巧克力。你剩下 3 - 3 = 0 块钱。所以我们返回 0 。 | ||
let prices = vec![1, 2, 2]; | ||
let money = 3; | ||
assert_eq!(Solution::buy_choco(prices, money), 0); | ||
|
||
// 示例 2: | ||
// 输入:prices = [3,2,3], money = 3 | ||
// 输出:3 | ||
// 解释:购买任意 2 块巧克力都会超过你拥有的钱数,所以我们返回 3 。 | ||
let prices = vec![3, 2, 3]; | ||
let money = 3; | ||
assert_eq!(Solution::buy_choco(prices, money), 3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters