Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit68a19de

Browse files
committed
Add solution #2742
1 parent5d52468 commit68a19de

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,7 @@
20182018
2733|[Neither Minimum nor Maximum](./solutions/2733-neither-minimum-nor-maximum.js)|Easy|
20192019
2739|[Total Distance Traveled](./solutions/2739-total-distance-traveled.js)|Easy|
20202020
2740|[Find the Value of the Partition](./solutions/2740-find-the-value-of-the-partition.js)|Medium|
2021+
2742|[Painting the Walls](./solutions/2742-painting-the-walls.js)|Hard|
20212022
2780|[Minimum Index of a Valid Split](./solutions/2780-minimum-index-of-a-valid-split.js)|Medium|
20222023
2799|[Count Complete Subarrays in an Array](./solutions/2799-count-complete-subarrays-in-an-array.js)|Medium|
20232024
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|

‎solutions/2742-painting-the-walls.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 2742. Painting the Walls
3+
* https://leetcode.com/problems/painting-the-walls/
4+
* Difficulty: Hard
5+
*
6+
* You are given two 0-indexed integer arrays, cost and time, of size n representing the costs
7+
* and the time taken to paint n different walls respectively. There are two painters available:
8+
* - A paid painter that paints the ith wall in time[i] units of time and takes cost[i] units of
9+
* money.
10+
* - A free painter that paints any wall in 1 unit of time at a cost of 0. But the free painter
11+
* can only be used if the paid painter is already occupied.
12+
*
13+
* Return the minimum amount of money required to paint the n walls.
14+
*/
15+
16+
/**
17+
*@param {number[]} cost
18+
*@param {number[]} time
19+
*@return {number}
20+
*/
21+
varpaintWalls=function(cost,time){
22+
constn=cost.length;
23+
constdp=newArray(n+1).fill().map(()=>newArray(n+1).fill(Infinity));
24+
dp[0][0]=0;
25+
26+
for(leti=0;i<n;i++){
27+
for(letj=0;j<=n;j++){
28+
if(dp[i][j]===Infinity)continue;
29+
30+
dp[i+1][j]=Math.min(dp[i+1][j],dp[i][j]);
31+
32+
constwalls=Math.min(n,j+time[i]+1);
33+
dp[i+1][walls]=Math.min(dp[i+1][walls],dp[i][j]+cost[i]);
34+
}
35+
}
36+
37+
returndp[n][n];
38+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp