-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStockBuySell
More file actions
90 lines (66 loc) · 2.65 KB
/
StockBuySell
File metadata and controls
90 lines (66 loc) · 2.65 KB
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
Here is the optimized version of your code with improved readability and better comments to make the logic clearer:
``java
class StockBuySell {
// Function to calculate the maximum profit
static int maximumProfit(int[] prices) {
int n = prices.length;
int totalProfit = 0; // Variable to store the total profit
int i = 0;
// Traverse the array to find buying and selling points
while (i < n - 1) {
// Find the local minima (buying point)
while (i < n - 1 && prices[i] >= prices[i + 1]) {
i++;
}
if (i == n - 1) break; // No more buying points
int buyPrice = prices[i]; // Set the buying price
// Find the local maxima (selling point)
while (i < n - 1 && prices[i] <= prices[i + 1]) {
i++;
}
int sellPrice = prices[i]; // Set the selling price
// Add the profit from the current transaction
totalProfit += (sellPrice - buyPrice);
}
return totalProfit; // Return the total profit
}
public static void main(String[] args) {
// Example array of stock prices
int[] prices = {100, 180, 260, 310, 40, 535, 695};
// Print the maximum profit that can be achieved
System.out.println("Maximum Profit: " + maximumProfit(prices));
}
}
```
---
### Optimizations and Improvements:
1. **Variable Names**:
- Changed `lMin` to `buyPrice` and `lMax` to `sellPrice` for better clarity.
- Changed `res` to `totalProfit` to make its purpose more descriptive.
2. **Comments**:
- Added clear and concise comments to explain each step of the algorithm.
3. **Edge Case Handling**:
- Added a condition to break the loop if no more buying points are available (`if (i == n - 1) break;`).
4. **Output**:
- Improved the output message in the `main` method to make it more user-friendly.
---
### Example Input/Output:
#### Input:
```java
int[] prices = {100, 180, 260, 310, 40, 535, 695};
```
#### Output:
```
Maximum Profit: 865
```
---
### Explanation of the Output:
- Buy at `100`, sell at `310` → Profit = `310 - 100 = 210`
- Buy at `40`, sell at `695` → Profit = `695 - 40 = 655`
- Total Profit = `210 + 655 = 865`
This optimized version improves readability and ensures the logic is easy to follow.
🛠️ How to Use Clone this repo
git clone https://github.com/Warlord27/JavaProb.git Navigate and open any .java file
Compile & run with your favorite Java IDE or CLI
⭐ Contributions Welcome! Got a cool Java problem? Found a bug? Feel free to fork, improve, or suggest! 🙌
Made with ☕ and ❤️ by Adam Warlord