Skip to content

Instantly share code, notes, and snippets.

@sinanduman
Created March 21, 2017 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinanduman/e46903f6f1ad1f893cc0e56f9de69054 to your computer and use it in GitHub Desktop.
Save sinanduman/e46903f6f1ad1f893cc0e56f9de69054 to your computer and use it in GitHub Desktop.
Amazon find symbols score and total sum. X = multiply by 2 with previous score. + = adding previous 2 scores. Z = ignoring last score, as if it isn't thrown never.
package algo;
public class Score {
public static void main(String[] args) {
String[] nums = { "5", "-2", "4", "Z", "X", "9", "+", "+" }; // 27
// String[] nums = { "1", "2", "4", "Z"}; // 3
System.out.println(findScore(nums, nums.length));
}
static int findScore(String[] blocks, int length) {
int sum = 0;
int[] score = new int[length];
for (int i = 0; i < length; i++) {
if (blocks[i].equals("Z")) {
if (i == 0) {
score[i] = 0;
}
if (i > 0) {
score[i - 1] = 0;
}
if (i > 1) {
score[i] = score[i - 2];
score[i - 2] = 0;
}
} else if (blocks[i].equals("X")) {
score[i] = score[i - 1] * 2;
} else if (blocks[i].equals("+")) {
score[i] = score[i - 1] + score[i - 2];
} else {
score[i] = (int) Integer.valueOf(blocks[i]);
}
}
for (int i = 0; i < score.length; i++) {
sum += score[i];
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment