Advertisement
darekfive

Buildings with an ocean view both side view update

Mar 2nd, 2025
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Solution {
  4.     public List<Integer> findBuildingViewCount(int[] heights) {
  5.         int left = 0, right = heights.length - 1;
  6.         Deque<Integer> deque = new ArrayDeque<>();
  7.         deque.addFirst(left);
  8.         deque.addLast(right);
  9.  
  10.         while (left < right) {
  11.             if (heights[left] < heights[right]) {
  12.                 left++;
  13.                 if (heights[left] > heights[deque.peekFirst()]) {
  14.                     deque.addFirst(left);
  15.                 }
  16.             } else {
  17.                 right--;
  18.                 if (heights[right] > heights[deque.peekLast()]) {
  19.                     deque.addLast(right);
  20.                 }
  21.             }
  22.         }
  23.  
  24.         return new ArrayList<>(deque);
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement