给定一个大小为N的二进制字符串S ,任务是找到给定字符串S 中最长非递增子序列的长度。
例子:
Input: S = “0101110110100001011”
Output: 12
Explanation: The longest non-increasing subsequence is “111111100000”, having length equal to 12.
Input: S = 10101
Output: 3
方法:根据观察字符串S是二进制字符串可以解决给定的问题,因此非递增子序列将始终由0和更多连续1或1和更多连续0 组成。请按照以下步骤解决问题:
- 初始化一个数组,说预[],其存储1秒直到每个索引的编号i为i是在范围[0,N – 1]。
- 初始化一个数组,比如post[] ,它存储0的数量,直到每个索引i到字符串末尾的i在范围[0, N – 1] 。
- 初始化一个变量,比如ans ,它存储给定字符串S 中最长非递增子序列的长度。
- 迭代范围[0, N – 1]并将ans的值更新为ans和(pre[i] + post[i])的最大值。
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the length of the
// longest non-increasing subsequence
int findLength(string str, int n)
{
// Stores the prefix and suffix
// count of 1s and 0s respectively
int pre[n], post[n];
// Initialize the array
memset(pre, 0, sizeof(pre));
memset(post, 0, sizeof(post));
// Store the number of '1's
// up to current index i in pre
for (int i = 0; i < n; i++) {
// Find the prefix sum
if (i != 0) {
pre[i] += pre[i - 1];
}
// If the current element
// is '1', update the pre[i]
if (str[i] == '1') {
pre[i] += 1;
}
}
// Store the number of '0's over
// the range [i, N - 1]
for (int i = n - 1; i >= 0; i--) {
// Find the suffix sum
if (i != n - 1)
post[i] += post[i + 1];
// If the current element
// is '0', update post[i]
if (str[i] == '0')
post[i] += 1;
}
// Stores the maximum length
int ans = 0;
// Find the maximum value of
// pre[i] + post[i]
for (int i = 0; i < n; i++) {
ans = max(ans, pre[i] + post[i]);
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
string S = "0101110110100001011";
cout << findLength(S, S.length());
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the length of the
// longest non-increasing subsequence
static int findLength(String str, int n)
{
// Stores the prefix and suffix
// count of 1s and 0s respectively
int pre[] = new int[n];
int post[] = new int[n];
// Initialize the array
for(int i = 0; i < n; i++)
{
pre[i] = 0;
post[i] = 0;
}
// Store the number of '1's
// up to current index i in pre
for(int i = 0; i < n; i++)
{
// Find the prefix sum
if (i != 0)
{
pre[i] += pre[i - 1];
}
// If the current element
// is '1', update the pre[i]
if (str.charAt(i) == '1')
{
pre[i] += 1;
}
}
// Store the number of '0's over
// the range [i, N - 1]
for(int i = n - 1; i >= 0; i--)
{
// Find the suffix sum
if (i != n - 1)
post[i] += post[i + 1];
// If the current element
// is '0', update post[i]
if (str.charAt(i) == '0')
post[i] += 1;
}
// Stores the maximum length
int ans = 0;
// Find the maximum value of
// pre[i] + post[i]
for(int i = 0; i < n; i++)
{
ans = Math.max(ans, pre[i] + post[i]);
}
// Return the answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
String S = "0101110110100001011";
System.out.println(findLength(S, S.length()));
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to find the length of the
# longest non-increasing subsequence
def findLength(str, n):
# Stores the prefix and suffix
# count of 1s and 0s respectively
pre = [0] * n
post = [0] * n
# Store the number of '1's
# up to current index i in pre
for i in range(n):
# Find the prefix sum
if (i != 0):
pre[i] += pre[i - 1]
# If the current element
# is '1', update the pre[i]
if (str[i] == '1'):
pre[i] += 1
# Store the number of '0's over
# the range [i, N - 1]
for i in range(n - 1, -1, -1):
# Find the suffix sum
if (i != (n - 1)):
post[i] += post[i + 1]
# If the current element
# is '0', update post[i]
if (str[i] == '0'):
post[i] += 1
# Stores the maximum length
ans = 0
# Find the maximum value of
# pre[i] + post[i]
for i in range(n):
ans = max(ans, pre[i] + post[i])
# Return the answer
return ans
# Driver Code
S = "0101110110100001011"
n = len(S)
print(findLength(S, n))
# This code is contributed by susmitakundugoaldanga
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the length of the
// longest non-increasing subsequence
static int findLength(String str, int n)
{
// Stores the prefix and suffix
// count of 1s and 0s respectively
int []pre = new int[n];
int []post = new int[n];
// Initialize the array
for(int i = 0; i < n; i++)
{
pre[i] = 0;
post[i] = 0;
}
// Store the number of '1's
// up to current index i in pre
for(int i = 0; i < n; i++)
{
// Find the prefix sum
if (i != 0)
{
pre[i] += pre[i - 1];
}
// If the current element
// is '1', update the pre[i]
if (str[i] == '1')
{
pre[i] += 1;
}
}
// Store the number of '0's over
// the range [i, N - 1]
for(int i = n - 1; i >= 0; i--)
{
// Find the suffix sum
if (i != n - 1)
post[i] += post[i + 1];
// If the current element
// is '0', update post[i]
if (str[i] == '0')
post[i] += 1;
}
// Stores the maximum length
int ans = 0;
// Find the maximum value of
// pre[i] + post[i]
for(int i = 0; i < n; i++)
{
ans = Math.Max(ans, pre[i] + post[i]);
}
// Return the answer
return ans;
}
// Driver Code
public static void Main(String[] args)
{
String S = "0101110110100001011";
Console.WriteLine(findLength(S, S.Length));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
12
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live