Untitled
package com.sararf; public class TestAS { // You are given a tower and a series of buildings in a straight row in front of the tower. // Each building has a different number of floors. The goal is to determine the highest floor of // each building from which the tower is visible. // // Visibility is defined as: // // A building blocks the visibility of another building (and the tower) if it is taller or equal // to the current building and is closer to the tower. // The tower is visible from a building if there are no taller buildings in front of it when // viewed from that building towards the tower. // Tower Height: 5 // Buildings: [[1,3], --> 3 // [2,6] --> 4 // [3,2] --> nothing // [4,8] --> 7 // [5,5]] --> nothing public static void main(String[] args) { int th = 5; int[][] buildings = {new int[]{1, 3}, new int[]{2, 6}, new int[]{3, 2}, new int[]{4, 8}, new int[]{5, 5}}; int[][] res = findView(th, buildings); for (int[] r : res) { System.out.println("b = " + r[0] + " v=" + r[1]); } } public static int[][] findView(int th, int[][] buildings) { int top = -1; int[][] result = new int[buildings.length][2]; int i = 0; for (int[] building : buildings) { if (top == -1) { top = building[1]; result[i][0] = building[0]; result[i][1] = building[1]; i++; continue; } if (top >= building[1]) { result[i][0] = building[0]; result[i][1] = -1; } else { result[i][0] = building[0]; result[i][1] = top + 1; top = building[1]; } i++; } return result; } }
Leave a Comment