Unblocked Buildings Ahead

Time limit: 20ms
Memory limit: 256mb

-------------------------Copy the following code, complete it and submit---------------------

/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INF -1

// data structure of the stack, which is implemented by an array "arr"
struct stack {
    int capacity;
    int size;
    int* arr;
};

typedef struct stack* StackADT;

// Description: Create a stack with the capacity xxx
// Usage: StackADT yourStack = CreateStack(capacity)
StackADT CreateStack(int capacity) {
    StackADT s = (StackADT) malloc(sizeof(struct stack));
    s->arr = (int*) malloc(capacity * sizeof(int));
    s->capacity = capacity;
    s->size = 0;
    return s;
}

// Description: Return 1 if the stack is empty, and 0 otherwise
// Usage: int result = IsEmpty(yourStack);
int IsEmpty(StackADT s) {
    return s->size == 0;
}

// Description: Return 1 if the stack is full, and 0 otherwise
// Usage: int result = IsFull(yourStack);
int IsFull(StackADT s) {
    return s->size == s->capacity;
}

// Description: Push a value into the stack
// Usage: Push(yourStack, value);
// Tips: If it is full, do nothing.
void Push(StackADT s, int value) {
    // write your code here
    
}

// Description: Pop a value from the stack
// Usage: Pop(yourStack);
// Tips: If it is empty, do nothing.
void Pop(StackADT s) {
    // write your code here
    
}

// Description: Obtain the top value from the stack
// Usage: int topValue = Top(yourStack);
// Tips: Your should check if it is empty. If empty, you should return INF.
int Top(StackADT s) {
    // write your code here
    
}

// Description: Calulate the number of unblocked buildings that a new item can see
// Usage: int numOfBuildings = DoSomething(yourStack, newValue);
// Tips: Implement it with the stack. 
// Tips: You may push every item into the stack. 
// Tips: Before you push a new item into the stack, you may do something in order to maintain certain property of this stack.
// Tips: This property should help a new item calculate the number of unblocked buildings it can see.
int DoSomething(StackADT s, int value) {
    // write your code here
    
}

int main() {
    int n;
    scanf("%d", &n);
    StackADT s = CreateStack(n);

    int value;
    for(int i=0; i<n; i++) {
        scanf("%d", &value);
        printf("%d\n", DoSomething(s, value));
    }
}


-----------------------------------------End of Code-----------------------------------------
Description:
Given a series of the height of buildings, for each building, please calculate the number of unblocked buildings it can see ahead.
We say that the i-th building with the height h_i is blocked by the j-th building with the height h_j only if i < j and h_i <= h_j.
The unblocked building (the j-th building) that the i-th building can see ahead satisfies: (1) j < i (2) not blocked by any k-th buildings with k < i.
You must write an algorithm that runs in O(n) total time.

Input:
The first line contains one integer: N;
The second line contains N integers, h_0, h_1, ..., h_(N-1), each of which is the height of each buildings;
1 <= N <= 10^3
1 <= h_i <= 10^3

Output:
N integers, each of which is the number of unblocked buildings that the i-th building can see ahead;

Sample Input 1:
8
5 3 8 3 2 5 5 5

Sample Output 1:
0
1
2
1
2
3
2
2

Reason:
The 1-th output integer 0: 	
For the 1-th building 5, nothing is in front of it

The 2-th output integer 1: 
For the 2-th building 3, it can see one building ahead (5)

The 3-th output integer 2: 	
For the 3-th building 8, it can see two buildings ahead (5 3)

The 4-th output integer 1: 
For the 4-th building 3, it can see one building ahead (8) as the 1-th and 2-th buildings are blocked by the 3-th building

The 5-th output integer 2: 
For the 5-th building 2, it can see two buildings ahead (8 3)

The 6-th output integer 3: 
For the 6-th building 5, it can see three buildings ahead (8 3 2)

The 7-th output integer 2: 
For the 7-th building 5, it can see two buildings ahead (8 5) as the 4-th and 5-th buildings are blocked by the 6-th building 

The 8-th output integer 2: 
For the 8-th building 5, it can see two buildings ahead (8 5) as the 6-th building is blocked by the 7-th building

Hints: 
The description of the function, DoSomething, may be helpful.

Submit