给定一个大小为N的二进制数组arr[] ,任务是找到使所有数组元素等于1所需的总成本,其中将任何0转换为1的成本等于存在于0之前的1的计数。
例子:
Input: arr[] = {1, 0, 1, 0, 1, 0}
Output: 9
Explanation:
Following operations are performed:
- Converting arr[1] to 1 modifies arr[] to {1, 1, 1, 0, 1, 0}. Cost = 1.
- Converting arr[3] to 1 modifies arr[] to {1, 1, 1, 1, 1, 0}. Cost = 3.
- Converting arr[5] to 1 modifies arr[] to {1, 1, 1, 1, 1, 5}. Cost = 5.
Therefore, the total cost is 1 + 3 + 5 = 9.
Input: arr[] = {1, 1, 1}
Output: 0
朴素方法:解决给定问题的最简单方法是遍历数组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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live