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

Commit90acab5

Browse files
committed
Add solution #2448
1 parent0d50f59 commit90acab5

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#1,955 LeetCode solutions in JavaScript
1+
#1,956 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1848,6 +1848,7 @@
18481848
2444|[Count Subarrays With Fixed Bounds](./solutions/2444-count-subarrays-with-fixed-bounds.js)|Hard|
18491849
2446|[Determine if Two Events Have Conflict](./solutions/2446-determine-if-two-events-have-conflict.js)|Easy|
18501850
2447|[Number of Subarrays With GCD Equal to K](./solutions/2447-number-of-subarrays-with-gcd-equal-to-k.js)|Medium|
1851+
2448|[Minimum Cost to Make Array Equal](./solutions/2448-minimum-cost-to-make-array-equal.js)|Hard|
18511852
2460|[Apply Operations to an Array](./solutions/2460-apply-operations-to-an-array.js)|Easy|
18521853
2462|[Total Cost to Hire K Workers](./solutions/2462-total-cost-to-hire-k-workers.js)|Medium|
18531854
2467|[Most Profitable Path in a Tree](./solutions/2467-most-profitable-path-in-a-tree.js)|Medium|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 2448. Minimum Cost to Make Array Equal
3+
* https://leetcode.com/problems/minimum-cost-to-make-array-equal/
4+
* Difficulty: Hard
5+
*
6+
* You are given two 0-indexed arrays nums and cost consisting each of n positive integers.
7+
*
8+
* You can do the following operation any number of times:
9+
* - Increase or decrease any element of the array nums by 1.
10+
*
11+
* The cost of doing one operation on the ith element is cost[i].
12+
*
13+
* Return the minimum total cost such that all the elements of the array nums become equal.
14+
*/
15+
16+
/**
17+
*@param {number[]} nums
18+
*@param {number[]} cost
19+
*@return {number}
20+
*/
21+
varminCost=function(nums,cost){
22+
constpairs=nums.map((num,i)=>[num,cost[i]]).sort((a,b)=>a[0]-b[0]);
23+
constn=nums.length;
24+
constprefixSum=Array(n).fill(0);
25+
constprefixCost=Array(n).fill(0);
26+
27+
prefixSum[0]=pairs[0][0]*pairs[0][1];
28+
prefixCost[0]=pairs[0][1];
29+
30+
for(leti=1;i<n;i++){
31+
prefixSum[i]=prefixSum[i-1]+pairs[i][0]*pairs[i][1];
32+
prefixCost[i]=prefixCost[i-1]+pairs[i][1];
33+
}
34+
35+
letresult=Infinity;
36+
for(leti=0;i<n;i++){
37+
consttarget=pairs[i][0];
38+
constleftCost=i>0 ?target*prefixCost[i-1]-prefixSum[i-1] :0;
39+
constrightCost=i<n-1 ?prefixSum[n-1]-prefixSum[i]
40+
-target*(prefixCost[n-1]-prefixCost[i]) :0;
41+
result=Math.min(result,leftCost+rightCost);
42+
}
43+
44+
returnresult;
45+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp