|
| 1 | +/* |
| 2 | +Problem Link: https://leetcode.com/problems/build-an-array-with-stack-operations/ |
| 3 | +
|
| 4 | +Problem Statement: You are given an integer array target and an integer n. |
| 5 | +
|
| 6 | +You have an empty stack with the two following operations: |
| 7 | +
|
| 8 | +"Push": pushes an integer to the top of the stack. |
| 9 | +"Pop": removes the integer on the top of the stack. |
| 10 | +You also have a stream of the integers in the range [1, n]. |
| 11 | +
|
| 12 | +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: |
| 13 | +
|
| 14 | +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. |
| 15 | +If the stack is not empty, pop the integer at the top of the stack. |
| 16 | +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. |
| 17 | +Return the stack operations needed to build target following the mentioned rules. If there are multiple valid answers, return any of them. |
| 18 | +
|
| 19 | +Solution Approach: |
| 20 | +The code builds an array following a given sequence of push ("Push") and pop ("Pop") operations to |
| 21 | +create an array arr. |
| 22 | +The algorithm iterates through the arr and the goal is to reach the values mentioned in arr. |
| 23 | + If the current value in arr is greater than the current stream value (stream), |
| 24 | + it performs pushes until they match. If there are any unnecessary elements on the stack, |
| 25 | + it performs pops to discard them. If the stream is equal to the current value in arr, |
| 26 | + it adds a push operation. |
| 27 | +
|
| 28 | +This process ensures that the list contains the correct sequence of operations to transform the |
| 29 | +stream into the desired array arr. |
| 30 | +
|
| 31 | +*/ |
| 32 | + |
| 33 | +/* ------------CODE---------------- */ |
| 34 | + |
| 35 | +class Solution { |
| 36 | + public List<String> buildArray(int[] arr, int n) { |
| 37 | + |
| 38 | + List<String> list = new ArrayList<>(); |
| 39 | + int stream = 1; |
| 40 | + |
| 41 | + for(int i=0; i<arr.length; i++) { |
| 42 | + int curr = arr[i]; |
| 43 | + int count = 0; |
| 44 | + |
| 45 | + // Keep pushing until you reach the current value |
| 46 | + while(stream < curr) { |
| 47 | + list.add("Push"); |
| 48 | + stream++; |
| 49 | + count++; |
| 50 | + } |
| 51 | + |
| 52 | + // Pop the elements not needed in the target array |
| 53 | + while(count > 0) { |
| 54 | + count--; |
| 55 | + list.add("Pop"); |
| 56 | + } |
| 57 | + |
| 58 | + // If the stream element is equal to the current one, add a push |
| 59 | + if(stream == curr) { |
| 60 | + list.add("Push"); |
| 61 | + stream++; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return list; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/* |
| 70 | +Time Complexity: O(n) |
| 71 | +Space Complexity: O(n) - for the return output |
| 72 | +*/ |
0 commit comments