Skip to content

Commit eed3795

Browse files
committed
Add solution #1703
1 parent 39913f2 commit eed3795

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-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,487 LeetCode solutions in JavaScript
1+
# 1,488 LeetCode solutions in JavaScript
22

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

@@ -1312,6 +1312,7 @@
13121312
1700|[Number of Students Unable to Eat Lunch](./solutions/1700-number-of-students-unable-to-eat-lunch.js)|Easy|
13131313
1701|[Average Waiting Time](./solutions/1701-average-waiting-time.js)|Medium|
13141314
1702|[Maximum Binary String After Change](./solutions/1702-maximum-binary-string-after-change.js)|Medium|
1315+
1703|[Minimum Adjacent Swaps for K Consecutive Ones](./solutions/1703-minimum-adjacent-swaps-for-k-consecutive-ones.js)|Hard|
13151316
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13161317
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13171318
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1703. Minimum Adjacent Swaps for K Consecutive Ones
3+
* https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array, nums, and an integer k. nums comprises of only 0's and 1's. In
7+
* one move, you can choose two adjacent indices and swap their values.
8+
*
9+
* Return the minimum number of moves required so that nums has k consecutive 1's.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @param {number} k
15+
* @return {number}
16+
*/
17+
var minMoves = function(nums, k) {
18+
const ones = [];
19+
for (let i = 0; i < nums.length; i++) {
20+
if (nums[i] === 1) ones.push(i);
21+
}
22+
23+
const prefixSum = [0];
24+
for (let i = 0; i < ones.length; i++) {
25+
prefixSum.push(prefixSum[i] + ones[i]);
26+
}
27+
28+
let minSwaps = Infinity;
29+
for (let i = 0; i <= ones.length - k; i++) {
30+
const j = i + k - 1;
31+
const mid = i + Math.floor(k / 2);
32+
const target = ones[mid];
33+
const leftCount = mid - i;
34+
const rightCount = j - mid;
35+
const leftSum = prefixSum[mid] - prefixSum[i];
36+
const rightSum = prefixSum[j + 1] - prefixSum[mid + 1];
37+
const swaps = (target * leftCount - leftSum) + (rightSum - target * rightCount);
38+
minSwaps = Math.min(minSwaps, swaps);
39+
}
40+
41+
const medianAdjust = Math.floor(k / 2) * Math.ceil(k / 2);
42+
return minSwaps - medianAdjust;
43+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy