-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-2023-11-03.py
37 lines (33 loc) · 953 Bytes
/
leetcode-2023-11-03.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
"""
Question Link: https://leetcode.com/problems/build-an-array-with-stack-operations/
"""
class Solution(object):
def buildArray(self, target, n):
"""
:type target: List[int]
:type n: int
:rtype: List[str]
"""
# ops = []
# s = []
# for i in range(1,n+1):
# if s and s[-1] not in target:
# s.pop()
# ops.append("Pop")
# s.append(i)
# ops.append("Push")
# if s==target:
# return ops
# return ops
ops = []
if target[0]>1:
ops.extend(["Push","Pop"]*(target[0]-1))
ops.append("Push")
for i in range(1,len(target)):
temp = target[i]-target[i-1]
if temp>1:
ops.extend(["Push","Pop"]*(temp-1))
ops.append("Push")
else:
ops.append("Push")
return ops