1

I have been trying to formulate an algorithm to solve a problem. In this problem, we have a photo containing some buildings. The photo is divided into n vertical regions (called pieces) and the height of a building in each piece is given.

One building may span several consecutive pieces, but each piece can only contain one visible building, or no buildings at all. We are required to find the minimum number of buildings.

e.g. given ,

3 ( no of pieces)

1 2 3 ( heights) ans = 3

3

1 2 1 ans = 2

6

1 2 3 1 2 3 ans = 5 ( a figure wud help show the overlap.).

Though I feel like I get it, I am unable to get a solid algorithm for it. Any ideas?

8
  • can you please explain the 2nd and 3rd examples? I must be missing something... Commented Jun 10, 2012 at 5:18
  • Is this homework? It sounds like it is to me. Commented Jun 10, 2012 at 5:19
  • What if the buildings are "drawn" on a grid each row represents a particular height while a column represents a "piece" -- does that help with the visualization / rules of approach? Also, work on cleaning up the initial data description :) Commented Jun 10, 2012 at 5:21
  • 2
    Here's a hint: a building with height 3 mandatorily ends whenever you get a height 2 or 1 in your input. As you want the minimal number, there's no reason for it to end any sooner. Keeping the buildings that still haven't ended looks like a job for a stack... Commented Jun 10, 2012 at 5:28
  • @frodo, Can you be lil bit more explanatory in examples that you stated? Commented Jun 10, 2012 at 6:57

1 Answer 1

0

You can find the lowest number from the given array and account for all occurances of this number. This will split the array into multiple subarrays and now you need to recursively solve the problem for each of them.

In the example:

1 2 3 1 2 3 (total = 0)

Smallest number is 1:

x 2 3 x 2 3 (total = 1)

Now you have 2 subarrays. Solve for the first one - the smallest number is 2:

  x 3       (total = 2)

Finally you have a single element: total = 3 Solving the other subarray makes it 5.

Here is some code in C#:

int Solve(int[] ar, int start, int end){
    //base for the recursion -> the subarray has single element
    if(end-start == 1) return 1;

    //base for the recursion -> the subarray is empty
    if(end-start < 1) return 0;

    //find min
    int m = int.MaxValue;
    for(int i = start; i < end; i++)
        if (ar[i] < m) m = ar[i];

    int total = 1;
    //find the subarrays and their contribution recursively
    int subStart = start;
    for(int subEnd = start; subEnd < end; subEnd++){
        if(ar[subEnd] == m) {
            total += Solve(ar, subStart, subEnd);
            subStart = subEnd + 1;
        }
    }
    total += Solve(ar, subStart, ar.Length);

    return total;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your reply. But im not sure i grasp the idea completely. Could you explain a bit? Thanks.
I did a little more work, and this algorithm does not handle the case where there could be empty spots in the pieces. Ideas anyone on that?
Thanks a lot. Just solved it with necessary modifications for empty spots. Could you kindly justify the reason for doing this approach.?
This algorithm works in quadratic time on the worst case (the heights are given in sorted order), so I'm really surprised it works on the online judge.
I guess the tests are weak. I had a pretty decent running time actually.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.