Leetcode Amazon
use two stack A and B, and and a vector to store answer. first stack A is to store the reverse order of the linkedlist, so we can go from right to left. Now iterate and pop through stack A, at every element, iterate and pop through B until you find an element that's greater then A's element or B's empty if B is empty, add 0 to answer vector(make sure you push front, since you're filling from right to left) if not, that means you found an element that's greater then A, then push this element into answer vector. Then add A to B's stack. keep doing this until A is empty
1019. Next Greater Node In Linked List
Use two for loops to loop throught the 2d vector and a counter to loop through the pattern. if the counter is smaller then pattern size and word[i][j] == pattern[counter] increment counter else if word[i][j] is capital, break push true if counter == pattern.size() && j == word[i]size() else false
1023. Camelcase Matching
recurse on a function with root, min and max base case null return max-min else return max(root-left, min(root->val,min), max(root-val,max) (root-right, min(root->val,min), max(root-val,max)
1026. Maximum Difference Between Node and Ancestor
use bfs, tricky part is that you want to implement the path only once at every level. use a double while loop, and add the neighbors to a secondary queue. only swap the queue after everything in the previous level has been visisted while (bfs.size() != 0) { path++; queue<pair<int,int>> bfs2; while (bfs.size() != 0) { push neighbors to bfs2 } swap(bfs2, bfs);
1091. Shortest Path in Binary Matrix
use a list<pair<int,int>> to store the key value pair ordered by LRU. use another map <key, list<pair<int,int>> iterator> to quickly delete and move the key value pair around the list. when insert, check if it already exists, if it does, delete from map and list. else insert it to front of list and insert it to map if full size, delete the front of list, and remove it from map as well. when get, get the pair from the map, then delete it from list and map. insert it to the front of the list and add it back to map return result
146. LRU Cache
iterate a through the input string from 0 to the last index, at every index, choose this as the middle and pass it to a helper function in the form of s, leftindex and rightindex. two possible cases: i is the middle, then left = i and right = i middle is between i and i+1, then left = i and right = i+1; the helper function would return the longest palindrome possible given s, left and right. keep track of the longest length so far, and when you find a new length, make sure you update the longest substring as well.
5. Longest Palindromic Substring
Use a map to first store the last occurrence of the character. Iterate through the string with index i. Keep track of the largest last occurrence of the character of the partition with index x. When i == x that means we found a partition where all character inside the partition (length = i - head + 1) is the only occurrence. class Solution { public: vector<int> partitionLabels(string S) { unordered_map<char, int> lastIndex; for (int i = 0; i < S.size(); i++) { lastIndex[S[i]] = i; } vector<int> answer; int head = 0; int last = 0; for (int i = 0; i < S.size(); i++) { last = max(last, lastIndex[S[i]]); if (i == last) { answer.push_back(i-head+1); head = i + 1; } } return answer; } };
763. Partition Labels
The idea is to use two vectors to store the configuration of day 1 and day x, where x > 1 and keep modifying X. when day x == day 1. this mean that a cycle has been found, and we can reduce N by modding. so we don't have to iterate throught all N days. the initial for loop will iterate 0 to N, and we use a secondary variable i to indicate how the long the cycle is. the inner for loop will just modify the next vector based ont he current vector.Afterwards set current = next; Now check for cycle, if theres a cycle reduce N. vector<int> prisonAfterNDays(vector<int>& cells, int N) { vector<int> previous; vector<int> next(cells.size(), 0); for (int i = 0; N-- > 0; i++) { for (int x = 1; x < cells.size() - 1; x++) { if (cells[x-1] == cells[x+1]) { next[x] = 1; } else { next[x] = 0; } } cells = next; if (i == 0) { previous = next; } else if (previous == next) { N = N % i; } } return cells; }
957. Prison Cells After N Days
Define a custom comparator to compare the points, and use this comparator to create a min PQ pop from PQ, until PQ is empty or K == 0, decrement K by 1 during each pop. store element popped in answer vector
973. K Closest Points to Origin
Initialize 3 variable, answer = 1, inc = 1 and dec = 1 start from index 1, if the current val is greater then previous val, then we know that its the incrementing pattern, so we do inc = dec + 1; and set dec = 1 if current val is smaller then previous val, then we know that its the decrementing pattern, so we do dec = inc + 1 and set inc = 1 else it means there its a tie in val, we set both inc and dec to 1 then at every index we set answer = max(answer, max(inc, dec))
978. Longest Turbulent Subarray
use a set to keep track of the days. iterate through 1 to 365 day. when the day is not found. set dp[i] = dp[i-1], since we never need to visit this day, so the cost will be the same of previous day. else dp[i] = minimum of three values: dp[i-1] + costs[0], optimal cost from 1 day ago plus 1 day ticket dp[max(0,i-7)] + costs[1], optimal costs from 7 day ago plus 7 day ticket dp[max(0,i-30)] + costs[2], optimals costs from 30 day ago plus 30 day ticket. return dp[365] class Solution { public: int mincostTickets(vector<int>& days, vector<int>& costs) { unordered_set<int> daySet (begin(days), end(days)); vector<int> dp(366, 0); for (int i = 1; i < dp.size(); i++) { if (daySet.find(i) == daySet.end()) { dp[i] = dp[i-1]; } else { dp[i] =min({ dp[i - 1] + costs[0], dp[max(0, i - 7)] + costs[1], dp[max(0, i - 30)] + costs[2]}); } } return dp[365]; } };
983. Minimum Cost For Tickets
use width and height variable to describe each tree node. use a 2d map, where the value is a set to store all the node->val root->val will be stored at (width = 0, height = 0) its left children will be stored at (width = -1, height = 1) and its right children will be stored at (width = 1, height = 1). recurse through the trees and store all the nodes. use a double for loop to iterate through the 2d map and add the sets to answer. for (int width = -999 to 1000) for (int height = 0 to 1000) tempVector.push_back(map[width][height] answerVector.push_back(tempVector) returnn answerVector
987. Vertical Order Traversal of a Binary Tree