Skip to main content

Stack - Question 1

Questions: We have an integer array with size n. The index of the first number is 0, and the last one is n-1. For each number, please find the index of the first number higher than it on the right side. If there is no such number, return -1.

Constains:

1 <= n <= 10^5


Analysis:

If we fix each number, then run a for loop for all the numbers on its right side in the array, we can find the first number that is larger. But the time complexity would be O(N^2) overall.

We need to find a solution that runs faster.

A stack can be used for this: since we are looking for the first element higher on the right side, we can maintain a monotonically decreasing order inside the stack. When scanning to a new number, 

1. if the stack is empty or the top element (the end of the numbers in the stack) is larger, just put the new number in the stack. Since the new number is smaller, the numbers in the stack still decreases;

2. if the stack is not empty and the top element is smaller than the new number, we need to pop up the top number, the first first larger number than it is just the new number!

3. we need to keep to do step 2 if the condition is still valid after popping out the top element;

4. when the condition in step2 becomes invalid, it becomes step 1;

5. after reaching the the last element, all the elements still inside the stack have no number larger than them on the right side, so we should return -1 for all of them.

Since this question asks for the index, so we need to store the index of the numbers in the stack, rather than the numbers.


See the code below:


class Solution {
public:
    vector<int> firstLargerIndex(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n, -1);
        stack<int> sk;
        for(int i=0; i<n; ++i) {
            while(sk.size() && nums[sk.top()] < nums[i]) {
                res[sk.top()] = i;
                sk.pop();
            }
            sk.push(i);
        }
        return res;
    }
};



 Upper Layer

Comments

Popular posts from this blog

Trie

Trie is a data structure belonging to n-ary trees, which is designed for some specific questions, such as, prefix of strings. (If you have never known what binary tree is, maybe you can check binary tree first before read more about Trie). The fact that there are only 26 different letters greatly reduce the possible choices for each position. Or to make it clear, for each position, there are 26 different choices. In mathematics, it seems to be very large (height^26 for the full tree), but in reality, it is rare to be a full tree for the set of meaningful strings (needs at least height^26 strings to be a full tree!), since the height or length of English words are less than 10 in most cases. Therefore, it is popular in string matching or related operations. Please be noted that the trie structure is not only limited to strings, the same idea can be extended to other data types, such as integer (then it becomes a binary tree, since for bit, there are only two possible values). In the cod...

Backtracking - Question 3

Leetcode 37 . Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. The '.' character indicates empty cells. Constraints: board.length == 9 board[i].length == 9 board[i][j] is a digit or '.'. It is guaranteed that the input board has only one solution. Analysis: This is a another classic backtracking question. We can try to solve the Sudoku row by row. Base case: if finish the last row, then we find a one solution, done; if finish one row, we can move onto the next row; if the current position is a number, move onto the next position; if not a number, we can make nine trials: 1 - 9. If one of them can reach the end and return true, we find one solution, done; if none of the...