给定一个正整数N ,任务是在交替重复执行以下操作后,从序列[1, N] 中打印最后一个剩余元素:
- 从序列中删除所有奇数索引元素。
- 从序列中删除所有偶数索引元素。
例子:
Input: N = 9
Output: 6
Explanation:
Sequence = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Step 1: Removing odd-indexed elements modifies sequence to {2, 4, 6, 8}
Step 2: Removing even-indexed elements modifies sequence to {2, 6}
Step 3: Removing odd-indexed elements modifies sequence to {6}
Therefore, the last remaining element is 6.
Input: N = 5
Output: 2
Explanation:
Sequence = {1, 2, 3, 4, 5}
Step 1: Removing odd-indexed elements modifies sequence to {2, 4}
Step 2: Removing even-indexed elements modifies sequence to {2}
Therefore, the last remaining element is 2.
朴素方法:最简单的方法是将1到N 的所有元素按顺序存储在一个数组中。对于每个操作,从数组中删除元素并将剩余元素向左移动。将数组缩减为单个元素后,将剩余的元素打印为所需答案。
时间复杂度: O(N 2 *log N)
辅助空间: O(N)
高效方法:上述方法可以使用动态规划进行优化。
递推关系如下:
where, i is in the range [1, N]
dp[i] stores the answer when the array elements are from 1 to i.
请按照以下步骤解决问题:
- 初始化数组dp[] ,其中dp[i]存储剩余元素或序列[1, i] 。
- 对于i = 1的基本条件,打印1作为所需答案。
- 使用前面提到的递推关系计算dp[N]的值,并使用已经计算的子问题来避免重叠子问题的重新计算。
- 完成以上步骤后,打印dp[N]的值作为结果。
下面是上述方法的实现:
C++14
// C++14 program for the above approach
#include
using namespace std;
// Function to calculate the last
// remaining element from the sequence
int lastRemaining(int n, map &dp)
{
// If dp[n] is already calculated
if (dp.find(n) != dp.end())
return dp[n];
// Base Case:
if (n == 1)
return 1;
// Recursive call
else
dp[n] = 2 * (1 + n / 2 -
lastRemaining(n / 2, dp));
// Return the value of dp[n]
return dp[n];
}
// Driver Code
int main()
{
// Given N
int N = 5;
// Stores the
map dp;
// Function call
cout << lastRemaining(N, dp);
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to calculate the last
// remaining element from the sequence
static int lastRemaining(int n, HashMap dp)
{
// If dp[n] is already calculated
if (dp.containsKey(n))
return dp.get(n);
// Base Case:
if (n == 1)
return 1;
// Recursive call
else
dp.put(n, 2 * (1 + n / 2 -
lastRemaining(n / 2, dp)));
// Return the value of dp[n]
return dp.get(n);
}
// Driver Code
public static void main(String[] args)
{
// Given N
int N = 5;
// Stores the
HashMap dp = new HashMap();
// Function call
System.out.print(lastRemaining(N, dp));
}
}
// This code is contributed by Princi Singh
Python3
# Python program for the above approach
# Function to calculate the last
# remaining element from the sequence
def lastRemaining(n, dp):
# If dp[n] is already calculated
if n in dp:
return dp[n]
# Base Case:
if n == 1:
return 1
# Recursive Call
else:
dp[n] = 2*(1 + n//2
- lastRemaining(n//2, dp))
# Return the value of dp[n]
return dp[n]
# Driver Code
# Given N
N = 5
# Stores the
dp = {}
# Function Call
print(lastRemaining(N, dp))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate the last
// remaining element from the sequence
static int lastRemaining(int n, Dictionary dp)
{
// If dp[n] is already calculated
if (dp.ContainsKey(n))
return dp[n];
// Base Case:
if (n == 1)
return 1;
// Recursive call
else
dp.Add(n, 2 * (1 + n / 2 -
lastRemaining(n / 2, dp)));
// Return the value of dp[n]
return dp[n];
}
// Driver Code
public static void Main(String[] args)
{
// Given N
int N = 5;
// Stores the
Dictionary dp = new Dictionary();
// Function call
Console.Write(lastRemaining(N, dp));
}
}
// This code is contributed by Princi Singh
Javascript
2
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。