<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>&#39;s and <code>1</code>&#39;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>&#39;s</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,0,0,1,0,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> In 1 move, nums could be [1,0,0,0,<u>1</u>,<u>1</u>] and have 2 consecutive 1&#39;s.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,0,0,0,0,0,1,1], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> 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>].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,0,1], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums already has 2 consecutive 1&#39;s.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums[i]</code> is <code>0</code> or <code>1</code>.</li>
	<li><code>1 &lt;= k &lt;= sum(nums)</code></li>
</ul>

class Solution:
    def minMoves(self, nums: List[int], k: int) -> int:
        right_ones = deque()
        n = len(nums)
        for i in range(0, n):
            if nums[i] == 1:
                right_ones.append(i)
        right_of_me = deque()
        left_of_me = deque()
        current_cost = 0
        lowest_cost = n * k
        for i in range(0, k):
            cur_i = right_ones.popleft()
            current_cost += cur_i - i
            right_of_me.append(cur_i)
        for me in range(0, n):
            #print("cost at {} = {}".format(me, current_cost))
            #print("using: {}, {}".format(left_of_me, right_of_me))
            if current_cost < lowest_cost:
                lowest_cost = current_cost
            if len(right_of_me) > 0 and right_of_me[0] == me:
                left_of_me.append(right_of_me.popleft())
                current_cost -= len(left_of_me)
                current_cost += len(right_of_me)
            current_cost += len(left_of_me)
            current_cost -= len(right_of_me)
            while len(right_ones) > 0 and len(left_of_me) > 0:
                right_distance = right_ones[0] - (me + 1 + len(right_of_me))
                left_distance = (me + 1 - len(left_of_me)) -  left_of_me[0]
                if right_distance <= left_distance:
                    current_cost += right_distance - left_distance
                    left_of_me.popleft()
                    right_of_me.append(right_ones.popleft())
                else:
                    break

        return lowest_cost

    def minMoves_bad(self, nums: List[int], k: int) -> int:
        right_ones = deque()
        n = len(nums)
        for i in range(0, n):
            if nums[i] == 1:
                right_ones.append(i)
        left_ones = deque()
        best_result = n * k

        for i in range(0, n):
            cur_result = 0
            #right_j = 0
            #left_j = len(left_ones) - 1
            right_offset = 0
            left_offset = 0
            for obtained in range(0, k):
                right_j = right_offset
                left_j = len(left_ones) - left_offset - 1
                left_price = None
                right_price = None
                if left_j >= 0:
                    left_price = (i - left_ones[left_j]) - left_offset - 1
                if right_j < len(right_ones):
                    right_price = (right_ones[right_j] - i) - right_offset
                if left_price is None:
                    cur_result += right_price
                    right_offset += 1
                elif right_price is None:
                    cur_result += left_price
                    left_offset += 1
                elif left_price < right_price:
                    cur_result += left_price
                    left_offset += 1
                else:
                    cur_result += right_price
                    right_offset += 1


            if cur_result < best_result:
                best_result = cur_result

            if len(right_ones) > 0 and right_ones[0] == i:
                left_ones.append(i)
                right_ones.popleft()
        return best_result
23800