sorting - Optimized algorithm in finding the visible building -


i have problem in finding number of buildings visible , provided heights of buildings 3,5,1,8,5, 9,7,4. buildings in single row , user stands in front of building height 3.

the setup in side view

the simplest of solutions perform search identify buildings taller ones in front of them.

  • start
  • create array , load building heights [array1]
  • create array same length first , initialize array slots '0' [array2]
  • declare , initialize variables currenthighestbuilding = 0, y = 0
  • for each item in array1 loop
    • if array1[x] > currenthighestbuilding
      • currenthighestbuilding = array1[x]
      • array2[y] = currenthighestbuilding
      • y = y + 1
    • end if
  • end loop
  • print array2 contents (all non-zero values visible buildings)
  • end

out of curiosity, question require consider angle of view well? assume not, because there no information provided regarding distance of viewer , buildings.

assumptions: assume buildings of same width, aligned in straight line, spaced @ equal intervals , user standing dead in front of them.

edit: if need count

  • start
  • create array , load building heights [array1]
  • declare , initialize variables currenthighestbuilding = 0, count = 0
  • for each item in array1 loop
    • if array1[x] > currenthighestbuilding
      • currenthighestbuilding = array1[x]
      • count = count + 1
    • end if
  • end loop
  • print count
  • end

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -