Skip to content

Commit

Permalink
Add the solution for the problem "#122 Best Time to Buy and Sell Stoc…
Browse files Browse the repository at this point in the history
…k II".
  • Loading branch information
dainslef committed Sep 6, 2023
1 parent 3fb7146 commit 879e308
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/leetcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ mod q103_binary_tree_zipzag_level_order_traversal;
mod q107_binary_tree_level_order_traversal_ii;
mod q10_regular_expression_matching;
mod q11_container_with_most_water;
mod q122_best_time_to_buy_and_sell_stock_ii;
mod q126_word_ladder_ii;
mod q127_word_ladder;
mod q12_integer_to_roman;
Expand Down Expand Up @@ -395,7 +396,6 @@ mod day_30_leetcoding_challenge;
// mod q1046_last_stone_weight;
// mod q155_min_stack;
// mod q876_middle_of_the_linked_list;
// mod q122_best_time_to_buy_and_sell_stock_ii;
// mod q283_move_zeroes;
// mod q136_single_number;
// mod q202_happy_number;
Expand Down
43 changes: 43 additions & 0 deletions src/leetcode/q122_best_time_to_buy_and_sell_stock_ii.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 122. Best Time to Buy and Sell Stock II
*
* Say you have an array for which the ith element is the price of a given stock on day i.
*
* Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
*
* Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
*
* Example 1:
*
* Input: [7,1,5,3,6,4]
* Output: 7
* Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
* Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Check failure on line 15 in src/leetcode/q122_best_time_to_buy_and_sell_stock_ii.rs

View workflow job for this annotation

GitHub Actions / Run tests

expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `buy`
* Example 2:
*
* Input: [1,2,3,4,5]
* Output: 4
* Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
* Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are

Check failure on line 21 in src/leetcode/q122_best_time_to_buy_and_sell_stock_ii.rs

View workflow job for this annotation

GitHub Actions / Run tests

expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `that`
* engaging multiple transactions at the same time. You must sell before buying again.

Check failure on line 22 in src/leetcode/q122_best_time_to_buy_and_sell_stock_ii.rs

View workflow job for this annotation

GitHub Actions / Run tests

expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `multiple`
* Example 3:
*
* Input: [7,6,4,3,1]
* Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
*/

fn max_profit(prices: Vec<i32>) -> i32 {
let mut sum = 0;
for i in 1..prices.len() {
sum += 0.max(prices[i] - prices[i - 1]);
}
sum
}

#[test]
fn q122_test() {
assert_eq!(max_profit(vec![7, 1, 5, 3, 6, 4]), 7);
assert_eq!(max_profit(vec![1, 2, 3, 4, 5]), 4);
assert_eq!(max_profit(vec![7, 6, 4, 3, 1]), 0);
}

0 comments on commit 879e308

Please sign in to comment.