Skip to content

Create 2742-painting-the-walls.js #3979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Mar 30, 2025

Solved painting-the-walls

File(s) Added: 2742-painting-the-walls.js
Language(s) Used: JavaScript
Submission URL : https://leetcode.com/problems/painting-the-walls/submissions/1590922459/

Solved painting-the-walls
@@ -0,0 +1,27 @@
/**
* DP | Memoization
* Time O(n^2) | Space O(n^2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can optimize space

/**
 * DP - Bottom Up
 * Array - Tabulation
 * Time O(N^2) | Space O(N)
 * @param {number[]} cost
 * @param {number[]} time
 * @return {number}
 */
var paintWalls = (cost, time) => {
    const tabu = initTabu(cost);

    for (let i = 0; (i < cost.length); i++) {
        for (let remain = cost.length; (0 < remain); remain--) {
            const left = tabu[Math.max(((remain - 1) - time[i]), 0)] + cost[i];
            const right = tabu[remain];

            tabu[remain] = Math.min(left, right);
        }
    }

    return tabu[cost.length];
}

var initTabu = ({ length }) => {
    const tabu = new Array(length + 1).fill(Number.MAX_SAFE_INTEGER);

    tabu[0] = 0;

    return tabu;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants