[Leet Code] Build an Array With Stack Operations

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]
Input: target = [1,2,3], n = 3
Output: ["Push","Push","Push"]
Input: target = [1,2], n = 4
Output: ["Push","Push"]
Explanation: You only need to read the first 2 numbers and stop.
Input: target = [2,3,4], n = 4
Output: ["Push","Pop","Push","Push","Push"]
class Solution(object):
def buildArray(self, target, n):
"""
:type target: List[int]
:type n: int
:rtype: List[str]
"""
result_list = []
for i in range(1,n+1):
if i>target[len(target)-1]:
return result_list
result_list.append("Push")
if i not in target:
result_list.append("Pop")
return result_list

--

--

--

Learning, and posting my findings!

Love podcasts or audiobooks? Learn on the go with our new app.

MEAN STACK — Part I

Introduction To Throttling Using JavaScript

Load More Data in Laravel Using Ajax jQuery

[Leet Code] How Many Numbers Are Smaller Than the Current Number

Case Study: Remote for Slides, a new way to control your presentation slides

JavaScript Shinies: Async/Await

9 Ways to Reduce Development Costs Using Components

Adobe Target: Response Token and Debugging approach

Response tokens — Target call

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Boyd

Matthew Boyd

Learning, and posting my findings!

More from Medium

Linked List — Doubly

PlatinumX Tech’s Algorithm Expert Course Review (How I got into Turing).

Data Structure — Linked List

MCLT — More Code Less Talk

The pandemic of Data Structures and Algorithms Courses