给定大小为N的数组arr [] 。在每个操作中,选择一个数组元素X并删除[X – 1,X + 1]范围内的所有数组元素。任务是找到所需的最大步骤数,以使阵列中没有硬币。
例子:
Input: coins [] = {5, 1, 3, 2, 6, 7, 4}
Output: 4
Explanation:
Picking the coin coins[1] modifies the array arr[] = {5, 3, 6, 7, 4}.
Picking the coin coins[1] modifies the array arr[] = {5, 6, 7}
Picking the coin coins[0] modifies the array arr[] = {7}
Picking the coin coins[0] modifies the array arr[] = {}. Therefore, the required output is 4.
Input: coins [] = {6, 7, 5, 1}
Output: 3
天真的方法:解决此问题的最简单方法是生成给定数组的所有可能排列,并针对数组的每个排列,通过仅选择数组的第一个元素来找到删除数组的所有元素所需的步骤数。在所有可能的步骤中进行排列。最后,打印删除所有元素所需的最大步骤数。
时间复杂度: O(N!)
辅助空间: O(1)
高效的方法:为了优化上述方法,其思想是从阵列中选择一个元素,以便在每个步骤中,最多移除两个数组中的元素。请按照以下步骤解决问题:
- 初始化一个变量,例如cntSteps,以存储从arr []数组中删除所有硬币所需的最大步数。
- 创建一个映射,例如说Map ,以按升序存储arr []数组元素的频率。
- 初始化一个变量,比如说Min,以存储Map的最小元素。
- 遍历Map并在每次遍历中从Map移除Min和(Min + 1) ,并将cntSteps的值增加1 。
- 最后,打印cntSteps的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find maximum steps to
// remove all coins from the arr[]
int maximumSteps(int arr[], int N)
{
// Store the frequency of array
// elements in ascending order
map Map;
// Traverse the arr[] array
for (int i = 0; i < N; i++) {
Map[arr[i]]++;
}
// Stores count of steps required
// to remove all the array elements
int cntSteps = 0;
// Traverse the map
for (auto i : Map) {
// Stores the smallest element
// of Map
int X = i.first;
// If frequency if X
// greater than 0
if (i.second > 0) {
// Update cntSteps
cntSteps++;
// Mark X as
// removed element
Map[X] = 0;
// If frequency of (X + 1)
// greater than 0
if (Map[X + 1])
// Mark (X + 1) as
// removed element
Map[X + 1] = 0;
}
}
return cntSteps;
}
// Driver Code
int main()
{
int arr[] = { 5, 1, 3, 2, 6, 7, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << maximumSteps(arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find maximum steps to
// remove all coins from the arr[]
static int maximumSteps(int arr[], int N)
{
// Store the frequency of array
// elements in ascending order
Map mp = new HashMap();
// Traverse the arr[] array
for(int i = 0; i < N; i++)
{
mp.put(arr[i],
mp.getOrDefault(arr[i], 0) + 1);
}
// Stores count of steps required
// to remove all the array elements
int cntSteps = 0;
// Traverse the mp
for(Map.Entry it : mp.entrySet())
{
// Stores the smallest element
// of mp
int X = it.getKey();
// If frequency if X
// greater than 0
if (it.getValue() > 0)
{
// Update cntSteps
cntSteps++;
// Mark X as
// removed element
mp.replace(X, 0);
// If frequency of (X + 1)
// greater than 0
if (mp.getOrDefault(X + 1, 0) != 0)
// Mark (X + 1) as
// removed element
mp.replace(X + 1, 0);
}
}
return cntSteps;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 5, 1, 3, 2, 6, 7, 4 };
int N = arr.length;
System.out.print(maximumSteps(arr, N));
}
}
// This code is contributed by ipg2016107
Python3
# Python3 program to implement
# the above approach
# Function to find maximum steps to
# remove all coins from the arr[]
def maximumSteps(arr, N):
# Store the frequency of array
# elements in ascending order
Map = {}
# Traverse the arr[] array
for i in range(N):
Map[arr[i]] = Map.get(arr[i], 0) + 1
# Stores count of steps required
# to remove all the array elements
cntSteps = 0
# Traverse the map
for i in Map:
# Stores the smallest element
# of Map
X = i
# If frequency if X
# greater than 0
if (Map[i] > 0):
# Update cntSteps
cntSteps += 1
# Mark X as
# removed element
Map[X] = 0
# If frequency of (X + 1)
# greater than 0
if (X + 1 in Map):
# Mark (X + 1) as
# removed element
Map[X + 1] = 0
return cntSteps
# Driver Code
if __name__ == '__main__':
arr = [ 5, 1, 3, 2, 6, 7, 4 ]
N = len(arr)
print(maximumSteps(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find maximum steps to
// remove all coins from the []arr
static int maximumSteps(int []arr, int N)
{
// Store the frequency of array
// elements in ascending order
Dictionary mp = new Dictionary();
// Traverse the []arr array
for(int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]]++;
}
else
{
mp.Add(arr[i], 1);
}
}
// Stores count of steps required
// to remove all the array elements
int cntSteps = 0;
// Traverse the mp
foreach(KeyValuePair it in mp)
{
// Stores the smallest element
// of mp
int X = it.Key;
// If frequency if X
// greater than 0
if (it.Value > 0)
{
// Update cntSteps
cntSteps++;
}
}
return (cntSteps + 1) / 2;
}
// Driver Code
public static void Main(String []args)
{
int []arr = { 5, 1, 3, 2, 6, 7, 4 };
int N = arr.Length;
Console.Write(maximumSteps(arr, N));
}
}
// This code is contributed by Princi Singh
4
时间复杂度: O(N * Log(N))
空间复杂度: O(N)