Problem solution · Java

The Skyline Problem

The Skyline Problem: 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
57 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 The Skyline Problem, 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

  • 57 lines of Java from the credited upstream file 218.java.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 3 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 codeThe Skyline Problem · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public List<List<Integer>> getSkyline(int[][] buildings) {    final int n = buildings.length;    if (n == 0)      return new ArrayList<>();    if (n == 1) {      final int left = buildings[0][0];      final int right = buildings[0][1];      final int height = buildings[0][2];      List<List<Integer>> ans = new ArrayList<>();      ans.add(new ArrayList<>(List.of(left, height)));      ans.add(new ArrayList<>(List.of(right, 0)));      return ans;    }     List<List<Integer>> leftSkyline = getSkyline(Arrays.copyOfRange(buildings, 0, n / 2));    List<List<Integer>> rightSkyline = getSkyline(Arrays.copyOfRange(buildings, n / 2, n));    return merge(leftSkyline, rightSkyline);  }   private List<List<Integer>> merge(List<List<Integer>> left, List<List<Integer>> right) {    List<List<Integer>> ans = new ArrayList<>();    int i = 0; // left's index    int j = 0; // right's index    int leftY = 0;    int rightY = 0;     while (i < left.size() && j < right.size())      // Choose the point with the smaller x.      if (left.get(i).get(0) < right.get(j).get(0)) {        leftY = left.get(i).get(1); // Update the ongoing `leftY`.        addPoint(ans, left.get(i).get(0), Math.max(left.get(i++).get(1), rightY));      } else {        rightY = right.get(j).get(1); // Update the ongoing `rightY`.        addPoint(ans, right.get(j).get(0), Math.max(right.get(j++).get(1), leftY));      }     while (i < left.size())      addPoint(ans, left.get(i).get(0), left.get(i++).get(1));     while (j < right.size())      addPoint(ans, right.get(j).get(0), right.get(j++).get(1));     return ans;  }   private void addPoint(List<List<Integer>> ans, int x, int y) {    if (!ans.isEmpty() && ans.get(ans.size() - 1).get(0) == x) {      ans.get(ans.size() - 1).set(1, y);      return;    }    if (!ans.isEmpty() && ans.get(ans.size() - 1).get(1) == y)      return;    ans.add(new ArrayList<>(List.of(x, y)));  }} 

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 ↗