Problem Β· Greedy
● MediumMicrosoftFULLTIMEOA
See Microsoft hiring insights

Problem statement

You are gien an array A of N integers, representing the maximum heights of N skyscrapers to be built.

Your task if to specify the actual heights of the skyscrapers, given that:

  • the height of the K-th skyscraper should be positive and not bigger than A[K]
  • no two skyscrapers should be of the same height
  • the toal sum of the skyscrapers' heights should be the maximum possible
  • Given an array A of N integers, returns an array B of N integers where B[K] is the assigned height of the K-th skyscraper satisfying the above conditions.

    If there are several possible answers, the function may return any of them. You may assume that it is always possible to build all skyscrapers while fulfilling all the requirements.

    Function

    skyscraper(A: int[]) β†’ int[]

    Examples

    Example 1

    A = [1, 2, 3]return = [1, 2, 3]
    As all of the skyscrapers may be built to their maximum height.

    Example 2

    A = [9, 4, 3, 7, 7]return = [9, 4, 3, 7, 6]
    Note that [9, 4, 3, 6, 7] is also a valid answer. It is not possible for the last two skyscrapers to have the same height. The height of one of them should be 7 and the other should be 6.

    Example 3

    A = [2, 5, 4, 5, 5]return = [1, 2, 3, 4, 5]
    N/A

    Constraints

  • N is an integer within the range [1..50,000]
  • Each element of array A is an integer within the range [1..1,000,000,000]
  • There is always a solution for the given input
  • More Microsoft problems

    drafts saved locally
    public int[] skyscraper(int[] A) {
      // write your code here
    }
    
    A[1, 2, 3]
    expected[1, 2, 3]
    checking account