lundi 25 mai 2020

C++ to javascript convert

I am unable to convert this following c++ code into javascript. It has some problems like stl conversion etc for me and I am unable to do it as i am not much good in javascript and have burnt my head over it since past weeks.

#include<bits/stdc++.h>
using namespace std;


int solve(vector<int>vect)
{
        int sz=vect.size(),i;

        int arr[sz];

        for(i=0;i<sz;i++)
        {
            arr[i]=1;
        }

        for(i=1;i<sz;i++)
        {
            if(vect[i]>vect[i-1])
            {
                arr[i]=arr[i-1]+1;
            }
        }

       int maxi=0;

       maxi=arr[sz-1];


        for(i=sz-2;i>=0;i--)
        {
            if(vect[i]>vect[i+1])
            {
                arr[i]=max(arr[i],arr[i+1]+1);
            }

            maxi+=arr[i];
        }

        return maxi;
}

int main()
{

    int start_s = clock();
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int i,n,e;

    cin>>n;

    vector<int>vect;

    for(i=0;i<n;i++)
    {
        cin>>e;

        vect.push_back(e);
    }

    cout<<solve(vect);
}

Please do the needful avoid the main function just check in the solved function in it

Aucun commentaire:

Enregistrer un commentaire