MaxProfit

Given a log of stock prices compute the maximum possible earning.


Solution:

Solution to Codility's Maximum Profit problem which is from the Codility Lesson 9: Maximum slice problem and, is solved in Java 8 with 100% performance and correctness scores. The goal here is to given a log of stock prices compute the maximum possible earning. You can find the question of this MaxProfit problem in the Codility website.


package Codility.Lesson9;

import java.util.*;

public class MaxProfit {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] int1 = { 23171, 21011, 21123, 21366, 21013, 21367 };
		int result1 = solution(int1);
		System.out.println(result1);

		int[] int2 = { 0, 0 };
		int result2 = solution(int2);
		System.out.println(result2);

		int[] int3 = { 4, 4, 2, 5, 3, 4, 4, 4 };
		int result3 = solution(int3);
		System.out.println(result3);

		int[] int4 = { 1, 2, 1, 1, 2, 1 };
		int result4 = solution(int3);
		System.out.println(result4);
	}

	public static int solution(int[] A) {

		if (A.length <= 1)
			return 0;

		int minPrice = A[0];
		int maxProfit = 0;

		for (int i = 1; i < A.length; i++) {
			if (A[i] < minPrice)
				minPrice = A[i];
			else {
				int curProfit = A[i] - minPrice;
				if (curProfit > maxProfit)
					maxProfit = curProfit;
			}
		}
		return maxProfit;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *