📌  相关文章
📜  最长子数组的长度仅由 1 组成

📅  最后修改于: 2021-10-27 06:25:04             🧑  作者: Mango

给定一个大小为N的数组 arr[] ,由二进制值组成,任务是在删除单个数组元素后找到仅由 1 s 组成的最长非空子数组。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化三个变量, newLen = 0prevLen = 0maxLen = 0
  • 通过在开头附加零来遍历数组arr[]:
    • 如果 arr[i] = 1:newLenprevLen 都增加1
    • 除此以外:
      • 将最大值分配给变量maxLen
      • 设置prevLen = newLennewLen = 0
  • 如果maxLen < len(arr) ,则打印maxLen
  • 否则,打印maxLen – 1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Utility function to find the length of
// longest subarray containing only 1s
int longestSubarrayUtil(vector arr, int n)
{
    int neww = 0, old = 0, m = 0;
     
    // Traverse the array
    for(int x = 0; x <= n; x++)
    {
         
        // If array element is 1
        if (arr[x] == 1)
        {
             
            // Increment both by 1
            neww += 1;
            old += 1;
        }
        else
        {
             
            // Assign maximum value
            m = max(m, old);
 
            //Assign new to old
            // and set new to zero
            old = neww;
            neww = 0;
        }
    }
    
    // Return the final length
    if (m < n)
    {
        return m;
    }
    else return m - 1;
}
 
// Function to find length of the
// longest subarray containing only 1's
void longestSubarray(vector arr, int n)
{
     
    // Stores the length of longest
    // subarray consisting of 1s
    int len = longestSubarrayUtil(arr, n);
 
    // Print the length
    // of the subarray
    cout << len;
}
 
// Driver code
int main()
{
     
    // Given array
    vector arr = {1, 1, 1};
    int n = arr.size();
     
    // Append 0 at beginning
    for(int i = n; i >= 0; i--)
    {
        arr[i] = arr[i - 1];
    }
    arr[0] = 0;
     
    // Function call to find the longest
    // subarray containing only 1's
    longestSubarray(arr, n);
}
 
// This code is contributed by SoumikMondal


Python3
# Python3 program for the above approach
 
# Utility function to find the length of
# longest subarray containing only 1s
def longestSubarrayUtil(arr):
 
    new, old, m = 0, 0, 0
 
    # Traverse the array
    for x in arr+[0]:
 
        # If array element is 1
        if x == 1:
 
            # Increment both by 1
            new += 1
            old += 1
        else:
 
            # Assign maximum value
            m = max(m, old)
 
            # Assign new to old
            # and set new to zero
            old, new = new, 0
 
    # Return the final length
    return m if m < len(arr) else m - 1
 
# Function to find length of the
# longest subarray containing only 1's
def longestSubarray(arr):
 
    # Stores the length of longest
    # subarray consisting of 1s
    len = longestSubarrayUtil(arr)
 
    # Print the length
    # of the subarray
    print(len)
 
 
# Given array
arr = [1, 1, 1]
 
# Function call to find the longest
# subarray containing only 1's
longestSubarray(arr)


Javascript


输出:
2

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

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