public class BBB {
public static int findK(int[] source) {
int length = source.length;
if (length <= 2) {
return 0;
}
int left = 0;
int right = length - 1;
int startValue = source[0];
int mid = 0;
int k = 0;
while (left < right) {
mid = (right - left) / 2 + left;
if (right == left + 1) {
k = source[left] > source[right] ? left : right;
break;
}
if (source[mid + 1] < startValue) {
right = mid;
} else {
left = mid + 1;
}
}
return left == right ? left : k;
}
public static void main(String[] args) {
int[] a = {90, 3, 4, 5, 7, 9};
System.out.println(findK(a));
}
}
public class FindsNodePunish {
static int beautifulPath(int n, int[][] edges, int start, int end) {
Map<Integer, Map<Integer, Integer>> edgesMap = getEdgesMap(n, edges);
int result = minPath(edgesMap, start, end);
return result == Integer.MAX_VALUE ? -1 : result;
}
private static Map<Integer, Map<Integer, Integer>> getEdgesMap(int n, int[][] edges) {
Map<Integer, Map<Integer, Integer>> map = new HashMap<>(2 * n);
List<int[]> list = new ArrayList<>(n);
Collections.addAll(list, edges);
list.forEach(e -> {
int start = e[0];
int end = e[1];
int distance = e[2];
map.computeIfAbsent(start, k -> new HashMap<Integer, Integer>(n));
map.get(start).merge(end, distance, (a, b) -> b < a ? b : a);
});
return map;
}
private static int minPath(Map<Integer, Map<Integer, Integer>> edgesMap, int start, int end) {
Map<Integer, Integer> startMap = edgesMap.get(start);
int result = Integer.MAX_VALUE;
for (Map.Entry<Integer, Integer> e : startMap.entrySet()) {
int next = e.getKey();
int path = e.getValue();
if (next != end) {
edgesMap.remove(start);
path = path | minPath(edgesMap, next, end);
edgesMap.put(start, startMap);
}
if (path < result) {
result = path;
}
}
return result;
}
public static void main(String[] args) {
int[][] edges = {{1, 2, 1}, {2, 3, 3}, {1, 3, 100}};
System.out.println(beautifulPath(3, edges, 1, 3));
}
}
public class CombineTime {
static int[][] merge(int[][] intervals) {
if (intervals.length == 0) {
return null;
}
int[][] midleValue = new int[intervals.length][];
int returnValueIndex = 0;
for (int i = 0; i < intervals.length - 1; i++) {
int[] leftOne = midleValue[returnValueIndex];
if (i == 0) {
leftOne = intervals[0];
}
int[] rightOne = intervals[i + 1];
if (leftOne[1] >= rightOne[0]) {
int[] combineOne = new int[2];
combineOne[0] = leftOne[0];
combineOne[1] = leftOne[1] > rightOne[1] ? leftOne[1] : rightOne[1];
midleValue[returnValueIndex] = combineOne;
} else {
midleValue[returnValueIndex++] = leftOne;
midleValue[returnValueIndex] = rightOne;
}
}
int[][] returnValue = new int[returnValueIndex + 1][];
for (int i = 0; i <= returnValueIndex; i++) {
returnValue[i] = midleValue[i];
}
return returnValue;
}
public static void main(String[] args) {
int[][] intervals = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
System.out.println(Arrays.deepToString(merge(intervals)));
}
}
public class ValidAbbr {
static boolean valid(String word, String abbr) {
char[] words = word.toCharArray();
char[] abbrs = abbr.toCharArray();
String numberTemp = "";
int wordsIndex = 0;
for (char temp : abbrs) {
if (Character.isDigit(temp)) {
numberTemp += temp;
continue;
} else {
if (!"".equals(numberTemp)) {
wordsIndex += Integer.parseInt(numberTemp);
numberTemp = "";
}
if (wordsIndex >= words.length) {
return false;
}
if (temp == words[wordsIndex]) {
wordsIndex++;
continue;
} else {
return false;
}
}
}
return wordsIndex == words.length;
}
public static void main(String[] args) {
System.out.println(valid("internationalization", "i12iz4n"));
}
}