1703. Minimum Adjacent Swaps for K Consecutive Ones Hard
1/**
2 * [1703] Minimum Adjacent Swaps for K Consecutive Ones
3 *
4 * 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.
5 * Return the minimum number of moves required so that nums has k consecutive 1's.
6 *
7 * Example 1:
8 *
9 * Input: nums = [1,0,0,1,0,1], k = 2
10 * Output: 1
11 * Explanation: In 1 move, nums could be [1,0,0,0,<u>1</u>,<u>1</u>] and have 2 consecutive 1's.
12 *
13 * Example 2:
14 *
15 * Input: nums = [1,0,0,0,0,0,1,1], k = 3
16 * Output: 5
17 * Explanation: In 5 moves, the leftmost 1 can be shifted right until nums = [0,0,0,0,0,<u>1</u>,<u>1</u>,<u>1</u>].
18 *
19 * Example 3:
20 *
21 * Input: nums = [1,1,0,1], k = 2
22 * Output: 0
23 * Explanation: nums already has 2 consecutive 1's.
24 *
25 *
26 * Constraints:
27 *
28 * 1 <= nums.length <= 10^5
29 * nums[i] is 0 or 1.
30 * 1 <= k <= sum(nums)
31 *
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/
36// discuss: https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41 pub fn min_moves(nums: Vec<i32>, k: i32) -> i32 {
42 0
43 }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_1703() {
54 }
55}
56
Back
© 2025 bowen.ge All Rights Reserved.