-
Notifications
You must be signed in to change notification settings - Fork 26
/
1441.BuildanArrayWithStackOperations.py
45 lines (40 loc) · 1.68 KB
/
1441.BuildanArrayWithStackOperations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Given an array target and an integer n. In each iteration, you will read a
number from list = {1,2,3..., n}.
Build the target array using the following operations:
- Push: Read a new element from the beginning list, and push it in the
array.
- Pop: delete the last element of the array.
- If the target array is already built, stop reading more elements.
You are guaranteed that the target array is strictly increasing, only
containing numbers between 1 to n inclusive.
Return the operations to build the target array.
You are guaranteed that the answer is unique.
Example:
Input: target = [1,3], n = 3
Output: ["Push","Push","Pop","Push"]
Explanation:
Read number 1 and automatically push in the array -> [1]
Read number 2 and automatically push in the array then Pop it -> [1]
Read number 3 and automatically push in the array -> [1,3]
Constraints:
- 1 <= target.length <= 100
- 1 <= target[i] <= 100
- 1 <= n <= 100
- target is strictly increasing.
"""
#Difficulty: Easy
#48 / 48 test cases passed.
#Runtime: 28 ms
#Memory Usage: 13.8 MB
#Runtime: 28 ms, faster than 90.11% of Python3 online submissions for Build an Array With Stack Operations.
#Memory Usage: 13.8 MB, less than 70.51% of Python3 online submissions for Build an Array With Stack Operations.
class Solution:
def buildArray(self, target: List[int], n: int) -> List[str]:
operations = []
n = min(n, target[-1]) + 1
for i in range(1, n):
operations += ["Push"]
if i not in target:
operations += ["Pop"]
return operations