Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROBLEM] 1441. Build an Array With Stack Operations #222

Open
godkingjay opened this issue Nov 3, 2023 · 1 comment
Open

[PROBLEM] 1441. Build an Array With Stack Operations #222

godkingjay opened this issue Nov 3, 2023 · 1 comment
Labels
difficulty: medium Medium difficulty problem. problem LeetCode problem.

Comments

@godkingjay
Copy link
Owner

Difficulty

Medium

Problem 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.

Link

https://leetcode.com/problems/build-an-array-with-stack-operations/

@godkingjay godkingjay added problem LeetCode problem. difficulty: medium Medium difficulty problem. labels Nov 3, 2023
@sinhaaayush10
Copy link

class Solution {
public List buildArray(int[] target, int n) {
List l = new ArrayList<>();
Stack stack = new Stack<>();

    int k=1;
    int i=0;

    while(i<target.length && k<=n){
        stack.push(target[i]);
        l.add("Push");
        if(target[i]==k){
            i++;
            k++;
        }else{
            stack.pop();
            l.add("Pop");
            k++;
        }
    }
    return l;

    
}

}
in JAVA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: medium Medium difficulty problem. problem LeetCode problem.
Projects
None yet
Development

No branches or pull requests

2 participants