通过将 K 大小子数组的最大值更改为 -1,最小化操作以使所有数组元素为 -1
给定一个由N个整数和一个整数K组成的数组arr[] ,任务是找到使所有数组元素-1所需的最小操作,使得在每个操作中,选择一个大小为K的子数组并更改所有最大值子数组中的元素到-1 。
例子:
Input: arr[] = {18, 11, 18, 11, 18}, K = 3
Output: 3
Explanation:
Following are the operations performed:
- Choosing the sub array from index 0 to 2 and by applying the operation, modifies the array to {-1, 11, -1, 11, 18}.
- Choosing the sub array form index 1 to 3 and by applying the operation, modifies the array to {-1, -1, -1, -1, 18}.
- Choosing the sub array form index 2 to 4 and by applying the operation, modifies the array to {-1, -1, -1, -1, -1}.
After the above operations all the array elements become -1. Therefore, the minimum number of operations required is 3.
Input: arr[] = {2, 1, 1}, K = 2
Output: 2
方法:给定的问题可以通过对数组arr[]进行排序来解决 带索引 然后通过从索引之间的差异小于 K的末尾选择数组元素来计算操作数。请按照以下步骤解决问题:
- 初始化一个对向量,比如vp[] ,它存储数组元素及其索引。
- 初始化一个变量,比如minCnt为0 ,它存储最小操作数的计数。
- 遍历数组arr[]并将arr[i]及其索引i推送到向量vp中。
- 相对于第一个值对向量对vp[]进行排序。
- 遍历向量vp[]直到它不为空并执行以下步骤:
- 将minCnt的值增加1 。
- 从后面弹出向量vp[]中具有相同值(每对的第一个元素)并且索引之间的差异小于K的所有元素。
- 完成上述步骤后,打印minCnt的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum the number
// of operations required to make all
// the array elements to -1
int minOperations(int arr[], int N, int K)
{
// Stores the array elements with
// their corresponding indices
vector > vp;
for (int i = 0; i < N; i++) {
// Push the array element
// and it's index
vp.push_back({ arr[i], i });
}
// Sort the elements according
// to it's first value
sort(vp.begin(), vp.end());
// Stores the minimum number of
// operations required
int minCnt = 0;
// Traverse until vp is not empty
while (!vp.empty()) {
int val, ind;
// Stores the first value of vp
val = vp.back().first;
// Stores the second value of vp
ind = vp.back().second;
// Update the minCnt
minCnt++;
// Pop the back element from the
// vp until the first value is
// same as val and difference
// between indices is less than K
while (!vp.empty()
&& vp.back().first == val
&& ind - vp.back().second + 1 <= K)
vp.pop_back();
}
// Return the minCnt
return minCnt;
}
// Driver Code
int main()
{
int arr[] = { 18, 11, 18, 11, 18 };
int K = 3;
int N = sizeof(arr) / sizeof(arr[0]);
cout << minOperations(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find minimum the number
// of operations required to make all
// the array elements to -1
static int minOperations(int arr[], int N, int K)
{
// Stores the array elements with
// their corresponding indices
Vector vp = new Vector();
for (int i = 0; i < N; i++) {
// Push the array element
// and it's index
vp.add(new pair( arr[i], i ));
}
// Sort the elements according
// to it's first value
Collections.sort(vp,(a,b)->a.first-b.first);
// Stores the minimum number of
// operations required
int minCnt = 0;
// Traverse until vp is not empty
while (!vp.isEmpty()) {
int val, ind;
// Stores the first value of vp
val = vp.get(vp.size()-1).first;
// Stores the second value of vp
ind = vp.get(vp.size()-1).second;
// Update the minCnt
minCnt++;
// Pop the back element from the
// vp until the first value is
// same as val and difference
// between indices is less than K
while (!vp.isEmpty()
&& vp.get(vp.size()-1).first == val
&& ind - vp.get(vp.size()-1).second + 1 <= K)
vp.remove(vp.size()-1);
}
// Return the minCnt
return minCnt;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 18, 11, 18, 11, 18 };
int K = 3;
int N = arr.length;
System.out.print(minOperations(arr, N, K));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
# Function to find minimum the number
# of operations required to make all
# the array elements to -1
def minOperations(arr, N, K):
# Stores the array elements with
# their corresponding indices
vp = []
for i in range(N):
# Push the array element
# and it's index
vp.append([arr[i], i])
# Sort the elements according
# to it's first value
vp.sort()
# Stores the minimum number of
# operations required
minCnt = 0
# Traverse until vp is not empty
while (len(vp) != 0):
# Stores the first value of vp
val = vp[-1][0]
# Stores the second value of vp
ind = vp[-1][1]
# Update the minCnt
minCnt += 1
# Pop the back element from the
# vp until the first value is
# same as val and difference
# between indices is less than K
while (len(vp) != 0
and vp[-1][0] == val
and ind - vp[-1][1] + 1 <= K):
vp.pop()
# Return the minCnt
return minCnt
# Driver Code
if __name__ == "__main__":
arr = [18, 11, 18, 11, 18]
K = 3
N = len(arr)
print(minOperations(arr, N, K))
# This code is contributed by mukesh07.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
class pair : IComparable
{
public int first,second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int CompareTo(pair p)
{
return this.first - p.first;
}
}
// Function to find minimum the number
// of operations required to make all
// the array elements to -1
static int minOperations(int []arr, int N, int K)
{
// Stores the array elements with
// their corresponding indices
List vp = new List();
for (int i = 0; i < N; i++) {
// Push the array element
// and it's index
vp.Add(new pair( arr[i], i ));
}
// Sort the elements according
// to it's first value
vp.Sort();
// Stores the minimum number of
// operations required
int minCnt = 0;
// Traverse until vp is not empty
while (vp.Count!=0) {
int val, ind;
// Stores the first value of vp
val = vp[vp.Count-1].first;
// Stores the second value of vp
ind = vp[vp.Count-1].second;
// Update the minCnt
minCnt++;
// Pop the back element from the
// vp until the first value is
// same as val and difference
// between indices is less than K
while (vp.Count!=0
&& vp[vp.Count-1].first == val
&& ind - vp[vp.Count-1].second + 1 <= K)
vp.RemoveAt(vp.Count-1);
}
// Return the minCnt
return minCnt;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 18, 11, 18, 11, 18 };
int K = 3;
int N = arr.Length;
Console.Write(minOperations(arr, N, K));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(N)