Skip to content

Commit

Permalink
MaxProfit
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianiacobghiula committed Jun 8, 2020
1 parent 51cde32 commit 954ca92
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/codility/p09_maximum_slice_problem/MaxProfit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package codility.p09_maximum_slice_problem;

/*
https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_profit/
*/
public class MaxProfit {

// https://app.codility.com/demo/results/trainingYJVWX2-A6J/
public int solution(int[] A) {
int maxSlice = 0;
int max = 0;
for (int i = 1; i < A.length; i++) {
max = Math.max(max + A[i] - A[i - 1], 0);
maxSlice = Math.max(max, maxSlice);
}

return maxSlice;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package codility.p09_maximum_slice_problem;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class MaxProfitTest {
MaxProfit maxProfit;

@BeforeEach
public void init() {
maxProfit = new MaxProfit();
}

@Test
public void sample1() {
int solution = maxProfit.solution(new int[]{23171, 21011, 21123, 21366, 21013, 21367});
assertThat(solution).isEqualTo(356);
}
}

0 comments on commit 954ca92

Please sign in to comment.