Last active
August 29, 2015 14:06
-
-
Save sadikovi/014f011baf48683e4132 to your computer and use it in GitHub Desktop.
Codility Lesson 2
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
class Solution { | |
public int[] solution(int N, int[] A) { | |
int[] counters = new int[N]; | |
int max = 0; | |
int absMax = 0; | |
for (int i=0; i<A.length; i++) { | |
if (A[i] == N+1) { | |
if ((i < A.length-1 && A[i+1] != N+1) || (i==A.length-1)) { | |
absMax += max; | |
max = 0; | |
counters = new int[N]; | |
} | |
} else { | |
counters[A[i]-1]++; | |
if (max < counters[A[i]-1]) | |
max = counters[A[i]-1]; | |
} | |
} | |
for (int i=0; i<counters.length; i++) | |
counters[i] += absMax; | |
return counters; | |
} | |
} |
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
import java.util.HashMap; | |
class Solution { | |
public int solution(int[] A) { | |
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); | |
int max = 0; | |
for (int i=0; i<A.length; i++) { | |
map.put(A[i], 1); | |
if (max < A[i]) | |
max = A[i]; | |
} | |
for (int i=1; i<=max; i++) | |
if (!map.containsKey(i)) | |
return i; | |
return max+1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment