Problem solution · Java

Length of the Longest Increasing Path

Length of the Longest Increasing Path: a Java solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Binary search
Source
walkccc LeetCode Solutions
Length
42 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Length of the Longest Increasing Path, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. 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.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeLength of the Longest Increasing Path · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int maxPathLength(int[][] coordinates, int k) {    final int xk = coordinates[k][0];    final int yk = coordinates[k][1];    List<int[]> leftCoordinates = new ArrayList<>();    List<int[]> rightCoordinates = new ArrayList<>();     for (int[] coordinate : coordinates) {      final int x = coordinate[0];      final int y = coordinate[1];      if (x < xk && y < yk)        leftCoordinates.add(new int[] {x, y});      else if (x > xk && y > yk)        rightCoordinates.add(new int[] {x, y});    }     return 1 + lengthOfLIS(leftCoordinates) + lengthOfLIS(rightCoordinates);  }   // Similar to 300. Longest Increasing Subsequence  private int lengthOfLIS(List<int[]> coordinates) {    coordinates.sort(Comparator.comparingInt((int[] coordinate) -> coordinate[0])                         .thenComparingInt((int[] coordinate) -> - coordinate[1]));    // tails[i] := the minimum tail of all the increasing subsequences having    // length i + 1    List<Integer> tails = new ArrayList<>();    for (int[] coordinate : coordinates) {      final int y = coordinate[1];      if (tails.isEmpty() || y > tails.get(tails.size() - 1))        tails.add(y);      else        tails.set(firstGreaterEqual(tails, y), y);    }    return tails.size();  }   private int firstGreaterEqual(List<Integer> arr, int target) {    final int i = Collections.binarySearch(arr, target);    return i < 0 ? -i - 1 : i;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗