Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 27d4b5f

Browse files
committed
Add solution #1674
1 parent 8a4561b commit 27d4b5f

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,466 LeetCode solutions in JavaScript
1+
# 1,467 LeetCode solutions in JavaScript
22

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

@@ -1290,6 +1290,7 @@
12901290
1671|[Minimum Number of Removals to Make Mountain Array](./solutions/1671-minimum-number-of-removals-to-make-mountain-array.js)|Hard|
12911291
1672|[Richest Customer Wealth](./solutions/1672-richest-customer-wealth.js)|Easy|
12921292
1673|[Find the Most Competitive Subsequence](./solutions/1673-find-the-most-competitive-subsequence.js)|Medium|
1293+
1674|[Minimum Moves to Make Array Complementary](./solutions/1674-minimum-moves-to-make-array-complementary.js)|Medium|
12931294
1679|[Max Number of K-Sum Pairs](./solutions/1679-max-number-of-k-sum-pairs.js)|Medium|
12941295
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
12951296
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 1674. Minimum Moves to Make Array Complementary
3+
* https://leetcode.com/problems/minimum-moves-to-make-array-complementary/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums of even length n and an integer limit. In one move, you
7+
* can replace any integer from nums with another integer between 1 and limit, inclusive.
8+
*
9+
* The array nums is complementary if for all indices i (0-indexed), nums[i] + nums[n - 1 - i]
10+
* equals the same number. For example, the array [1,2,3,4] is complementary because for all
11+
* indices i, nums[i] + nums[n - 1 - i] = 5.
12+
*
13+
* Return the minimum number of moves required to make nums complementary.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @param {number} limit
19+
* @return {number}
20+
*/
21+
var minMoves = function(nums, limit) {
22+
const n = nums.length;
23+
const delta = new Array(2 * limit + 2).fill(0);
24+
let result = n;
25+
26+
for (let i = 0; i < n / 2; i++) {
27+
const left = nums[i];
28+
const right = nums[n - 1 - i];
29+
const minSum = Math.min(left, right) + 1;
30+
const maxSum = Math.max(left, right) + limit;
31+
delta[2] += 2;
32+
delta[minSum] -= 1;
33+
delta[left + right] -= 1;
34+
delta[left + right + 1] += 1;
35+
delta[maxSum + 1] += 1;
36+
}
37+
38+
let moves = 0;
39+
for (let i = 2; i <= 2 * limit; i++) {
40+
moves += delta[i];
41+
result = Math.min(result, moves);
42+
}
43+
44+
return result;
45+
};

0 commit comments

Comments
 (0)