1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Best Time to Buy and Sell Stock [leetcode: best_time_to_buy_and_sell_stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
//!
//! Say you have an array for which the ith element is the price of a given stock on day *i*.
//!
//! If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
//!
//! Note that you cannot sell a stock before you buy one.
//!
//! ***Example1:***
//!
//! ```
//! Input: [7,1,5,3,6,4]
//! Output: 5
//! Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
//!              Not 7-1 = 6, as selling price needs to be larger than buying price.
//! ```
//!
//! ***Example2:***
//!
//! ```
//! Input: [7,6,4,3,1]
//! Output: 0
//! Explanation: In this case, no transaction is done, i.e. max profit = 0.
//! ```
//!

/// # Solutions
///
/// # Approach 1: Dynamic Programming
///
/// * Time complexity: O(3n)
///
/// * Space complexity: O(n)
///
/// * Runtime: 0 ms
/// * Memory: 3.6 MB
///
/// ```rust
/// use std::cmp;
///
/// impl Solution {
///     pub fn max_profit(prices: Vec<i32>) -> i32 {
///         if prices.len() < 2 { return 0; }
///
///         let mut result  = 0;
///         let mut profits = vec![vec![0; 3]; prices.len()];
///         profits[0][1] = -prices[0];
///
///         for i in 1..prices.len() {
///             profits[i][0] = profits[i - 1][0];
///             profits[i][1] = cmp::max(profits[i - 1][1], profits[i - 1][0] - prices[i]);
///             profits[i][2] = profits[i - 1][1] + prices[i];
///
///             result = cmp::max(result, cmp::max(profits[i][0], cmp::max(profits[i][1], profits[i][2])));
///         }
///         result
///     }
/// }
/// ```
///
/// # Approach 2: Dynamic Programming
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(n)
///
/// * Runtime: 0 ms
/// * Memory: 2.7 MB
///
/// ```rust
/// impl Solution {
///     pub fn max_profit(prices: Vec<i32>) -> i32 {
///         if prices.len() < 2 { return 0; }
///
///         let mut profits = vec![0; prices.len()];
///         profits[0] = -prices[0];
///         for i in 1..prices.len() {
///             if profits[i - 1] < 0 { profits[ i - 1] = 0; }
///
///             profits[i] = profits[i - 1] - prices[i - 1] + prices[i];
///         }
///
///         profits.sort();
///         *profits.last().unwrap()
///     }
/// }
/// ```
///
/// # Approach 3: Dynamic Programming
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(n)
///
/// * Runtime: 0 ms
/// * Memory: 2.6 MB
///
/// ```rust
/// use std::cmp;
///
/// impl Solution {
///     pub fn max_profit(prices: Vec<i32>) -> i32 {
///         if prices.len() < 2 { return 0; }
///
///         let mut profits = vec![0; prices.len()];
///         let mut max_profit = 0;
///         let mut tmp_min = prices[0];
///         for i in 1..prices.len() {
///             profits[i] = cmp::max(profits[i - 1], prices[i] - tmp_min);
///             tmp_min = cmp::min(tmp_min, prices[i]);
///             max_profit = cmp::max(profits[i], max_profit);
///         }
///
///         max_profit
///     }
/// }
/// ```
///
/// # Approach 4: Dynamic Programming
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(n)
///
/// * Runtime: 0 ms
/// * Memory: 2.6 MB
///
/// ```rust
/// use std::cmp;
///
/// impl Solution {
///     pub fn max_profit(prices: Vec<i32>) -> i32 {
///         if prices.len() < 2 { return 0; }
///
///         let mut profits = vec![0; 2];
///         let mut max_profit = 0;
///         let mut tmp_min = prices[0];
///         for i in 1..prices.len() {
///             let (x, y) = (i % 2, (i - 1) % 2);
///             profits[x] = cmp::max(profits[y], prices[i] - tmp_min);
///             tmp_min = cmp::min(tmp_min, prices[i]);
///             max_profit = cmp::max(profits[x], max_profit);
///         }
///
///         max_profit
///     }
/// }
/// ```
///
pub fn max_profit(prices: Vec<i32>) -> i32 {
    if prices.len() < 2 { return 0; }

    let mut profits = vec![0; prices.len()];
    profits[0] = -prices[0];
    for i in 1..prices.len() {
        if profits[i - 1] < 0 { profits[ i - 1] = 0; }

        profits[i] = profits[i - 1] - prices[i - 1] + prices[i];
    }

    profits.sort();
    *profits.last().unwrap()
}