通过删除第一个或添加先前删除的元素来最大化 Array 的第一个元素
给定一个大小为N的数组arr[]和一个整数K ,任务是在K个操作中最大化数组的第一个元素,其中在每个操作中:
- 如果数组不为空,则删除数组的最顶部元素。
- 在数组的开头添加任何一个先前删除的元素。
例子:
Input: arr[] = [5, 2, 2, 4, 0, 6], K = 4
Output: 5
Explanation: The 4 operations are as:
- Remove the topmost element = 5. The arr becomes [2, 2, 4, 0, 6].
- Remove the topmost element = 2. The arr becomes [2, 4, 0, 6].
- Remove the topmost element = 2. The arr becomes [4, 0, 6].
- Add 5 back onto the arr. The arr becomes [5, 4, 0, 6].
Here 5 is the largest answer possible after 4 moves.
Input: arr[] = [2], K = 1
Output: -1
Explanation: Only one move can be applied and in the first move.
The only option is to remove the first element of the arr[].
If that is done the array becomes empty. So answer is -1
方法:这个问题可以借助基于以下思想的贪心方法来解决:
In first K-1 operations, the K-1 value of the starting can be removed. So currently at Kth node. Now in the last operation there are two possible choice:
- Either remove the current starting node (optimal if the value of the (K+1)th node is greater than the largest amongst first K-1 already removed elements)
- Add the largest from the already removed K-1 elements (optimal when the (K+1)th node has less value than this largest one)
请按照下图所示进行更好的理解。
插图:
For example arr[] = {5, 2, 2, 4, 0, 6}, K = 4
1st Operation:
=> Remove 5. arr[] = {2, 2, 4, 0, 6}
=> maximum = 5, K = 4 – 1 = 3
2nd Operation:
=> Remove 2. arr[] = {2, 4, 0, 6}
=> maximum = max (5, 2) = 5, K = 3 – 1 = 2
3rd Operation:
=> Remove 2. arr[] = {4, 0, 6}
=> maximum = max (5, 2) = 5, K = 2 – 1 = 1
4th Operation:
=> Here the current 2nd element i.e. 0 is less than 5.
=> So add 5 back in the array. arr[] = {5, 4, 0, 6}
=> maximum = max (5, 0) = 5, K = 1 – 1 = 0
Therefore the maximum possible first element is 5.
请按照以下步骤解决问题:
- 如果K = 0 ,则返回第一个节点值。
- 如果K = 1 ,则返回第二个节点值(如果有),否则返回 -1 (因为在K操作之后列表不存在)。
- 如果链表的大小是1,那么在每个奇数操作(即1、3、5、...)中,返回-1,否则返回第一个节点值(因为如果执行奇数操作,则数组将变为空)。
- 如果K > 2 ,则:
- 遍历前K-1个节点,找出最大值。
- 将该最大值与第(K+1) 个节点值进行比较。
- 如果第 (K+1) 个值大于之前的最大值,则使用第 (K+1) 个节点值对其进行更新。否则,不要更新最大值。
- 返回最大值。
以下是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
int maximumTopMost(int arr[], int k,int N){
// Checking if k is odd and
// length of array is 1
if (N == 1 and k % 2 != 0)
return -1;
// Initializing ans with -1
int ans = -1;
// If k is greater or equal to the
// length of array
for(int i = 0; i < min(N, k - 1); i++)
ans = max(ans, arr[i]);
// If k is less than length of array
if (k < N)
ans = max(ans, arr[k]);
// Returning ans
return ans;
}
// Driver code
int main() {
int arr[] = {5, 2, 2, 4, 0, 6};
int N = 6;
int K = 4;
cout <<(maximumTopMost(arr, K, N));
return 0;
}
// This code is contributed by hrithikgarg03188.
Java
// Java code to implement the approach
class GFG {
static int maximumTopMost(int[] arr, int k, int N)
{
// Checking if k is odd and
// length of array is 1
if (N == 1 && k % 2 != 0)
return -1;
// Initializing ans with -1
int ans = -1;
// If k is greater or equal to the
// length of array
for (int i = 0; i < Math.min(N, k - 1); i++)
ans = Math.max(ans, arr[i]);
// If k is less than length of array
if (k < N)
ans = Math.max(ans, arr[k]);
// Returning ans
return ans;
}
public static void main(String[] args)
{
int[] arr = { 5, 2, 2, 4, 0, 6 };
int N = 6;
int K = 4;
System.out.println(maximumTopMost(arr, K, N));
}
}
// This code is contributed by phasing17.
Python3
# Python code to implement the approach
def maximumTopMost(arr, k):
# Checking if k is odd and
# length of array is 1
if len(arr) == 1 and k % 2 != 0:
return -1
# Initializing ans with -1
ans = -1
# If k is greater or equal to the
# length of array
for i in range(min(len(arr), k - 1)):
ans = max(ans, arr[i])
# If k is less than length of array
if k < len(arr):
ans = max(ans, arr[k])
# Returning ans
return ans
# Driver code
if __name__ == "__main__":
arr = [5, 2, 2, 4, 0, 6]
K = 4
print(maximumTopMost(arr, K))
C#
// C# code to implement the approach
using System;
class GFG {
static int maximumTopMost(int[] arr, int k, int N)
{
// Checking if k is odd and
// length of array is 1
if (N == 1 && k % 2 != 0)
return -1;
// Initializing ans with -1
int ans = -1;
// If k is greater or equal to the
// length of array
for (int i = 0; i < Math.Min(N, k - 1); i++)
ans = Math.Max(ans, arr[i]);
// If k is less than length of array
if (k < N)
ans = Math.Max(ans, arr[k]);
// Returning ans
return ans;
}
// Driver code
public static void Main()
{
int[] arr = { 5, 2, 2, 4, 0, 6 };
int N = 6;
int K = 4;
Console.Write(maximumTopMost(arr, K, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
5
时间复杂度: O(N)
辅助空间: O(1)