Question: Webstaff, Web Developer Test, Online Assignment Test (20/9/2023) | Hiring Drive
0
Entering edit mode

Entering edit mode
1

can u please tell other questions which came ?!

ADD REPLYlink 12 months ago
JAYESH BHOJAWAT
• 20
0
Entering edit mode

Code of above ⬆
it will give tle please optimize it.


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

int solve(int n, vector<int>& front, vector<int>& back, int frontend, int backend) {
    vector<vector<int>> dp(n + 1, vector<int>(frontend + 1, INT_MAX));
    dp[0][0] = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= frontend; j++) {
            if (dp[i][j] == INT_MAX) continue;
            if (j < frontend) {
                dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + front[i]);
            }
            int b = i - j;
            if (b < backend) {
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + back[i]);
            }
        }
    }

    return dp[n][frontend];
}

int32_t main() {
    int frontend, backend;
    cin >> frontend >> backend;
    int n = frontend + backend;
    vector<int> front(n), back(n);
    
    for (int i = 0; i < n; i++) {
        cin >> front[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> back[i];
    }

    int result = solve(n, front, back, frontend, backend);
    cout << result << endl;

    return 0;
}

//input ke according function name change kar lena

ADD COMMENTlink 23 days ago Mousam Kumari • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts