Skip to content

Commit a537654

Browse files
authored
Add StockProfitCalculator (#5793)
1 parent 2a518e3 commit a537654

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
* [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java)
321321
* [MergeIntervals](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MergeIntervals.java)
322322
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java)
323+
* [StockProfitCalculator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java)
323324
* io
324325
* [BufferedReader](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/io/BufferedReader.java)
325326
* lineclipping
@@ -920,6 +921,7 @@
920921
* [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java)
921922
* [MergeIntervalsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MergeIntervalsTest.java)
922923
* [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java)
924+
* [StockProfitCalculatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java)
923925
* io
924926
* [BufferedReaderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/io/BufferedReaderTest.java)
925927
* lineclipping
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
/**
4+
* The StockProfitCalculator class provides a method to calculate the maximum profit
5+
* that can be made from a single buy and sell of one share of stock.
6+
* The approach uses a greedy algorithm to efficiently determine the maximum profit.
7+
*
8+
* @author Hardvan
9+
*/
10+
public final class StockProfitCalculator {
11+
private StockProfitCalculator() {
12+
}
13+
14+
/**
15+
* Calculates the maximum profit from a list of stock prices.
16+
*
17+
* @param prices an array of integers representing the stock prices on different days
18+
* @return the maximum profit that can be achieved from a single buy and sell
19+
* transaction, or 0 if no profit can be made
20+
*/
21+
public static int maxProfit(int[] prices) {
22+
if (prices == null || prices.length == 0) {
23+
return 0;
24+
}
25+
26+
int minPrice = prices[0];
27+
int maxProfit = 0;
28+
for (int price : prices) {
29+
minPrice = Math.min(price, minPrice);
30+
maxProfit = Math.max(price - minPrice, maxProfit);
31+
}
32+
return maxProfit;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.greedyalgorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class StockProfitCalculatorTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("provideTestCases")
14+
public void testMaxProfit(int[] prices, int expected) {
15+
assertEquals(expected, StockProfitCalculator.maxProfit(prices));
16+
}
17+
18+
private static Stream<Arguments> provideTestCases() {
19+
return Stream.of(Arguments.of(new int[] {7, 1, 5, 3, 6, 4}, 5), Arguments.of(new int[] {7, 6, 4, 3, 1}, 0), Arguments.of(new int[] {5, 5, 5, 5, 5}, 0), Arguments.of(new int[] {10}, 0), Arguments.of(new int[] {1, 5}, 4), Arguments.of(new int[] {2, 4, 1, 3, 7, 5}, 6));
20+
}
21+
}

0 commit comments

Comments
 (0)