给定一个二元数组,其中将元素从索引i移动到索引j需要 abs(i – j) 成本。任务是找到将所有1移动到给定数组的每个索引的成本。
例子:
Input: arr[] = {0, 1, 0, 1}
Output: 4 2 2 2
Explanation:
Moving elements from index 1, index 3 to index 0 requires abs(0 – 1) + abs(0 – 3) = 4.
Moving elements from index 1, index 3 to index 1 requires abs(1 – 1) + abs(1 – 3) = 2.
Moving elements from index 1, index 2 to index 2 requires abs(2 – 1) + abs(2 – 3) = 2.
Moving elements from index 1, index 2 to index 3 requires abs(3 – 1) + abs(3 – 3) = 2.
Therefore, the required output is 4 2 2 2.
Input: arr[] = {1, 1, 1}
Output: 3 2 3
朴素的方法:解决这个问题的最简单的方法是遍历给定数组,对于给定数组的每个数组元素,打印将给定数组的所有1移动到当前索引的成本。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print the cost
// to move all 1s to each index
void costMove1s(int arr[], int N)
{
// Traverse the array.
for (int i = 0; i < N; i++) {
// Stores cost to move
// all 1s at current index
int cost = 0;
// Calculate cost to move
// all 1s at current index
for (int j = 0; j < N;
j++) {
// If current element
// of the array is 1.
if (arr[j] == 1) {
// Update cost
cost += abs(i - j);
}
}
cout<
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to print the cost
// to move all 1s to each index
static void costMove1s(int[] arr, int N)
{
// Traverse the array.
for(int i = 0; i < N; i++)
{
// Stores cost to move
// all 1s at current index
int cost = 0;
// Calculate cost to move
// all 1s at current index
for(int j = 0; j < N; j++)
{
// If current element
// of the array is 1.
if (arr[j] == 1)
{
// Update cost
cost += Math.abs(i - j);
}
}
System.out.print(cost + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 0, 1, 0, 1 };
int N = arr.length;
costMove1s(arr, N);
}
}
// This code is contributed by akhilsaini
Python3
# Pyhton3 program to implement
# the above approach
# Function to print the cost
# to move all 1s to each index
def costMove1s(arr, N):
# Traverse the array.
for i in range(0, N):
# Stores cost to move
# all 1s at current index
cost = 0
# Calculate cost to move
# all 1s at current index
for j in range(0, N):
# If current element
# of the array is 1.
if (arr[j] == 1):
# Update cost
cost = cost + abs(i - j)
print(cost, end = " ")
# Driver Code
if __name__ == "__main__":
arr = [ 0, 1, 0, 1 ]
N = len(arr)
costMove1s(arr, N)
# This code is contributed by akhilsaini
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print the cost
// to move all 1s to each index
static void costMove1s(int[] arr, int N)
{
// Traverse the array.
for(int i = 0; i < N; i++)
{
// Stores cost to move
// all 1s at current index
int cost = 0;
// Calculate cost to move
// all 1s at current index
for(int j = 0; j < N; j++)
{
// If current element
// of the array is 1.
if (arr[j] == 1)
{
// Update cost
cost += Math.Abs(i - j);
}
}
Console.Write(cost + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 0, 1, 0, 1 };
int N = arr.Length;
costMove1s(arr, N);
}
}
// This code is contributed by akhilsaini
Javascript
C++14
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print the cost
// to move all 1s to each index
void costMove1s(int arr[], int N)
{
// cost[i] Stores cost to move
// all 1s at index i
int cost[N] = { 0 };
// Stores count of 1s on
// the left side of index i
int cntLeft = 0;
// Stores cost to move all 1s
// from the left side of index i
// to index i
int costLeft = 0;
// Traverse the array from
// left to right.
for (int i = 0; i < N; i++) {
// Update cost to move
// all 1s from left side
// of index i to index i
costLeft += cntLeft;
// Update cost[i] to cntLeft
cost[i] += costLeft;
// If current element is 1.
if (arr[i] == 1) {
cntLeft++;
}
}
// Stores count of 1s on
// the right side of index i
int cntRight = 0;
// Stores cost to move all 1s
// from the right of index i
// to index i
int costRight = 0;
// Traverse the array from
// right to left.
for (int i = N - 1; i >= 0;
i--) {
// Update cost to move
// all 1s from right side
// of index i to index i
costRight += cntRight;
// Update cost[i]
// to costRight
cost[i] += costRight;
// If current element
// is 1.
if (arr[i] == 1) {
cntRight++;
}
}
// Print cost to move all 1s
for(int i = 0; i< N; i++) {
cout<
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to print the cost
// to move all 1s to each index
static void costMove1s(int arr[], int N)
{
// cost[i] Stores cost to move
// all 1s at index i
int cost[] = new int[N];
// Stores count of 1s on
// the left side of index i
int cntLeft = 0;
// Stores cost to move all 1s
// from the left side of index i
// to index i
int costLeft = 0;
// Traverse the array from
// left to right.
for(int i = 0; i < N; i++)
{
// Update cost to move
// all 1s from left side
// of index i to index i
costLeft += cntLeft;
// Update cost[i] to cntLeft
cost[i] += costLeft;
// If current element is 1.
if (arr[i] == 1)
{
cntLeft++;
}
}
// Stores count of 1s on
// the right side of index i
int cntRight = 0;
// Stores cost to move all 1s
// from the right of index i
// to index i
int costRight = 0;
// Traverse the array from
// right to left.
for(int i = N - 1; i >= 0; i--)
{
// Update cost to move
// all 1s from right side
// of index i to index i
costRight += cntRight;
// Update cost[i]
// to costRight
cost[i] += costRight;
// If current element
// is 1.
if (arr[i] == 1)
{
cntRight++;
}
}
// Print cost to move all 1s
for(int i = 0; i < N; i++)
{
System.out.print(cost[i] + " ");
}
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 0, 1, 0, 1 };
int N = arr.length;
// Function Call
costMove1s(arr, N);
}
}
// This code is contributed by math_lover
Python3
# Python3 program to implement
# the above approach
# Function to prthe cost
# to move all 1s to each index
def costMove1s(arr, N):
# cost[i] Stores cost to move
# all 1s at index i
cost = [0] * N
# Stores count of 1s on
# the left side of index i
cntLeft = 0
# Stores cost to move all 1s
# from the left side of index i
# to index i
costLeft = 0
# Traverse the array from
# left to right.
for i in range(N):
# Update cost to move
# all 1s from left side
# of index i to index i
costLeft += cntLeft
# Update cost[i] to cntLeft
cost[i] += costLeft
# If current element is 1.
if (arr[i] == 1):
cntLeft += 1
# Stores count of 1s on
# the right side of index i
cntRight = 0
# Stores cost to move all 1s
# from the right of index i
# to index i
costRight = 0
# Traverse the array from
# right to left.
for i in range(N - 1, -1, -1):
# Update cost to move
# all 1s from right side
# of index i to index i
costRight += cntRight
# Update cost[i]
# to costRight
cost[i] += costRight
# If current element
# is 1.
if (arr[i] == 1):
cntRight += 1
# Print cost to move all 1s
for i in range(N):
print(cost[i], end = " ")
# Driver Code
if __name__ == '__main__':
arr = [ 0, 1, 0, 1 ]
N = len(arr)
costMove1s(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print the cost
// to move all 1s to each index
static void costMove1s(int []arr, int N)
{
// cost[i] Stores cost to move
// all 1s at index i
int []cost = new int[N];
// Stores count of 1s on
// the left side of index i
int cntLeft = 0;
// Stores cost to move all 1s
// from the left side of index i
// to index i
int costLeft = 0;
// Traverse the array from
// left to right.
for(int i = 0; i < N; i++)
{
// Update cost to move
// all 1s from left side
// of index i to index i
costLeft += cntLeft;
// Update cost[i] to cntLeft
cost[i] += costLeft;
// If current element is 1.
if (arr[i] == 1)
{
cntLeft++;
}
}
// Stores count of 1s on
// the right side of index i
int cntRight = 0;
// Stores cost to move all 1s
// from the right of index i
// to index i
int costRight = 0;
// Traverse the array from
// right to left.
for(int i = N - 1; i >= 0; i--)
{
// Update cost to move
// all 1s from right side
// of index i to index i
costRight += cntRight;
// Update cost[i]
// to costRight
cost[i] += costRight;
// If current element
// is 1.
if (arr[i] == 1)
{
cntRight++;
}
}
// Print cost to move all 1s
for(int i = 0; i < N; i++)
{
Console.Write(cost[i] + " ");
}
}
// Driver Code
public static void Main (String[] args)
{
int []arr = { 0, 1, 0, 1 };
int N = arr.Length;
// Function Call
costMove1s(arr, N);
}
}
// This code is contributed by math_lover
Javascript
4 2 2 2
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是遍历给定数组并使用前缀和技术找到从给定数组的每个索引的左侧和右侧移动所有1的成本。请按照以下步骤解决问题:
- 初始化一个数组,比如cost[]来存储在给定数组的每个索引处移动所有1的成本。
- 初始化两个变量,比如costLeft和costRight来存储分别从每个索引的左侧和右侧移动所有1的成本。
- 从左到右遍历给定数组,将costLeft的值增加左侧1的数量,将result[i]的值增加到 costLeft ..
- 从右到左遍历给定数组,将costRight的值增加右侧1的数量,将result[i]的值增加到 costRight 。
- 最后,打印cost[]数组。
下面是上述方法的实现:
C++14
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print the cost
// to move all 1s to each index
void costMove1s(int arr[], int N)
{
// cost[i] Stores cost to move
// all 1s at index i
int cost[N] = { 0 };
// Stores count of 1s on
// the left side of index i
int cntLeft = 0;
// Stores cost to move all 1s
// from the left side of index i
// to index i
int costLeft = 0;
// Traverse the array from
// left to right.
for (int i = 0; i < N; i++) {
// Update cost to move
// all 1s from left side
// of index i to index i
costLeft += cntLeft;
// Update cost[i] to cntLeft
cost[i] += costLeft;
// If current element is 1.
if (arr[i] == 1) {
cntLeft++;
}
}
// Stores count of 1s on
// the right side of index i
int cntRight = 0;
// Stores cost to move all 1s
// from the right of index i
// to index i
int costRight = 0;
// Traverse the array from
// right to left.
for (int i = N - 1; i >= 0;
i--) {
// Update cost to move
// all 1s from right side
// of index i to index i
costRight += cntRight;
// Update cost[i]
// to costRight
cost[i] += costRight;
// If current element
// is 1.
if (arr[i] == 1) {
cntRight++;
}
}
// Print cost to move all 1s
for(int i = 0; i< N; i++) {
cout<
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to print the cost
// to move all 1s to each index
static void costMove1s(int arr[], int N)
{
// cost[i] Stores cost to move
// all 1s at index i
int cost[] = new int[N];
// Stores count of 1s on
// the left side of index i
int cntLeft = 0;
// Stores cost to move all 1s
// from the left side of index i
// to index i
int costLeft = 0;
// Traverse the array from
// left to right.
for(int i = 0; i < N; i++)
{
// Update cost to move
// all 1s from left side
// of index i to index i
costLeft += cntLeft;
// Update cost[i] to cntLeft
cost[i] += costLeft;
// If current element is 1.
if (arr[i] == 1)
{
cntLeft++;
}
}
// Stores count of 1s on
// the right side of index i
int cntRight = 0;
// Stores cost to move all 1s
// from the right of index i
// to index i
int costRight = 0;
// Traverse the array from
// right to left.
for(int i = N - 1; i >= 0; i--)
{
// Update cost to move
// all 1s from right side
// of index i to index i
costRight += cntRight;
// Update cost[i]
// to costRight
cost[i] += costRight;
// If current element
// is 1.
if (arr[i] == 1)
{
cntRight++;
}
}
// Print cost to move all 1s
for(int i = 0; i < N; i++)
{
System.out.print(cost[i] + " ");
}
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 0, 1, 0, 1 };
int N = arr.length;
// Function Call
costMove1s(arr, N);
}
}
// This code is contributed by math_lover
蟒蛇3
# Python3 program to implement
# the above approach
# Function to prthe cost
# to move all 1s to each index
def costMove1s(arr, N):
# cost[i] Stores cost to move
# all 1s at index i
cost = [0] * N
# Stores count of 1s on
# the left side of index i
cntLeft = 0
# Stores cost to move all 1s
# from the left side of index i
# to index i
costLeft = 0
# Traverse the array from
# left to right.
for i in range(N):
# Update cost to move
# all 1s from left side
# of index i to index i
costLeft += cntLeft
# Update cost[i] to cntLeft
cost[i] += costLeft
# If current element is 1.
if (arr[i] == 1):
cntLeft += 1
# Stores count of 1s on
# the right side of index i
cntRight = 0
# Stores cost to move all 1s
# from the right of index i
# to index i
costRight = 0
# Traverse the array from
# right to left.
for i in range(N - 1, -1, -1):
# Update cost to move
# all 1s from right side
# of index i to index i
costRight += cntRight
# Update cost[i]
# to costRight
cost[i] += costRight
# If current element
# is 1.
if (arr[i] == 1):
cntRight += 1
# Print cost to move all 1s
for i in range(N):
print(cost[i], end = " ")
# Driver Code
if __name__ == '__main__':
arr = [ 0, 1, 0, 1 ]
N = len(arr)
costMove1s(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print the cost
// to move all 1s to each index
static void costMove1s(int []arr, int N)
{
// cost[i] Stores cost to move
// all 1s at index i
int []cost = new int[N];
// Stores count of 1s on
// the left side of index i
int cntLeft = 0;
// Stores cost to move all 1s
// from the left side of index i
// to index i
int costLeft = 0;
// Traverse the array from
// left to right.
for(int i = 0; i < N; i++)
{
// Update cost to move
// all 1s from left side
// of index i to index i
costLeft += cntLeft;
// Update cost[i] to cntLeft
cost[i] += costLeft;
// If current element is 1.
if (arr[i] == 1)
{
cntLeft++;
}
}
// Stores count of 1s on
// the right side of index i
int cntRight = 0;
// Stores cost to move all 1s
// from the right of index i
// to index i
int costRight = 0;
// Traverse the array from
// right to left.
for(int i = N - 1; i >= 0; i--)
{
// Update cost to move
// all 1s from right side
// of index i to index i
costRight += cntRight;
// Update cost[i]
// to costRight
cost[i] += costRight;
// If current element
// is 1.
if (arr[i] == 1)
{
cntRight++;
}
}
// Print cost to move all 1s
for(int i = 0; i < N; i++)
{
Console.Write(cost[i] + " ");
}
}
// Driver Code
public static void Main (String[] args)
{
int []arr = { 0, 1, 0, 1 };
int N = arr.Length;
// Function Call
costMove1s(arr, N);
}
}
// This code is contributed by math_lover
Javascript
4 2 2 2
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live