LoginSignup
0
0

Leetcode 1441. Build an Array With Stack Operations

Posted at

class Solution {
    public List<String> buildArray(int[] target, int n) {
        ArrayList<String> result = new ArrayList<>();
        
        int index = 0;
        int currNum = 1;

        while (index < target.length && currNum <= n) {
            int num = target[index];
            if(num == currNum){
                index++;
                currNum++;
                result.add("Push");
            } else {
                result.add("Push");
                result.add("Pop");
                currNum++;
            }
        }
        return result;
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0