[LeetCode]#1441. Build an Array With Stack Operations

Environment: Python 3.7

Key technique: append

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 1:

Analysis:

  1. Check 1 in target and answer is yes. Add “Push” in list.
  2. Check 2 in target and answer is No. Add “Push” and “Pop” in list.
  3. Check 3 in target and answer is yes. Add “Push” in list.
  4. Return [‘Push’, ‘Push’, ‘Pop’, ‘Push’]

Solution:

Submission:

Reference:

https://zdyxry.github.io/2020/05/16/2020-%E7%AC%AC20%E5%91%A8-LeetCode-%E8%AE%B0%E5%BD%95/#more

--

--

Interesting in any computer science.

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