跳至主要內容

1441. Build an Array With Stack Operations


1441. Build an Array With Stack Operationsopen in new window

🟠   🔖  数组 模拟  🔗 LeetCodeopen in new window

题目

You are given an integer array target and an integer n.

You have an empty stack with the two following operations:

  • "Push": pushes an integer to the top of the stack.
  • "Pop": removes the integer on the top of the stack.

You also have a stream of the integers in the range [1, n].

Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to target. You should follow the following rules:

  • If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.
  • If the stack is not empty, pop the integer at the top of the stack.
  • If, at any moment, the elements in the stack (from the bottom to the top) are equal to target, do not read new integers from the stream and do not do more operations on the stack.

Return the stack operations needed to buildtarget following the mentioned rules. If there are multiple valid answers, return any of them.

Example 1:

Input: target = [1,3], n = 3

Output: ["Push","Push","Pop","Push"]

Explanation: Initially the stack s is empty. The last element is the top of the stack.

Read 1 from the stream and push it to the stack. s = [1].

Read 2 from the stream and push it to the stack. s = [1,2].

Pop the integer on the top of the stack. s = [1].

Read 3 from the stream and push it to the stack. s = [1,3].

Example 2:

Input: target = [1,2,3], n = 3

Output: ["Push","Push","Push"]

Explanation: Initially the stack s is empty. The last element is the top of the stack.

Read 1 from the stream and push it to the stack. s = [1].

Read 2 from the stream and push it to the stack. s = [1,2].

Read 3 from the stream and push it to the stack. s = [1,2,3].

Example 3:

Input: target = [1,2], n = 4

Output: ["Push","Push"]

Explanation: Initially the stack s is empty. The last element is the top of the stack.

Read 1 from the stream and push it to the stack. s = [1].

Read 2 from the stream and push it to the stack. s = [1,2].

Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.

The answers that read integer 3 from the stream are not accepted.

Constraints:

  • 1 <= target.length <= 100
  • 1 <= n <= 100
  • 1 <= target[i] <= n
  • target is strictly increasing.

题目大意

给你一个数组 target 和一个整数 n。每次迭代,需要从 list = { 1 , 2 , 3 ..., n } 中依次读取一个数字。

请使用下述操作来构建目标数组 target

  • "Push":从 list 中读取一个新元素, 并将其推入数组中。
  • "Pop":删除数组中的最后一个元素。
  • 如果目标数组构建完成,就停止读取更多元素。
  • 题目数据保证目标数组严格递增,并且只包含 1n 之间的数字。

解题思路

操作的对象是 1n 按顺序排列的数字,因为 target 中数字是严格递增的,因此只要遍历 1 ~ n ,每次操作一个数字时,与 target 的第 idx 个元素对比:

  • 如果它与 target[idx] 相等,则只需要将它 Push 入栈即可,idx++
  • 如果它与 target[idx] 中,可以先将其 Push 入栈,紧接着 Pop 出栈;
  • idxtarget 的长度相等时,说明目标数组已经构建完成,调出循环;

代码

/**
 * @param {number[]} target
 * @param {number} n
 * @return {string[]}
 */
var buildArray = function (target, n) {
  let idx = 0;
  let res = [];
  for (let i = 1; i <= n; i++) {
    if (target[idx] != i) {
      res.push("Push");
      res.push("Pop");
    } else if (target[idx] === i) {
      res.push("Push");
      idx++;
    }
    if (idx === target.length) break;
  }
  return res;
};

相关题目

相关题目
- [2869. Minimum Operations to Collect Elements](https://leetcode.com/problems/minimum-operations-to-collect-elements)