|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=121 lang=java |
| 3 | + * |
| 4 | + * [121] Best Time to Buy and Sell Stock |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Easy (51.34%) |
| 10 | + * Total Accepted: 1.2M |
| 11 | + * Total Submissions: 2.2M |
| 12 | + * Testcase Example: '[7,1,5,3,6,4]' |
| 13 | + * |
| 14 | + * You are given an array prices where prices[i] is the price of a given stock |
| 15 | + * on the i^th day. |
| 16 | + * |
| 17 | + * You want to maximize your profit by choosing a single day to buy one stock |
| 18 | + * and choosing a different day in the future to sell that stock. |
| 19 | + * |
| 20 | + * Return the maximum profit you can achieve from this transaction. If you |
| 21 | + * cannot achieve any profit, return 0. |
| 22 | + * |
| 23 | + * |
| 24 | + * Example 1: |
| 25 | + * |
| 26 | + * |
| 27 | + * Input: prices = [7,1,5,3,6,4] |
| 28 | + * Output: 5 |
| 29 | + * Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit |
| 30 | + * = 6-1 = 5. |
| 31 | + * Note that buying on day 2 and selling on day 1 is not allowed because you |
| 32 | + * must buy before you sell. |
| 33 | + * |
| 34 | + * |
| 35 | + * Example 2: |
| 36 | + * |
| 37 | + * |
| 38 | + * Input: prices = [7,6,4,3,1] |
| 39 | + * Output: 0 |
| 40 | + * Explanation: In this case, no transactions are done and the max profit = |
| 41 | + * 0. |
| 42 | + * |
| 43 | + * |
| 44 | + * |
| 45 | + * Constraints: |
| 46 | + * |
| 47 | + * |
| 48 | + * 1 <= prices.length <= 10^5 |
| 49 | + * 0 <= prices[i] <= 10^4 |
| 50 | + * |
| 51 | + * |
| 52 | + */ |
| 53 | +class Solution { |
| 54 | + public int maxProfit(int[] prices) { |
| 55 | + // Given information: |
| 56 | + // Must buy before selling |
| 57 | + // Theres no transaction if the array is sorted in descending order |
| 58 | + // Theres always at least 1 element in the array |
| 59 | + // Prices will never be negative |
| 60 | + |
| 61 | + // Create a dynamic buying indicator variable |
| 62 | + // Loop over the array, updating the buy variable as a better buying price |
| 63 | + // present itself. |
| 64 | + // If theres a selling oppportunity, try to update profits if its greater |
| 65 | + // than the existing. |
| 66 | + // |
| 67 | + // Would it benefit me to have two pointers? |
| 68 | + // Yes |
| 69 | + // |
| 70 | + // Input: [7,1,5,4] |
| 71 | + // ^ <- buy |
| 72 | + // [7,1,5,4] |
| 73 | + // ^ |
| 74 | + // [7,1,5,4] |
| 75 | + // ^ <- sell (profit = 4) |
| 76 | + // [7,1,5,4] |
| 77 | + // ^ |
| 78 | + |
| 79 | + int profit = 0; |
| 80 | + int buyIdx = 0; |
| 81 | + |
| 82 | + for (int i = 0; i < prices.length; i++) { |
| 83 | + int curr = prices[i]; |
| 84 | + int buy = prices[buyIdx]; |
| 85 | + // Look for a higher price point for selling |
| 86 | + if (curr > buy && curr - buy > profit) { |
| 87 | + profit = curr - buy; |
| 88 | + } |
| 89 | + // Look for a lower price point for buying |
| 90 | + if (curr < buy) { |
| 91 | + buyIdx = i; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return profit; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments