Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/1533237294978764801
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I just attempted the Maximum Element Challenge on Hackerrank. This question has been posted before in swift Swift Hackerrank Maximum Element in a Stack Swift Hackerrank Maximum Element in a Stack but it has attracted low views/no answers. Moving forward, based on the requirements of finding the maximum element, I used a vector<int> as my underlying data structure as opposed to using Stack as I can't iterate through a stack. This exercise is meant to be a simple one but the time limit gets exceeded for larger input.

I just attempted the Maximum Element Challenge on Hackerrank. This question has been posted before in swift Swift Hackerrank Maximum Element in a Stack but it has attracted low views/no answers. Moving forward, based on the requirements of finding the maximum element, I used a vector<int> as my underlying data structure as opposed to using Stack as I can't iterate through a stack. This exercise is meant to be a simple one but the time limit gets exceeded for larger input.

I just attempted the Maximum Element Challenge on Hackerrank. This question has been posted before in swift Swift Hackerrank Maximum Element in a Stack but it has attracted low views/no answers. Moving forward, based on the requirements of finding the maximum element, I used a vector<int> as my underlying data structure as opposed to using Stack as I can't iterate through a stack. This exercise is meant to be a simple one but the time limit gets exceeded for larger input.

added 9 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

C++ Implementationimplementation of Hackerrank's Maximum"Maximum Element in a StackStack"

How do I improve the performance? pleasePlease feel free to criticise my approach as I'm new to C++ with 5 days of experience.

You have an empty sequence, and you will be given \$N\$ queries. Each query is one of these three types:

  • 1 x -Push the element x into the stack
  • 2 -Delete the element present at the top of the stack
  • 3 -Print the maximum element in the stack

Input Format

The first line of input contains an integer, \$N\$ . The next \$N\$ lines each contain an above mentioned query. (It is guaranteed that each query is valid.)

Constraints

  • \1ドル \leq N \leq 10^5 \$
  • \1ドル \leq x \leq 10^9 \$
  • \1ドル \leq type \leq 3 \$\1ドル \leq \text{type} \leq 3 \$

Output Format

For each type \3ドル\$ query, print the maximum element in the stack on a new line.

Sample Input

10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3

Sample Output

26
91

C++ Implementation of Hackerrank's Maximum Element in a Stack

How do I improve the performance? please free to criticise my approach as I'm new to C++ with 5 days experience

You have an empty sequence, and you will be given \$N\$ queries. Each query is one of these three types:

  • 1 x -Push the element x into the stack
  • 2 -Delete the element present at the top of the stack
  • 3 -Print the maximum element in the stack

Input Format

The first line of input contains an integer, \$N\$ . The next \$N\$ lines each contain an above mentioned query. (It is guaranteed that each query is valid.)

Constraints

  • \1ドル \leq N \leq 10^5 \$
  • \1ドル \leq x \leq 10^9 \$
  • \1ドル \leq type \leq 3 \$

Output Format

For each type \3ドル\$ query, print the maximum element in the stack on a new line.

Sample Input

10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3

Sample Output

26
91

C++ implementation of Hackerrank's "Maximum Element in a Stack"

How do I improve the performance? Please feel free to criticise my approach as I'm new to C++ with 5 days of experience.

You have an empty sequence, and you will be given \$N\$ queries. Each query is one of these three types:

  • 1 x -Push the element x into the stack
  • 2 -Delete the element present at the top of the stack
  • 3 -Print the maximum element in the stack

Input Format

The first line of input contains an integer, \$N\$ . The next \$N\$ lines each contain an above mentioned query. (It is guaranteed that each query is valid.)

Constraints

  • \1ドル \leq N \leq 10^5 \$
  • \1ドル \leq x \leq 10^9 \$
  • \1ドル \leq \text{type} \leq 3 \$

Output Format

For each type \3ドル\$ query, print the maximum element in the stack on a new line.

Sample Input

10
1 97
2
1 20
2
1 26
1 20
2
3
1 91
3

Sample Output

26
91
edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Source Link
Tolani
  • 2.5k
  • 7
  • 31
  • 49
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /