📌  相关文章
📜  使所有数组元素等于 1 所需的成本

📅  最后修改于: 2021-10-26 05:46:49             🧑  作者: Mango

给定一个大小为N的二进制数组arr[] ,任务是找到使所有数组元素等于1所需的总成本,其中将任何0转换为1的成本等于存在于0之前的1的计数。

例子:

朴素方法:解决给定问题的最简单方法是遍历数组arr[]并计算每个包含0 的索引之前存在的1的数量,并打印获得的所有成本的总和。

时间复杂度: O(N 2 )
辅助空间: O(1)

有效的方法:上述方法可以通过观察以下事实进行优化:在将每个0转换为1 之后,每个0之前存在的1的计数由0出现的索引给出。因此,任务是遍历给定的数组并打印数组arr[] 中具有0 s 的所有索引的所有总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the cost required
// to make all array elements equal to 1
int findCost(int A[], int N)
{
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If current element is 0
        if (A[i] == 0) {
 
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 1, 0, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << findCost(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
 
class GFG{
 
// Function to calculate the cost required
// to make all array elements equal to 1
static int findCost(int[] A, int N)
{
     
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If current element is 0
        if (A[i] == 0)
        {
             
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 0, 1, 0, 1, 0 };
    int N = arr.length;
 
    System.out.println(findCost(arr, N));
}
}
 
// This code is contributed by ukasp


Python3
# Python3 program for the above approach
 
# Function to calculate the cost required
# to make all array elements equal to 1
def findCost(A, N):
 
    # Stores the total cost
    totalCost = 0
 
    # Traverse the array arr[]
    for i in range(N):
 
        # If current element is 0
        if (A[i] == 0):
 
            # Convert 0 to 1
            A[i] = 1
 
            # Add the cost
            totalCost += i
 
    # Return the total cost
    return totalCost
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 1, 0, 1, 0, 1, 0 ]
    N = len(arr)
     
    print(findCost(arr, N))
     
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to calculate the cost required
// to make all array elements equal to 1
static int findCost(int []A, int N)
{
     
    // Stores the total cost
    int totalCost = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If current element is 0
        if (A[i] == 0)
        {
 
            // Convert 0 to 1
            A[i] = 1;
 
            // Add the cost
            totalCost += i;
        }
    }
 
    // Return the total cost
    return totalCost;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 1, 0, 1, 0, 1, 0 };
    int N = arr.Length;
     
    Console.Write(findCost(arr, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
9

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程