返回介绍

solution / 1400-1499 / 1441.Build an Array With Stack Operations / README_EN

发布于 2024-06-17 01:03:19 字数 6288 浏览 0 评论 0 收藏 0

1441. Build an Array With Stack Operations

中文文档

Description

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 build _target 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.

Solutions

Solution 1

class Solution:
  def buildArray(self, target: List[int], n: int) -> List[str]:
    cur, ans = 0, []
    for v in target:
      cur += 1
      while cur < v:
        ans.extend(['Push', 'Pop'])
        cur += 1
      ans.append('Push')
    return ans
class Solution {
  public List<String> buildArray(int[] target, int n) {
    int cur = 0;
    List<String> ans = new ArrayList<>();
    for (int v : target) {
      while (++cur < v) {
        ans.add("Push");
        ans.add("Pop");
      }
      ans.add("Push");
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> buildArray(vector<int>& target, int n) {
    int cur = 0;
    vector<string> ans;
    for (int& v : target) {
      while (++cur < v) {
        ans.emplace_back("Push");
        ans.emplace_back("Pop");
      }
      ans.emplace_back("Push");
    }
    return ans;
  }
};
func buildArray(target []int, n int) []string {
  cur := 0
  ans := []string{}
  for _, v := range target {
    for cur = cur + 1; cur < v; cur++ {
      ans = append(ans, "Push", "Pop")
    }
    ans = append(ans, "Push")
  }
  return ans
}
function buildArray(target: number[], n: number): string[] {
  const res = [];
  let cur = 0;
  for (const num of target) {
    while (++cur < num) {
      res.push('Push', 'Pop');
    }
    res.push('Push');
  }
  return res;
}
impl Solution {
  pub fn build_array(target: Vec<i32>, n: i32) -> Vec<String> {
    let mut res = Vec::new();
    let mut cur = 1;
    for &num in target.iter() {
      while cur < num {
        res.push("Push");
        res.push("Pop");
        cur += 1;
      }
      res.push("Push");
      cur += 1;
    }
    res.into_iter().map(String::from).collect()
  }
}
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char** buildArray(int* target, int targetSize, int n, int* returnSize) {
  char** res = (char**) malloc(sizeof(char*) * n * 2);
  int cur = 1;
  int i = 0;
  for (int j = 0; j < targetSize; j++) {
    while (++cur < target[j]) {
      res[i] = (char*) malloc(sizeof(char) * 8);
      strcpy(res[i++], "Push");
      res[i] = (char*) malloc(sizeof(char) * 8);
      strcpy(res[i++], "Pop");
    }
    res[i] = (char*) malloc(sizeof(char) * 8);
    strcpy(res[i++], "Push");
  }
  *returnSize = i;
  return res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文