-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51cde32
commit 954ca92
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/main/java/codility/p09_maximum_slice_problem/MaxProfit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/codility/p09_maximum_slice_problem/MaxProfitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |