LeetCode Solution, Easy, 121. Best Time to Buy and Sell Stock
Play this article
121. Best Time to Buy and Sell Stock
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0
.
Example 1:
Input: prices = [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.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
1 <= prices.length <= 10^5
0 <= prices[i] <= 10^4
題目敘述
題目翻譯
題目就是會給一個陣列 prices
,其中的每個元素都表示當天股票的價格。然後要找出在一次買賣的操作中可以獲得的最大獲利。如果沒有辦法獲利的話就回傳 0
。
解法解析
這題很簡單的解法就是直接找出這個陣列的最大值和最小值,相減就會是最大的獲利了。唯一需要注意的是因為買賣操作,一定是買先後賣。所以順序的限制要注意到。
程式範例
Go
func maxProfit(prices []int) int {
minPrice := math.MaxInt32
theMaxProfit := 0
for _, price := range prices {
if price < minPrice {
minPrice = price
} else if price-minPrice > theMaxProfit {
theMaxProfit = price - minPrice
}
}
return theMaxProfit
}
JavaScript
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let minPrice = Number.MAX_SAFE_INTEGER,
theMaxProfit = 0;
for (let price of prices) {
if (price < minPrice) {
minPrice = price;
} else if (price - minPrice > theMaxProfit) {
theMaxProfit = price - minPrice;
}
}
return theMaxProfit;
};
Kotlin
class Solution {
fun maxProfit(prices: IntArray): Int {
var minPrice = Int.MAX_VALUE
var theMaxProfit = 0
for (price in prices) {
if (price < minPrice) {
minPrice = price
} else if (price - minPrice > theMaxProfit) {
theMaxProfit = price - minPrice
}
}
return theMaxProfit
}
}
PHP
class Solution
{
/**
* @param Integer[] $prices
* @return Integer
*/
function maxProfit($prices)
{
$minPrice = PHP_INT_MAX;
$maxProfit = 0;
foreach ($prices as $price) {
if ($price < $minPrice) {
$minPrice = $price;
} elseif ($price - $minPrice > $maxProfit) {
$maxProfit = $price - $minPrice;
}
}
return $maxProfit;
}
}
Python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = float('inf')
max_profit = 0
for i in range(len(prices)):
if prices[i] < min_price:
min_price = prices[i]
elif prices[i] - min_price > max_profit:
max_profit = prices[i] - min_price
return max_profit
Rust
impl Solution {
pub fn max_profit(prices: Vec<i32>) -> i32 {
let mut max_profit = 0;
let mut min_price = std::i32::MAX;
for price in prices {
if price < min_price {
min_price = price;
} else if price - min_price > max_profit {
max_profit = price - min_price;
}
}
max_profit
}
}
Swift
class Solution {
func maxProfit(_ prices: [Int]) -> Int {
var minPrice = Int.max
var theMaxProfit = 0
for price in prices {
if price < minPrice {
minPrice = price
} else if price - minPrice > theMaxProfit {
theMaxProfit = price - minPrice
}
}
return theMaxProfit
}
}