From b566655930a6547bc5ccad563e4952de32f221ff Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 14 Oct 2024 09:32:40 +0530 Subject: [PATCH 1/3] feat: Add `StockProfitCalculator` new algorithm with Junit tests --- .../StockProfitCalculator.java | 32 +++++++++++++++++++ .../StockProfitCalculatorTest.java | 21 ++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java create mode 100644 src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java b/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java new file mode 100644 index 000000000000..6c5f2b61e46b --- /dev/null +++ b/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java @@ -0,0 +1,32 @@ +package com.thealgorithms.greedyalgorithms; + +/** + * The StockProfitCalculator class provides a method to calculate the maximum profit + * that can be made from a single buy and sell of one share of stock. + * The approach uses a greedy algorithm to efficiently determine the maximum profit. + * + * @author Hardvan + */ +public class StockProfitCalculator { + + /** + * Calculates the maximum profit from a list of stock prices. + * + * @param prices an array of integers representing the stock prices on different days + * @return the maximum profit that can be achieved from a single buy and sell + * transaction, or 0 if no profit can be made + */ + public static int maxProfit(int[] prices) { + if (prices == null || prices.length == 0) { + return 0; + } + + int minPrice = prices[0]; + int maxProfit = 0; + for (int price : prices) { + minPrice = Math.min(price, minPrice); + maxProfit = Math.max(price - minPrice, maxProfit); + } + return maxProfit; + } +} diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java new file mode 100644 index 000000000000..afad76a34521 --- /dev/null +++ b/src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java @@ -0,0 +1,21 @@ +package com.thealgorithms.greedyalgorithms; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class StockProfitCalculatorTest { + + @ParameterizedTest + @MethodSource("provideTestCases") + public void testMaxProfit(int[] prices, int expected) { + assertEquals(expected, StockProfitCalculator.maxProfit(prices)); + } + + private static Stream provideTestCases() { + 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)); + } +} From 686149b45212c49f89bf22a25c3a025073de3059 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Mon, 14 Oct 2024 04:02:55 +0000 Subject: [PATCH 2/3] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f1a531486de7..87c54b642678 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -310,6 +310,7 @@ * [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java) * [MergeIntervals](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MergeIntervals.java) * [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/MinimizingLateness.java) + * [StockProfitCalculator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java) * io * [BufferedReader](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/io/BufferedReader.java) * lineclipping @@ -890,6 +891,7 @@ * [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java) * [MergeIntervalsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MergeIntervalsTest.java) * [MinimizingLatenessTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/MinimizingLatenessTest.java) + * [StockProfitCalculatorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/StockProfitCalculatorTest.java) * io * [BufferedReaderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/io/BufferedReaderTest.java) * lineclipping From fafabd40f17222f6ed100807af97133633e3f206 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 14 Oct 2024 09:37:06 +0530 Subject: [PATCH 3/3] Fix --- .../thealgorithms/greedyalgorithms/StockProfitCalculator.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java b/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java index 6c5f2b61e46b..01950d902b4f 100644 --- a/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java +++ b/src/main/java/com/thealgorithms/greedyalgorithms/StockProfitCalculator.java @@ -7,7 +7,9 @@ * * @author Hardvan */ -public class StockProfitCalculator { +public final class StockProfitCalculator { + private StockProfitCalculator() { + } /** * Calculates the maximum profit from a list of stock prices.