先决条件:堆栈,散列
给定一堆N个数字和一个数字数组。计算获取数组的每个元素所需的弹出操作数。弹出元素后,就不会再将其推回。假设数组中的所有元素最初都位于堆栈内部。
例子:
Input: N = 5
Stack: 6 4 3 2 1
Array: 6 3 4 1 2
Output: 1 2 0 2 0
The 1st element of the stack is the same as the array elements. So to get 6, one pop is required i.e. pop 6 from the stack. To get 3, 2 elements will be popped i.e. 4 and 3. To get 4, 4 was already popped, thus we won’t pop more elements. Similarly, to get 1, we will pop 2 elements and for the 2, no pop count will be added.
方法:可以通过使用堆栈轻松解决此问题。我们将不断弹出元素,直到找到要搜索的元素。唯一的障碍是,当元素已经弹出并且不在堆栈中时,如何处理这种情况。为此,我们将维护一个哈希映射。当我们从堆栈中弹出一个元素时,我们会将其插入到哈希图中,以便如果该元素稍后出现在数组中,我们将首先检查其是否存在于哈希图中,或者换句话说,是否先前已从堆栈中弹出该元素。否则,我们将知道它存在于堆栈中,并且我们将开始弹出元素,直到找到所需的数字为止。
下面是上述方法的实现:
C++
// C++ program to implement above approach
#include
using namespace std;
// Function to find the count
void countEle(stack& s, int a[], int N)
{
// Hashmap to store all the elements
// which are popped once.
unordered_map mp;
for (int i = 0; i < N; ++i) {
int num = a[i];
// Check if the number is present
// in the hashmap Or in other words
// been popped out from the stack before.
if (mp.find(num) != mp.end())
cout << "0 ";
else {
int cnt = 0;
// Keep popping the elements
// while top is not equal to num
while (s.top() != num) {
mp[s.top()] = true;
s.pop();
cnt++;
}
// Pop the top ie. equal to num
s.pop();
cnt++;
// Print the number of elements popped.
cout << cnt << " ";
}
}
}
// Driver code
int main()
{
int N = 5;
stack s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(6);
int a[] = { 6, 3, 4, 1, 2 };
countEle(s, a, N);
return 0;
}
Java
// Java program to implement above approach
import java.util.HashMap;
import java.util.Stack;
class GFG
{
// Function to find the count
public static void countEle(Stack s,
int[] a, int N)
{
// Hashmap to store all the elements
// which are popped once.
HashMap mp = new HashMap<>();
for (int i = 0; i < N; ++i)
{
int num = a[i];
// Check if the number is present
// in the hashmap Or in other words
// been popped out from the stack before.
if (mp.containsKey(num))
System.out.print("0 ");
else
{
int cnt = 0;
// Keep popping the elements
// while top is not equal to num
while (s.peek() != num)
{
mp.put(s.peek(), true);
s.pop();
cnt++;
}
// Pop the top ie. equal to num
s.pop();
cnt++;
// Print the number of elements popped.
System.out.print(cnt + " ");
}
}
}
// Driver code
public static void main(String[] args)
{
int N = 5;
Stack s = new Stack<>();
s.add(1);
s.add(2);
s.add(3);
s.add(4);
s.add(6);
int[] a = { 6, 3, 4, 1, 2 };
countEle(s, a, N);
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 program to implement above approach
# Function to find the count
def countEle(s, a, N):
# Hashmap to store all the elements
# which are popped once.
mp = {}
for i in range(0, N):
num = a[i]
# Check if the number is present
# in the hashmap Or in other words
# been popped out from the stack before.
if num in mp:
print("0", end = " ")
else:
cnt = 0
# Keep popping the elements
# while top is not equal to num
while s[-1] != num:
mp[s.pop()] = True
cnt += 1
# Pop the top ie. equal to num
s.pop()
cnt += 1
# Print the number of elements popped.
print(cnt, end = " ")
# Driver code
if __name__ == "__main__":
N = 5
s = []
s.append(1)
s.append(2)
s.append(3)
s.append(4)
s.append(6)
a = [6, 3, 4, 1, 2]
countEle(s, a, N)
# This code is contributed by Rituraj Jain
C#
// C# implementation of the above approach:
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the count
public static void countEle(Stack s,
int[] a, int N)
{
// Hashmap to store all the elements
// which are popped once.
Dictionary mp = new Dictionary();
for (int i = 0; i < N; ++i)
{
int num = a[i];
// Check if the number is present
// in the hashmap Or in other words
// been popped out from the stack before.
if (mp.ContainsKey(num))
Console.Write("0 ");
else
{
int cnt = 0;
// Keep popping the elements
// while top is not equal to num
while (s.Peek() != num)
{
mp.Add(s.Peek(), true);
s.Pop();
cnt++;
}
// Pop the top ie. equal to num
s.Pop();
cnt++;
// Print the number of elements popped.
Console.Write(cnt + " ");
}
}
}
// Driver code
public static void Main(String[] args)
{
int N = 5;
Stack s = new Stack();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(6);
int[] a = { 6, 3, 4, 1, 2 };
countEle(s, a, N);
}
}
// This code is contributed by PrinciRaj1992
1 2 0 2 0
时间复杂度: O(N)