Problem 7. New York has many tall buildings, but only some of them have a clear view of Hudson River. Suppose we are giv
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Problem 7. New York has many tall buildings, but only some of them have a clear view of Hudson River. Suppose we are giv
Problem 7. New York has many tall buildings, but only some of them have a clear view of Hudson River. Suppose we are given an array A1,..., n] that stores the height of n buildings on a city block, indexed from east to west. Building i has a good view of Hudson River if and only if every building to the west of i is shorter than i. Here is an algorithm that computes which buildings have a good view of Hudson River. Analyze the correctness and running time of the algorithm. Algorithm: GoodView (A[1,..., n]) 1: Initialize a stack S 2: for i = 1,...,n do 3: while S is not empty and A > Aſtop(S)] do 4: Pop(S) 5: end while 6: Push(S,) 7: end for 8: return s
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!