最长子序列的长度,使得每个元素的前缀和保持大于零
给定一个大小为N的数组arr[]和一个整数X,任务是找到最长子序列的长度,使得子序列的每个元素的前缀和保持大于零。
例子:
Input: arr[] = {-2, -1, 1, 2, -2}, N = 5
Output: 3
Explanation: The sequence can be made of elements at index 2, 3 and 4. The prefix sum at every element stays greater than zero: 1, 3, 1
Input: arr[] = {-2, 3, 3, -7, -5, 1}, N = 6
Output: 12
方法:给定的问题可以使用贪心方法来解决。这个想法是创建一个最小堆优先级队列并从左到右遍历数组。将当前元素arr[i]添加到sum和 minheap 中,如果sum小于零,则从 minheap 中删除最负的元素并将其从sum中减去。可以按照以下方法解决问题:
- 使用优先队列数据结构初始化最小堆
- 初始化变量sum以计算所需子序列的前缀和
- 迭代数组和每个元素arr[i]并将值添加到sum和 min-heap
- 如果sum的值小于零,则从 min-heap 中删除最负的元素并从sum中减去该值
- 返回最小堆的大小作为最长子序列的长度
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate longest length
// of subsequence such that its prefix sum
// at every element stays greater than zero
int maxScore(int arr[], int N)
{
// Variable to store the answer
int score = 0;
// Min heap implementation
// using a priority queue
priority_queue,
greater >
pq;
// Variable to store the sum
int sum = 0;
for (int i = 0; i < N; i++) {
// Add the current element
// to the sum
sum += arr[i];
// Push the element in
// the min-heap
pq.push(arr[i]);
// If the sum becomes less than
// zero pop the top element of
// the min-heap and subtract it
// from the sum
if (sum < 0) {
int a = pq.top();
sum -= a;
pq.pop();
}
}
// Return the answer
return pq.size();
}
// Driver Code
int main()
{
int arr[] = { -2, 3, 3, -7, -5, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maxScore(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.io.*;
import java.util.PriorityQueue;
class GFG
{
// Function to calculate longest length
// of subsequence such that its prefix sum
// at every element stays greater than zero
static int maxScore(int arr[], int N)
{
// Variable to store the answer
int score = 0;
// Min heap implementation
// using a priority queue
PriorityQueue pq
= new PriorityQueue();
// Variable to store the sum
int sum = 0;
for (int i = 0; i < N; i++) {
// Add the current element
// to the sum
sum += arr[i];
// Push the element in
// the min-heap
pq.add(arr[i]);
// If the sum becomes less than
// zero pop the top element of
// the min-heap and subtract it
// from the sum
if (sum < 0) {
int a = pq.poll();
sum -= a;
}
}
// Return the answer
return pq.size();
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { -2, 3, 3, -7, -5, 1 };
int N = arr.length;
System.out.println(maxScore(arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python implementation for the above approach
from queue import PriorityQueue
# Function to calculate longest length
# of subsequence such that its prefix sum
# at every element stays greater than zero
def maxScore(arr, N):
# Variable to store the answer
score = 0;
# Min heap implementation
# using a priority queue
pq = PriorityQueue();
# Variable to store the sum
sum = 0;
for i in range(N) :
# Add the current element
# to the sum
sum += arr[i];
# Push the element in
# the min-heap
pq.put(arr[i]);
# If the sum becomes less than
# zero pop the top element of
# the min-heap and subtract it
# from the sum
if (sum < 0):
a = pq.queue[0]
sum -= a;
pq.get()
# Return the answer
return pq.qsize();
# Driver Code
arr = [ -2, 3, 3, -7, -5, 1 ];
N = len(arr)
print(maxScore(arr, N))
# This code is contributed by saurabh_jaiswal.
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to calculate longest length
// of subsequence such that its prefix sum
// at every element stays greater than zero
static int maxScore(int []arr, int N) {
// Variable to store the answer
int score = 0;
// Min heap implementation
// using a priority queue
List pq = new List();
// Variable to store the sum
int sum = 0;
for (int i = 0; i < N; i++) {
// Add the current element
// to the sum
sum += arr[i];
// Push the element in
// the min-heap
pq.Add(arr[i]);
pq.Sort();
// If the sum becomes less than
// zero pop the top element of
// the min-heap and subtract it
// from the sum
if (sum < 0) {
int a = pq[0];
pq.RemoveAt(0);
sum -= a;
}
}
// Return the answer
return pq.Count;
}
// Driver Code
public static void Main(String[] args) {
int []arr = { -2, 3, 3, -7, -5, 1 };
int N = arr.Length;
Console.WriteLine(maxScore(arr, N));
}
}
// This code contributed by Rajput-Ji
Javascript
输出
4
时间复杂度: O(N * log N)
辅助空间: O(N)