https://instant.1point3acres.com/thread/194567
https://instant.1point3acres.com/thread/206528
https://instant.1point3acres.com/thread/190933
https://instant.1point3acres.com/thread/184100
OA1
replace two adjacent digits with the larger one, return min
public int deleteLarger(int X) {
StringBuilder sb = new StringBuilder(String.valueOf(X));
int len = sb.length();
int result = Integer.MAX_VALUE;
for (int i = len - 2; i >= 0; --i) {
StringBuilder tmp = new StringBuilder(sb.toString());
if (Character.getNumericValue(sb.charAt(i)) > Character.getNumericValue(sb.charAt(i + 1))) {
tmp.deleteCharAt(i + 1);
} else if (Character.getNumericValue(sb.charAt(i)) < Character.getNumericValue(sb.charAt(i + 1))){
tmp.deleteCharAt(i);
}
result = Math.min(result, Integer.valueOf(tmp.toString()));
}
return result;
}
test
int[] test3 = {233614, 12, 21, 245, 254, 23456, 65432, 12112, 12111, 11211};
int[] ans3 = {23364, 2, 2, 25, 25, 2346, 6432, 1212, 1211, 1121};
Replace two adjacent digits with round up average, return max
public int deleteRoundUp (int X) {
StringBuilder sb = new StringBuilder(String.valueOf(X));
int len = sb.length();
int lastAvg = 0;
for (int i = 1; i < len; ++i) {
int avg = (int)Math.ceil((Character.getNumericValue(sb.charAt(i)) +
Character.getNumericValue(sb.charAt(i - 1))) / 2.0);
if (avg > Character.getNumericValue(sb.charAt(i - 1))) {
sb.replace(i - 1, i + 1, String.valueOf(avg));
return Integer.valueOf(sb.toString());
}
lastAvg = avg;
}
sb.replace(len - 2, len, String.valueOf(lastAvg));
return Integer.valueOf(sb.toString());
}
test
int[] test2 = {623315, 15, 623, 378364, 987654331, 11111, 33211};
int[] ans2 = {63315, 3, 63, 58364, 98765432, 1111, 3321};
Choose a group of identical adjacent and remove a single digit, return max
public int deleteIdenticalAdj(int X) {
StringBuilder sb = new StringBuilder(String.valueOf(X));
int lastPos = 0;
for (int i = 1; i < sb.length(); ++i) {
if (sb.charAt(i) == sb.charAt(i - 1)) {
lastPos = i;
if (i + 1 < sb.length() && sb.charAt(i + 1) > sb.charAt(i)) {
sb.deleteCharAt(i);
return Integer.valueOf(sb.toString());
}
}
}
sb.deleteCharAt(lastPos);
return Integer.valueOf(sb.toString());
}
test
int[] test1 = {223336226, 5522344, 22, 123223, 422124, 4221221};
int[] ans1 = {23336226, 552344, 2, 12323, 42124, 422121};
OA2
longest image path from root
public int lengthLongestPath1(String input) {
int count = 0;
int result = 0;
Map<Integer, Integer> levels = new HashMap<>();
int prevLevel = -1;
String[] files = input.split("\n");
for (String file : files) {
int level = file.lastIndexOf(' ') + 1;
int len = file.length() - level;
//System.out.println(file);
//System.out.println(len);
if (level <= prevLevel) {
while (prevLevel > level) {
count = count - levels.get(prevLevel) - 1;
prevLevel--;
}
count = count - levels.get(prevLevel) - 1;
}
if (file.contains(".jpeg") || file.contains(".png") || file.contains(".gif")) {
result = Math.max(result, count);
}
prevLevel = level;
levels.put(level, len);
count = count + len + 1;
}
return result;
}
longest image path from root (including image file)
public int lengthLongestPath2(String input) {
int count = 0;
int result = 0;
Map<Integer, Integer> levels = new HashMap<>();
int prevLevel = -1;
String[] files = input.split("\n");
for (String file : files) {
int level = file.lastIndexOf(' ') + 1;
int len = file.length() - level;
//System.out.println(file);
//System.out.println(len);
if (level <= prevLevel) {
while (prevLevel > level) {
count = count - levels.get(prevLevel) - 1;
prevLevel--;
}
count = count - levels.get(prevLevel) - 1;
}
prevLevel = level;
levels.put(level, len);
count = count + len + 1;
if (file.contains(".jpeg") || file.contains(".png") || file.contains(".gif")) {
result = Math.max(result, count);
}
}
return result;
}
total path to image from root
public int lengthLongestPath3(String input) {
int count = 0;
int result = 0;
Map<Integer, Integer> levels = new HashMap<>();
int prevLevel = -1;
String[] files = input.split("\n");
for (String file : files) {
int level = file.lastIndexOf(' ') + 1;
int len = file.length() - level;
//System.out.println(file);
//System.out.println(len);
if (level <= prevLevel) {
while (prevLevel > level) {
count = count - levels.get(prevLevel) - 1;
prevLevel--;
}
count = count - levels.get(prevLevel) - 1;
}
if (file.contains(".jpeg") || file.contains(".png") || file.contains(".gif")) {
result += count;
}
prevLevel = level;
levels.put(level, len);
count = count + len + 1;
}
return result;
}
total path to image from root, including image file
public int lengthLongestPath4(String input) {
int count = 0;
int result = 0;
Map<Integer, Integer> levels = new HashMap<>();
int prevLevel = -1;
String[] files = input.split("\n");
for (String file : files) {
int level = file.lastIndexOf(' ') + 1;
int len = file.length() - level;
//System.out.println(file);
//System.out.println(len);
if (level <= prevLevel) {
while (prevLevel > level) {
count = count - levels.get(prevLevel) - 1;
prevLevel--;
}
count = count - levels.get(prevLevel) - 1;
}
prevLevel = level;
levels.put(level, len);
count = count + len + 1;
if (file.contains(".jpeg") || file.contains(".png") || file.contains(".gif")) {
result += count;
}
}
return result;
}