Coder Social home page Coder Social logo

problem-solving's Introduction

problem-solving

Solutions for programming problems

problem-solving's People

Contributors

gardusig avatar

Stargazers

 avatar

Watchers

 avatar

problem-solving's Issues

Implement my own solution to interview problem

Problem: https://github.com/gardusig/road2senior/tree/main/solutions/real-questions/count_stock_intervals

Reference:

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

int n;
vector<int> R2;
vector<long long> memo;
long long dp(int i){
    if(i == n)
        return 0;
    long long& ans = memo[i];
    if(ans != -1)
        return ans;
    return ans = dp(R2[i]) + 1;
}

long long solve(vector<int> arr){
    // Para cada i, encontrar o primeiro j > i tal que a[j] > a[i]
    n = arr.size();
    stack<int> st;
    vector<int> R(n);
    for(int i = n - 1; i >= 0; i--){
        while(!st.empty() && arr[st.top()] <= arr[i])
            st.pop();
        if(st.empty())
            R[i] = n;
        else
            R[i] = st.top();
        st.push(i);
    }
    
    long long ans = 0;
    for(int i = 0; i < n; i++)
        ans += R[i] - i;

    // segundo caso
    R2 = vector<int>(n);
    while(!st.empty())
        st.pop();

    for(int i = n - 1; i >= 0; i--){
        while(!st.empty() && arr[st.top()] < arr[i])
            st.pop();
        if(st.empty())
            R2[i] = n;
        else
            R2[i] = st.top();
        st.push(i);
    }

    memo = vector<long long>(n, -1);
    for(int i = 0; i < n; i++)
        ans += dp(R[i]);

    return ans;
}

long long solve_brute(vector<int> arr){
    long long ans = 0;
    int n = arr.size();
    for(int i = 0; i < n; i++)
        for(int j = i; j < n; j++){
            int greater = arr[i];
            for(int k = i + 1; k <= j; k++)
                greater = max(greater, arr[k]);
            if(greater == arr[i] || greater == arr[j])
                ans++;
        }
    
    return ans;
}

int main(){
    vector<int> arr{1, 2, 1, 2};
    assert(solve(arr) == solve_brute(arr));

    arr = vector<int>{1, 3, 2};
    assert(solve(arr) == solve_brute(arr));

    arr = vector<int>{1, 2, 3, 4, (int)1e9, 42, 69, 0, -4, 23, 0, 22, (int)-1e8};
    assert(solve(arr) == solve_brute(arr));

    return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.