Last active
August 29, 2015 14:06
-
-
Save sadikovi/e08e17d6b63ebd9192ac to your computer and use it in GitHub Desktop.
Codility Lesson 3
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 A, int B, int K) { | |
return B/K - A/K + ((A % K == 0)?1:0); | |
} | |
} |
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(String S, int[] P, int[] Q) { | |
int[] seq = new int[P.length]; | |
int[][] fs = new int[3][S.length()]; | |
for (int i=0; i<S.length(); i++) { | |
short a = 0; short c = 0; short g = 0; | |
if (S.charAt(i) == 'A') a = 1; | |
if (S.charAt(i) == 'C') c = 1; | |
if (S.charAt(i) == 'G') g = 1; | |
fs[0][i] = ((i==0)?0:fs[0][i-1]) + a; | |
fs[1][i] = ((i==0)?0:fs[1][i-1]) + c; | |
fs[2][i] = ((i==0)?0:fs[2][i-1]) + g; | |
} | |
for (int k=0; k<P.length; k++) { | |
if (P[k] == Q[k]) { | |
if (S.charAt(P[k]) == 'A') seq[k] = 1; | |
if (S.charAt(P[k]) == 'C') seq[k] = 2; | |
if (S.charAt(P[k]) == 'G') seq[k] = 3; | |
if (S.charAt(P[k]) == 'T') seq[k] = 4; | |
} else { | |
if (P[k] > 0) { | |
if ((fs[0][Q[k]] - fs[0][P[k]-1]) > 0) { | |
seq[k] = 1; | |
} else if ((fs[1][Q[k]] - fs[1][P[k]-1]) > 0) { | |
seq[k] = 2; | |
} else if ((fs[2][Q[k]] - fs[2][P[k]-1]) > 0) { | |
seq[k] = 3; | |
} else { | |
seq[k] = 4; | |
} | |
} else { | |
if (fs[0][Q[k]] > 0) seq[k] = 1; | |
else if (fs[1][Q[k]] > 0) seq[k] = 2; | |
else if (fs[2][Q[k]] > 0) seq[k] = 3; | |
else seq[k] = 4; | |
} | |
} | |
} | |
return seq; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment