Skip to content

Commit

Permalink
add: 购买两块巧克力
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 29, 2023
1 parent b878dd3 commit e494bc4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"cabac",
"camelcase",
"Ccodegen",
"choco",
"coverallsapp",
"Coverflow",
"Cpanic",
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 排序

- [购买两块巧克力](src/sort/buy_two_chocolates.rs) [数组, 排序]

- LeetCode 2706. 购买两块巧克力 <https://leetcode.cn/problems/buy-two-chocolates>

- [下一个更大元素 IV](src/sort/next_greater_element_iv.rs) [栈, 数组, 二分查找, 排序, 单调栈, 堆(优先队列)]

- LeetCode 2454. 下一个更大元素 IV <https://leetcode.cn/problems/next-greater-element-iv>
Expand Down
Binary file added images/sort/buy_two_chocolates.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/sort/buy_two_chocolates.rs
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
}
}
}
1 change: 1 addition & 0 deletions src/sort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ pub mod compare_strings_by_frequency_of_the_smallest_character;
pub mod greatest_sum_divisible_by_three;
pub mod maximum_earnings_from_taxi;
pub mod next_greater_element_iv;
pub mod buy_two_chocolates;
20 changes: 20 additions & 0 deletions tests/sort/buy_two_chocolates_test.rs
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);
}
1 change: 1 addition & 0 deletions tests/sort/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ pub mod compare_strings_by_frequency_of_the_smallest_character_test;
pub mod greatest_sum_divisible_by_three_test;
pub mod maximum_earnings_from_taxi_test;
pub mod next_greater_element_iv_test;
pub mod buy_two_chocolates_test;

0 comments on commit e494bc4

Please sign in to comment.