Created
March 21, 2017 04:51
-
-
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.
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
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