1703. Minimum Adjacent Swaps for K Consecutive Ones LeetCode Solution
In this guide, you will get 1703. Minimum Adjacent Swaps for K Consecutive Ones LeetCode Solution with the best time and space complexity. The solution to Minimum Adjacent Swaps for K Consecutive Ones problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Adjacent Swaps for K Consecutive Ones solution in C++
- Minimum Adjacent Swaps for K Consecutive Ones solution in Java
- Minimum Adjacent Swaps for K Consecutive Ones solution in Python
- Additional Resources
Problem Statement of Minimum Adjacent Swaps for K Consecutive Ones
You are given an integer array, nums, and an integer k. nums comprises of only 0’s and 1’s. In one move, you can choose two adjacent indices and swap their values.
Return the minimum number of moves required so that nums has k consecutive 1’s.
Example 1:
Input: nums = [1,0,0,1,0,1], k = 2
Output: 1
Explanation: In 1 move, nums could be [1,0,0,0,1,1] and have 2 consecutive 1’s.
Example 2:
Input: nums = [1,0,0,0,0,0,1,1], k = 3
Output: 5
Explanation: In 5 moves, the leftmost 1 can be shifted right until nums = [0,0,0,0,0,1,1,1].
Example 3:
Input: nums = [1,1,0,1], k = 2
Output: 0
Explanation: nums already has 2 consecutive 1’s.
Constraints:
1 <= nums.length <= 105
nums[i] is 0 or 1.
1 <= k <= sum(nums)
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
1703. Minimum Adjacent Swaps for K Consecutive Ones LeetCode Solution in C++
class Solution {
public:
int minMoves(vector<int>& nums, int k) {
vector<int> ones;
for (int i = 0; i < nums.size(); ++i)
if (nums[i] == 1)
ones.push_back(i);
// Returns the median index of [i..i + k).
auto getMedIndex = [&](int i) { return (i + (i + k - 1)) / 2; };
// Calculate the first group: window[0] = A[0..k).
const int median = ones[getMedIndex(0)];
int moves = 0;
for (int i = 0; i < k; ++i)
moves += abs(ones[i] - median);
int ans = moves;
for (int i = 1; i <= ones.size() - k; ++i) {
const int oldMedianIndex = ones[getMedIndex(i - 1)];
const int newMedianIndex = ones[getMedIndex(i)];
if (k % 2 == 1)
moves += newMedianIndex - oldMedianIndex;
moves -= newMedianIndex - ones[i - 1];
moves += ones[i + k - 1] - newMedianIndex;
ans = min(ans, moves);
}
auto nThSum = [&](int n) { return n * (n + 1) / 2; };
return ans - nThSum((k - 1) / 2) - nThSum(k / 2);
}
};
/* code provided by PROGIEZ */
1703. Minimum Adjacent Swaps for K Consecutive Ones LeetCode Solution in Java
class Solution {
public int minMoves(int[] nums, int k) {
List<Integer> ones = new ArrayList<>();
for (int i = 0; i < nums.length; ++i)
if (nums[i] == 1)
ones.add(i);
final int median = ones.get(getMedIndex(0, k));
int moves = 0;
for (int i = 0; i < k; ++i)
moves += Math.abs(ones.get(i) - median);
int ans = moves;
for (int i = 1; i <= ones.size() - k; ++i) {
final int oldMedianIndex = ones.get(getMedIndex(i - 1, k));
final int newMedianIndex = ones.get(getMedIndex(i, k));
if (k % 2 == 1)
moves += newMedianIndex - oldMedianIndex;
moves -= newMedianIndex - ones.get(i - 1);
moves += ones.get(i + k - 1) - newMedianIndex;
ans = Math.min(ans, moves);
}
return ans - nThSum((k - 1) / 2) - nThSum(k / 2);
}
// Returns the median index of [i..i + k).
private int getMedIndex(int i, int k) {
return (i + (i + k - 1)) / 2;
}
// Returns 1 + 2 + ... + n
private int nThSum(int n) {
return n * (n + 1) / 2;
}
}
// code provided by PROGIEZ
1703. Minimum Adjacent Swaps for K Consecutive Ones LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.