给定一个数组,大小为N 的arr[]和一个整数T 。 任务是为每个索引找到最小数量的索引,如果第 i 个索引之前的总和不应超过T ,则应跳过该索引。
例子:
Input: N = 7, T = 15, arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 0 0 0 0 0 2 3
Explanation: No indices need to be skipped for the first 5 indices: {1, 2, 3, 4, 5}, since their sum is 15 and <= T.
For the sixth index, indices 3 and 4 can be skipped, that makes its sum = (1+2+3+6) = 12.
For the seventh, indices 3, 4 and 5 can be skipped which makes its sum = (1+2+3+7) = 13.
Input: N = 2, T = 100, arr[] = {100, 100}
Output: 0 1
方法:想法是使用映射在遍历时按递增顺序存储访问过的元素。请按照以下步骤解决问题:
- 创建一个有序映射M以保持第 i 个索引之前元素的计数。
- 将变量sum初始化为0以存储前缀和。
- 使用变量i遍历数组arr[]
- 将sum+arr[i]和T的差值存储在变量d 中。
- 如果d>0的值,则从最后遍历地图并选择具有最大元素的索引,直到总和变得小于T 。将所需元素的数量存储在变量k 中。
- 将arr[i]添加到 sum 并将M 中的A[i]增加1 。
- 打印k的值。
下面是上述方法的实现:
C++
// C++ approach for above approach
#include
using namespace std;
// Function to calculate minimum indices to be skipped
// so that sum till i remains smaller than T
void skipIndices(int N, int T, int arr[])
{
// Store the sum of all indices before i
int sum = 0;
// Store the elements that can be skipped
map count;
// Traverse the array, A[]
for (int i = 0; i < N; i++) {
// Store the total sum of elements that
// needs to be skipped
int d = sum + arr[i] - T;
// Store the number of elements need
// to be removed
int k = 0;
if (d > 0) {
// Traverse from the back of map so
// as to take bigger elements first
for (auto u = count.rbegin(); u != count.rend();
u++) {
int j = u->first;
int x = j * count[j];
if (d <= x) {
k += (d + j - 1) / j;
break;
}
k += count[j];
d -= x;
}
}
// Update sum
sum += arr[i];
// Update map with the current element
count[arr[i]]++;
cout << k << " ";
}
}
// Driver code
int main()
{
// Given Input
int N = 7;
int T = 15;
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
// Function Call
skipIndices(N, T, arr);
return 0;
}
Java
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
// C++ approach for above approach
class GFG {
// Function to calculate minimum indices to be skipped
// so that sum till i remains smaller than T
public static void skipIndices(int N, int T, int arr[])
{
// Store the sum of all indices before i
int sum = 0;
// Store the elements that can be skipped
TreeMap count = new TreeMap();
// Traverse the array, A[]
for (int i = 0; i < N; i++) {
// Store the total sum of elements that
// needs to be skipped
int d = sum + arr[i] - T;
// Store the number of elements need
// to be removed
int k = 0;
if (d > 0) {
// Traverse from the back of map so
// as to take bigger elements first
for (Map.Entry u : count.descendingMap().entrySet()) {
int j = u.getKey();
int x = j * count.get(j);
if (d <= x) {
k += (d + j - 1) / j;
break;
}
k += count.get(j);
d -= x;
}
}
// Update sum
sum += arr[i];
// Update map with the current element
if(count.containsKey(arr[i])){
count.put(arr[i], count.get(arr[i]) + 1);
}else{
count.put(arr[i], 1);
}
System.out.print(k + " ");
}
}
// Driver code
public static void main(String args[])
{
// Given Input
int N = 7;
int T = 15;
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
// Function Call
skipIndices(N, T, arr);
}
}
// This code is contributed by _saurabh_jaiswal.
Python3
# Python3 approach for above approach
# Function to calculate minimum indices to be skipped
# so that sum till i remains smaller than T
def skipIndices(N, T, arr):
# Store the sum of all indices before i
sum = 0
# Store the elements that can be skipped
count = {}
# Traverse the array, A[]
for i in range(N):
# Store the total sum of elements that
# needs to be skipped
d = sum + arr[i] - T
# Store the number of elements need
# to be removed
k = 0
if (d > 0):
# Traverse from the back of map so
# as to take bigger elements first
for u in list(count.keys())[::-1]:
j = u
x = j * count[j]
if (d <= x):
k += (d + j - 1) // j
break
k += count[j]
d -= x
# Update sum
sum += arr[i]
# Update map with the current element
count[arr[i]] = count.get(arr[i], 0) + 1
print(k, end = " ")
# Driver code
if __name__ == '__main__':
# Given Input
N = 7
T = 15
arr = [1, 2, 3, 4, 5, 6, 7]
# Function Call
skipIndices(N, T, arr)
# This code is contributed by mohit kumar 29.
Javascript
输出
0 0 0 0 0 2 3
时间复杂度: O(N 2 )
辅助空间:O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。