2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
भवतः कृते पूर्णाङ्कसरणिका दत्ता अस्ति prices
कुत्रprices[i]
इति दत्तस्य स्टॉकस्य मूल्यं भवतिi^th
दिनं।
प्रत्येकं दिवसे भवन्तः स्टॉकस्य क्रयणं/विक्रयणं वा कर्तुं निर्णयं कर्तुं शक्नुवन्ति। भवन्तः केवलं धारयितुं शक्नुवन्तिअधिकतया एकः कदापि स्टॉकस्य भागः। तथापि, भवन्तः तत् क्रेतुं शक्नुवन्ति ततः तत्क्षणमेव तत् विक्रेतुं शक्नुवन्तितस्मिन् एव दिने.
अन्विष्य प्रत्यागच्छतु the अधिकतमं लाभं भवन्तः प्राप्तुं शक्नुवन्ति.
उदाहरणम् १ : १.
**Input:** prices = [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.
Total profit is 4 + 3 = 7.
उदाहरणम् २ : १.
**Input:** prices = [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.
Total profit is 4.
उदाहरणम् ३ : १.
**Input:** prices = [7,6,4,3,1]
**Output:** 0
**Explanation:** There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
बाधाः : १.
1 <= prices.length <= 3 * 10^4
0 <= prices[i] <= 10^4
* DSF
* DP
* Greedy
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let res = 0;
for(let i = 1; i < prices.length; i++){
if(prices[i-1] < prices[i]){ // 将每天的收益看作自己的收益
res += prices[i] - prices[i-1]
}
}
return res;
};