内容中心

2026年质量好的农业灌溉泵站主流厂家对比评测-山东朝启电子科技有限公司

To solve this problem, we need to determine the minimum number of flips required to transform a binary sequence such that all 0s come before all 1s. Each flip operation can change a 0 to a 1 or vice versa.

Approach

The key insight is to consider all possible split points in the sequence where the left part should be all 0s and the right part should be all 1s. For each split point, we calculate the number of flips needed:

  1. Left Part: Count of 1s (since these need to be flipped to 0s).
  2. Right Part: Count of 0s (since these need to be flipped to 1s).

We then take the minimum of these values across all possible split points.

Steps to implement:

  1. Prefix Sum of 1s: Compute an array where each element at index i is the number of 1s from the start up to index i.
  2. Suffix Sum of 0s: Compute an array where each element at index i is the number of 0s from index i to the end.
  3. Calculate Costs: For each split point, compute the cost and find the minimum cost.

Solution Code

def min_flips_to_zeros_before_ones(arr):
    n = len(arr)
    if n <= 1:
        return 0

    # Compute prefix sum of 1s
    prefix_ones = [0] * n
    prefix_ones[0] = 1 if arr[0] == 1 else 0
    for i in range(1, n):
        prefix_ones[i] = prefix_ones[i-1] + (1 if arr[i] == 1 else 0)

    # Compute suffix sum of 0s
    suffix_zeros = [0] * n
    suffix_zeros[-1] = 1 if arr[-1] == 0 else 0
    for i in range(n-2, -1, -1):
        suffix_zeros[i] = suffix_zeros[i+1] + (1 if arr[i] == 0 else 0)

    min_cost = float('inf')
    # Case where all elements are 1s (split before first element)
    min_cost = min(min_cost, suffix_zeros[0])
    # Case where all elements are 0s (split after last element)
    min_cost = min(min_cost, prefix_ones[-1])
    # Cases where split is between elements
    for k in range(n-1):
        cost = prefix_ones[k] + suffix_zeros[k+1]
        if cost < min_cost:
            min_cost = cost

    return min_cost

# Example usage:
# arr = [0, 1, 0, 1]
# print(min_flips_to_zeros_before_ones(arr))  # Output: 1

Explanation

  1. Prefix Sum of 1s: This array helps quickly find the number of 1s in any subarray from the start to index i.
  2. Suffix Sum of 0s: This array helps quickly find the number of 0s in any subarray from index i to the end.
  3. Cost Calculation: For each split point k (from -1 to n-1):
    • k = -1: All elements should be 1s, so cost is the number of 0s in the entire array.
    • k = n-1: All elements should be 0s, so cost is the number of 1s in the entire array.
    • 0 <= k < n-1: Cost is the number of 1s in the left part (0 to k) plus the number of 0s in the right part (k+1 to end).

This approach efficiently computes the minimum flips in O(n) time, which is optimal for large sequences. The space complexity is O(n) due to the prefix and suffix arrays, which is manageable for most practical purposes.

山东朝启电子科技有限公司

山东朝启电子科技有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?