-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path121.best-time-to-buy-and-sell-stock.java
106 lines (93 loc) · 2.28 KB
/
121.best-time-to-buy-and-sell-stock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* @lc app=leetcode id=121 lang=java
*
* [121] Best Time to Buy and Sell Stock
*
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
*
* algorithms
* Easy (51.34%)
* Total Accepted: 1.2M
* Total Submissions: 2.2M
* Testcase Example: '[7,1,5,3,6,4]'
*
* You are given an array prices where prices[i] is the price of a given stock
* on the i^th 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
*
*
*/
class Solution {
public int maxProfit(int[] prices) {
// Given information:
// Must buy before selling
// Theres no transaction if the array is sorted in descending order
// Theres always at least 1 element in the array
// Prices will never be negative
// Create a dynamic buying indicator variable
// Loop over the array, updating the buy variable as a better buying price
// present itself.
// If theres a selling oppportunity, try to update profits if its greater
// than the existing.
//
// Would it benefit me to have two pointers?
// Yes
//
// Input: [7,1,5,4]
// ^ <- buy
// [7,1,5,4]
// ^
// [7,1,5,4]
// ^ <- sell (profit = 4)
// [7,1,5,4]
// ^
int profit = 0;
if (prices.length == 0) {
return profit;
}
int buy = 10000; // Max price constraint = 10^4
for (int i = 0; i < prices.length; i++) {
int curr = prices[i];
// Look for a lower price point for buying
if (curr < buy) {
buy = curr;
} else if (curr - buy > profit) {
profit = curr - buy;
}
}
return profit;
}
}