- Identify the ordered answer range or sorted search domain.
- Write a predicate whose truth changes only once.
- Move the appropriate boundary after each midpoint check and return the final feasible position.
Code notes
- 42 lines of Java from the credited upstream file 3288.java.
- The implementation visibly relies on sequence storage, ordered lookup.
- 2 loop blocks detected.
Complexity
Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int maxPathLength(int[][] coordinates, int k) {3 final int xk = coordinates[k][0];4 final int yk = coordinates[k][1];5 List<int[]> leftCoordinates = new ArrayList<>();6 List<int[]> rightCoordinates = new ArrayList<>();7 8 for (int[] coordinate : coordinates) {9 final int x = coordinate[0];10 final int y = coordinate[1];11 if (x < xk && y < yk)12 leftCoordinates.add(new int[] {x, y});13 else if (x > xk && y > yk)14 rightCoordinates.add(new int[] {x, y});15 }16 17 return 1 + lengthOfLIS(leftCoordinates) + lengthOfLIS(rightCoordinates);18 }19 20 21 private int lengthOfLIS(List<int[]> coordinates) {22 coordinates.sort(Comparator.comparingInt((int[] coordinate) -> coordinate[0])23 .thenComparingInt((int[] coordinate) -> - coordinate[1]));24 25 26 List<Integer> tails = new ArrayList<>();27 for (int[] coordinate : coordinates) {28 final int y = coordinate[1];29 if (tails.isEmpty() || y > tails.get(tails.size() - 1))30 tails.add(y);31 else32 tails.set(firstGreaterEqual(tails, y), y);33 }34 return tails.size();35 }36 37 private int firstGreaterEqual(List<Integer> arr, int target) {38 final int i = Collections.binarySearch(arr, target);39 return i < 0 ? -i - 1 : i;40 }41}42