[LeetCode][JavaScript]Best Time to Buy and Sell Stock

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 (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/



一图流解释,最低买进最高卖出。

 1 /**
 2  * @param {number[]} prices
 3  * @return {number}
 4  */
 5 var maxProfit = function(prices) {
 6     var min = Infinity;
 7     var res = -Infinity;
 8     for(var i = 0; i < prices.length; i++){
 9         if(prices[i] < min){
10             min = prices[i];
11         }
12         if(prices[i] > min && prices[i] - min > res){
13             res = prices[i] - min;
14         }
15     }
16     if(isFinite(res)){
17         return res;
18     }else{
19         return 0;
20     }
21 };
时间: 2024-08-05 23:41:20

[LeetCode][JavaScript]Best Time to Buy and Sell Stock的相关文章

[LeetCode][JavaScript]Best Time to Buy and Sell Stock II

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 (ie, buy one and sell one sha

[LeetCode OJ] Best Time to Buy and Sell Stock I

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 (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. eg:输

【LeetCode】Best Time to Buy and Sell Stock II

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 (ie, buy one and sell one sha

Leetcode 贪心 Best Time to Buy and Sell Stock

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted: 13234 Total Submissions: 43145 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

[C++]LeetCode: 77 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 (ie, buy one and sell one share of the stock multiple times)

Leetcode problem-122 Best Time to Buy and Sell Stock II 题解

Leetcode Problem-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 (ie, bu

LeetCode\EPI &quot;Best Time to Buy and Sell Stock&quot;

Also the very first problem on EPI. class Solution { public: int maxProfit(vector<int> &prices) { size_t len = prices.size(); if (len <= 1) return 0; else if (len == 2) { if (prices[0] >= prices[1]) return 0; else return prices[1] - prices

[LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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 at most two transactions. Note:You may not engage in multiple transactions at the same time (ie,

LeetCode | 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机【Python】

LeetCode 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机[Easy][Python][贪心] Problem LeetCode 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., b