给定一个整数N代表N人站在一个圆圈中,任务是找到当一个人以顺时针方向杀死紧邻的邻居时剩下的最后 2 个人。
例子:
Input: N = 5
Output: 1 4
Explanation:
Initially: 1 2 3 4 5
=> 1 kills 3
Standing: 1 2 4 5
=> 2 kills 5
Standing: 1 2 4
=> 4 kills 2
Final Standing: 1 4
Input: N = 2
Output: 1 2
朴素的方法:一个简单的方法是保留一个大小为 N 的bool 数组来跟踪一个人是否还活着。
- 最初,布尔数组对所有人都成立。
- 保留两个指针,一个指向当前活着的人,第二个存储前一个当前人。
- 一旦从当前人中找到第二个活着的邻居,将其布尔值更改为 false。
- 然后再次将当前更新为从前一个活动的下一个。
- 这个过程会一直持续到最后两个人幸存下来。
时间复杂度: O(N 2 )
辅助空间: O(N)
有效的方法:有效的方法是从数据结构中删除已经死亡的人,这样它就不会被再次遍历。
- 仅完成一轮后,最多只有 N/2 人。
- 然后在下一轮中,它将留下 N/4 个人,依此类推,直到活着的人数变为 2。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Node for a Linked List
struct Node {
int val;
struct Node* next;
Node(int _val)
{
val = _val;
next = NULL;
}
};
// Function to find the last 2 survivors
void getLastTwoPerson(int n)
{
// Total is the count
// of alive people
int total = n;
struct Node* head = new Node(1);
struct Node* temp = head;
// Initiating the list of n people
for (int i = 2; i <= n; i++) {
temp->next = new Node(i);
temp = temp->next;
}
temp->next = head;
temp = head;
struct Node* del;
// Total != 2 is terminating
// condition because
// at last only two-person
// will remain alive
while (total != 2) {
// Del represent next person
// to be deleted or killed
del = temp->next->next;
temp->next->next
= temp->next->next->next;
temp = temp->next;
free(del);
total -= 1;
}
// Last two person to
// survive (in any order)
cout << temp->val << " "
<< temp->next->val;
}
// Driver code
int main()
{
int n = 2;
getLastTwoPerson(n);
return 0;
}
Java
// Java implementation of the approach
class GFG{
// Node for a Linked List
static class Node
{
int val;
Node next;
Node(int _val)
{
val = _val;
next = null;
}
};
// Function to find the last 2 survivors
static void getLastTwoPerson(int n)
{
// Total is the count
// of alive people
int total = n;
Node head = new Node(1);
Node temp = head;
// Initiating the list of n people
for(int i = 2; i <= n; i++)
{
temp.next = new Node(i);
temp = temp.next;
}
temp.next = head;
temp = head;
Node del;
// Total != 2 is terminating
// condition because
// at last only two-person
// will remain alive
while (total != 2)
{
// Del represent next person
// to be deleted or killed
del = temp.next.next;
temp.next.next = temp.next.next.next;
temp = temp.next;
del = null;
System.gc();
total -= 1;
}
// Last two person to
// survive (in any order)
System.out.print(temp.val + " " +
temp.next.val);
}
// Driver code
public static void main(String[] args)
{
int n = 2;
getLastTwoPerson(n);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Node for a Linked List
class newNode:
def __init__(self, val):
self.val = val
self.next = None
# Function to find the last 2 survivors
def getLastTwoPerson(n):
# Total is the count
# of alive people
total = n
head = newNode(1)
temp = head
# Initiating the list of n people
for i in range(2, n + 1, 1):
temp.next = newNode(i)
temp = temp.next
temp.next = head
temp = head
de = None
# Total != 2 is terminating
# condition because
# at last only two-person
# will remain alive
while (total != 2):
# de represent next person
# to be deeted or killed
de = temp.next.next
temp.next.next = temp.next.next.next
temp = temp.next
del de
total -= 1
# Last two person to
# survive (in any order)
print(temp.val, temp.next.val)
# Driver code
if __name__ == '__main__':
n = 2
getLastTwoPerson(n)
# This code is contributed by SURENDRA_GANGWAR
C#
// C# implementation of the approach
using System;
class GFG{
// Node for a Linked List
class Node
{
public int val;
public Node next;
public Node(int _val)
{
val = _val;
next = null;
}
};
// Function to find the last 2 survivors
static void getLastTwoPerson(int n)
{
// Total is the count
// of alive people
int total = n;
Node head = new Node(1);
Node temp = head;
// Initiating the list of n people
for(int i = 2; i <= n; i++)
{
temp.next = new Node(i);
temp = temp.next;
}
temp.next = head;
temp = head;
Node del;
// Total != 2 is terminating
// condition because
// at last only two-person
// will remain alive
while (total != 2)
{
// Del represent next person
// to be deleted or killed
del = temp.next.next;
temp.next.next = temp.next.next.next;
temp = temp.next;
del = null;
total -= 1;
}
// Last two person to
// survive (in any order)
Console.Write(temp.val + " " +
temp.next.val);
}
// Driver code
public static void Main(String[] args)
{
int n = 2;
getLastTwoPerson(n);
}
}
// This code is contributed by Amit Katiyar
输出:
1 2
时间复杂度: O(N*log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live