Skip to main content
Commonmark migration
Source Link

I've tried asking about the performance on the HackerRank discussion forum, it didn't work out.

The task is to write a program with three operations:

 1 x  Push the element x onto the stack.    
 2    Delete the element present at the top of the stack.   
 3    Print the maximum element in the stack.
 

The first input line is the number of lines in the program, all subsequent lines are one of the three instructions.

Sample Input:

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

Sample Output:

 26
 91

My Solution:

data = []
for _ in range(int(input())):
    ins = input().split()
    if ins[0] == '1':
        data.append(int(ins[1]))
    elif ins[0] == '2':
        data.pop()
    else:
        print(max(data))

It gets slow on working with input size of 1000 elements or so, how could I speed this up?

I've tried asking about the performance on the HackerRank discussion forum, it didn't work out.

The task is to write a program with three operations:

 1 x  Push the element x onto the stack.    
 2    Delete the element present at the top of the stack.   
 3    Print the maximum element in the stack.
 

The first input line is the number of lines in the program, all subsequent lines are one of the three instructions.

Sample Input:

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

Sample Output:

 26
 91

My Solution:

data = []
for _ in range(int(input())):
    ins = input().split()
    if ins[0] == '1':
        data.append(int(ins[1]))
    elif ins[0] == '2':
        data.pop()
    else:
        print(max(data))

It gets slow on working with input size of 1000 elements or so, how could I speed this up?

I've tried asking about the performance on the HackerRank discussion forum, it didn't work out.

The task is to write a program with three operations:

 1 x  Push the element x onto the stack.    
 2    Delete the element present at the top of the stack.   
 3    Print the maximum element in the stack.

The first input line is the number of lines in the program, all subsequent lines are one of the three instructions.

Sample Input:

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

Sample Output:

 26
 91

My Solution:

data = []
for _ in range(int(input())):
    ins = input().split()
    if ins[0] == '1':
        data.append(int(ins[1]))
    elif ins[0] == '2':
        data.pop()
    else:
        print(max(data))

It gets slow on working with input size of 1000 elements or so, how could I speed this up?

edited title
Link
Phrancis
  • 20.4k
  • 6
  • 68
  • 154

Where is it slow? Push, Delete and Print stack elements

Removed edits which did express opinions not mentioned by the author
Source Link
Graipher
  • 41.1k
  • 7
  • 69
  • 133

I've tried asking about the performance on the HackerRank discussion forum, it didn't work out. The question is:

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.
Output the largest number in the Stack

Sample InputThe task is to write a program with three operations:

 10
 1 97
x 2
 1Push 20
the 2
element 1x 26
onto 1the 20stack.    
 2
  3
 1 91 Delete the element present at the top of the stack.   
 3    Print the maximum element in the stack.
 

Sample Output:The first input line is the number of lines in the program, all subsequent lines are one of the three instructions.

 26
 91

Sample Input:

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

Sample Output:

 26
 91

My Solution:

data = []
for _ in range(int(input())):
    ins = input().split()
    if ins[0] == '1':
        data.append(int(ins[1]))
    elif ins[0] == '2':
        data.pop()
    else:
        print(max(data))

It gets slow on working with input size of 1000 elements or so, how could I speed this up?

I've tried asking about the performance on the HackerRank discussion forum, it didn't work out. The question is:

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.
Output the largest number in the Stack

Sample Input:

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

Sample Output:

 26
 91

My Solution:

data = []
for _ in range(int(input())):
    ins = input().split()
if ins[0] == '1':
    data.append(int(ins[1]))
elif ins[0] == '2':
    data.pop()
else:
    print(max(data))

It gets slow on working with input size of 1000 elements or so, how could I speed this up?

I've tried asking about the performance on the HackerRank discussion forum, it didn't work out.

The task is to write a program with three operations:

 1 x  Push the element x onto the stack.    
 2    Delete the element present at the top of the stack.   
 3    Print the maximum element in the stack.
 

The first input line is the number of lines in the program, all subsequent lines are one of the three instructions.

Sample Input:

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

Sample Output:

 26
 91

My Solution:

data = []
for _ in range(int(input())):
    ins = input().split()
    if ins[0] == '1':
        data.append(int(ins[1]))
    elif ins[0] == '2':
        data.pop()
    else:
        print(max(data))

It gets slow on working with input size of 1000 elements or so, how could I speed this up?

deleted 100 characters in body; edited tags
Source Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155
Loading
added 51 characters in body
Source Link
BCdotWEB
  • 11.3k
  • 2
  • 27
  • 45
Loading
Source Link
Loading