给定一个二进制数组arr[] ,任务是找到从 0 的相邻左侧删除所有 1 所需的操作数。在每个操作中,紧邻 0 左侧的所有 1 都更改为 0。
例子:
Input: arr[] = { 1, 0, 0, 1, 1, 0 }
Output: 2
Explanation:
Operation 1: Change in index 0 and 4. arr[] = { 0, 0, 0, 1, 0, 0 }
Operation 2: Change in index 3. arr[] = { 0, 0, 0, 0, 0, 0 }
No more operations required.
Input: arr[] = { 0, 1, 0, 1 }
Output: 1
Explanation:
Operation 1: Change in index 1. arr[] = { 0, 0, 0, 1 }
No more operations required.
方法:这个问题可以使用贪心方法来解决。这个想法是计算0之前连续 1 的最大数量,它给出了给定操作需要执行的次数,这样数组就不能被进一步更改。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the maximum number
// of 1's before 0
void noOfMoves(int arr[], int n)
{
int cnt = 0;
int maxCnt = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// If value is 1
if (arr[i] == 1) {
cnt++;
}
else {
// If consecutive 1 followed
// by 0, then update the maxCnt
if (cnt != 0) {
maxCnt = max(maxCnt, cnt);
cnt = 0;
}
}
}
// Print the maximum consecutive 1's
// followed by 0
cout << maxCnt << endl;
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
noOfMoves(arr, N);
int arr1[] = { 1, 0, 1, 0, 1, 0, 1, 0 };
N = sizeof(arr) / sizeof(arr[0]);
// Function Call
noOfMoves(arr1, N);
return 0;
}
Java
// Java implementation of the above approach
class GFG{
// Function to find the maximum number
// of 1's before 0
static void noOfMoves(int arr[], int n)
{
int cnt = 0;
int maxCnt = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// If value is 1
if (arr[i] == 1) {
cnt++;
}
else {
// If consecutive 1 followed
// by 0, then update the maxCnt
if (cnt != 0) {
maxCnt = Math.max(maxCnt, cnt);
cnt = 0;
}
}
}
// Print the maximum consecutive 1's
// followed by 0
System.out.print(maxCnt +"\n");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 0, 1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 1 };
int N = arr.length;
// Function Call
noOfMoves(arr, N);
int arr1[] = { 1, 0, 1, 0, 1, 0, 1, 0 };
N = arr1.length;
// Function Call
noOfMoves(arr1, N);
}
}
// This code is contributed by 29AjayKumar
Python 3
# Python 3 implementation of the above approach
# Function to find the maximum number
# of 1's before 0
def noOfMoves(arr,n):
cnt = 0
maxCnt = 0
# Traverse the array
for i in range(n):
# If value is 1
if (arr[i] == 1):
cnt += 1
else:
# If consecutive 1 followed
# by 0, then update the maxCnt
if (cnt != 0):
maxCnt = max(maxCnt, cnt)
cnt = 0
# Print the maximum consecutive 1's
# followed by 0
print(maxCnt)
# Driver Code
if __name__ == '__main__':
arr = [0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1]
N = len(arr)
# Function Call
noOfMoves(arr, N)
arr1 = [1, 0, 1, 0, 1, 0, 1, 0]
N = len(arr1)
# Function Call
noOfMoves(arr1, N)
# This code is contributed by Surendra_Gangwar
C#
// C# implementation of the above approach
using System;
public class GFG{
// Function to find the maximum number
// of 1's before 0
static void noOfMoves(int []arr, int n)
{
int cnt = 0;
int maxCnt = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// If value is 1
if (arr[i] == 1) {
cnt++;
}
else {
// If consecutive 1 followed
// by 0, then update the maxCnt
if (cnt != 0) {
maxCnt = Math.Max(maxCnt, cnt);
cnt = 0;
}
}
}
// Print the maximum consecutive 1's
// followed by 0
Console.Write(maxCnt +"\n");
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 0, 1, 1, 1, 1, 0,
0, 1, 1, 0, 0, 1 };
int N = arr.Length;
// Function Call
noOfMoves(arr, N);
int []arr1 = { 1, 0, 1, 0, 1, 0, 1, 0 };
N = arr1.Length;
// Function Call
noOfMoves(arr1, N);
}
}
// This code contributed by Rajput-Ji
Javascript
输出:
4
1
时间复杂度: O(N) ,其中 N 是数组的长度。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live