Problem solution · Java

Maximize the Distance Between Points on a Square

Maximize the Distance Between Points on a Square: a Java solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
83 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

Sliding window or two pointers

For Maximize the Distance Between Points on a Square, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 83 lines of Java from the credited upstream file 3464.java.
  • The implementation visibly relies on sequence storage, work queue.
  • 4 loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

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 codeMaximize the Distance Between Points on a Square · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int maxDistance(int side, int[][] points, int k) {    List<int[]> ordered = getOrderedPoints(side, points);    int l = 0;    int r = side;     while (l < r) {      final int m = (l + r + 1) / 2;      if (isValidDistance(ordered, k, m))        l = m;      else        r = m - 1;    }     return l;  }   private record Sequence(int startX, int startY, int endX, int endY, int length) {}   // Returns true if we can select `k` points such that the minimum Manhattan  // distance between any two consecutive chosen points is at least `m`.  private boolean isValidDistance(List<int[]> ordered, int k, int d) {    Deque<Sequence> dq = new ArrayDeque<>(List.of(new Sequence(        ordered.get(0)[0], ordered.get(0)[1], ordered.get(0)[0], ordered.get(0)[1], 1)));    int maxLength = 1;     for (int i = 1; i < ordered.size(); ++i) {      final int x = ordered.get(i)[0];      final int y = ordered.get(i)[1];      int startX = x;      int startY = y;      int length = 1;      while (!dq.isEmpty() &&             (Math.abs(x - dq.peekFirst().endX()) + Math.abs(y - dq.peekFirst().endY()) >= d)) {        if (Math.abs(x - dq.peekFirst().startX()) + Math.abs(y - dq.peekFirst().startY()) >= d &&            dq.peekFirst().length() + 1 >= length) {          startX = dq.peekFirst().startX();          startY = dq.peekFirst().startY();          length = dq.peekFirst().length() + 1;          maxLength = Math.max(maxLength, length);        }        dq.pollFirst();      }      dq.addLast(new Sequence(startX, startY, x, y, length));    }     return maxLength >= k;  }   // Returns the ordered points on the perimeter of a square of side length  // `side`, starting from left, top, right, and bottom boundaries.  private List<int[]> getOrderedPoints(int side, int[][] points) {    List<int[]> left = new ArrayList<>();    List<int[]> top = new ArrayList<>();    List<int[]> right = new ArrayList<>();    List<int[]> bottom = new ArrayList<>();     for (int[] point : points) {      final int x = point[0];      final int y = point[1];      if (x == 0 && y > 0)        left.add(point);      else if (x > 0 && y == side)        top.add(point);      else if (x == side && y < side)        right.add(point);      else        bottom.add(point);    }     left.sort(Comparator.comparingInt(a -> a[1]));    top.sort(Comparator.comparingInt(a -> a[0]));    right.sort(Comparator.comparingInt(a -> - a[1]));    bottom.sort(Comparator.comparingInt(a -> - a[0]));    List<int[]> ordered = new ArrayList<>();    ordered.addAll(left);    ordered.addAll(top);    ordered.addAll(right);    ordered.addAll(bottom);    return ordered;  }} 

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 ↗