Problem Best Time to Buy and Sell Stock I

Problem Description:

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.

Solution:

 1     public int maxProfit(int[] prices) {
 2         if (prices.length <= 1) return 0;
 3         int max = 0;
 4         int low = prices[0];
 5         for (int i = 0; i < prices.length; i++){
 6             low = Math.min(low, prices[i]);
 7             max = Math.max(max, prices[i] - low);
 8         }
 9         return max;
10     }

Problem Best Time to Buy and Sell Stock I

时间: 2024-10-07 21:51:32

Problem Best Time to Buy and Sell Stock I的相关文章

Problem Best Time to Buy and Sell Stock 11

Problem Description: 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 stoc

Problem Best Time to Buy and Sell Stock III

Problem Description: 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 a

[LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

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

【题解】【数组】【DP】【Codility】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

Best Time to Buy and Sell Stock IV

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

leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is inte

LeetCode188:Best Time to Buy and Sell Stock IV

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

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】Best Time to Buy and Sell Stock IV

Best Time to Buy and Sell Stock IV 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 k transactions. Note: You may not engage in multiple t