java.lang.Object
g1701_1800.s1703_minimum_adjacent_swaps_for_k_consecutive_ones.Solution

public class Solution extends Object
1703 - Minimum Adjacent Swaps for K Consecutive Ones.<p>Hard</p> <p>You are given an integer array, <code>nums</code>, and an integer <code>k</code>. <code>nums</code> comprises of only <code>0</code>&rsquo;s and <code>1</code>&rsquo;s. In one move, you can choose two <strong>adjacent</strong> indices and swap their values.</p> <p>Return <em>the <strong>minimum</strong> number of moves required so that</em> <code>nums</code> <em>has</em> <code>k</code> <em><strong>consecutive</strong></em> <code>1</code><em>&rsquo;s</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,0,0,1,0,1], k = 2</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> In 1 move, nums could be [1,0,0,0,1,1] and have 2 consecutive 1&rsquo;s.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,0,0,0,0,0,1,1], k = 3</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> In 5 moves, the leftmost 1 can be shifted right until nums = [0,0,0,0,0,1,1,1].</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,1,0,1], k = 2</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> nums already has 2 consecutive 1&rsquo;s.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>nums[i]</code> is <code>0</code> or <code>1</code>.</li> <li><code>1 <= k <= sum(nums)</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minMoves

      public int minMoves(int[] nums, int k)