mardi 23 juin 2015

C++ Hackerrank Solution - Jim and the Skyscrapers [on hold]

I've tried searching for a solution to this problem (http://ift.tt/1IUKvA2), trying it out a few times on my own as well. Here is my code:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
long N;
cin>>N;
long B[N];

for(long i = 0;i<N;i++) {
    cin>>B[i];
}

    int k = 0;
for(long i = 0;i<N;i++) {
    long tmp = B[i];
    long j = i + 1;
    while(B[j] <= B[i]) {
        if(B[j] == B[i]) k++;
        j++;
    }
}

cout<<k * 2;
return 0;
}

I keep getting timeouts in 5 testcases. I would appreciate it if someone could help me out. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire