Skip to content

feat: Add StockProfitCalculator new algorithm with Junit tests #5793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,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
Expand Down Expand Up @@ -920,6 +921,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 final class StockProfitCalculator {
private 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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));
}
}