This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- history: [leetcode, youtube, facebook, google] | |
- urlPosition: i | |
visit: pop out all urls after the urlPosition, append the url to visit and move the urlPosition | |
- history: [leetcode, youtube, facebook, google], steps = 2 | |
- urlPosition: i | |
back and forward --> you don't want to go out of bound because of the steps | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
DFS | |
[ | |
[1,1,1,1], | |
[1,1,1,1], | |
[1,1,1,1], | |
[1,1,1,1] | |
] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
cache --> using memory to speed up the computer algorithms | |
* all the key values are positive so we can return something that won't clash with -1 when the key is not found in the cache | |
- We could use OrderedDict library in Python which remembers the order in which elements are inserted | |
- Get or put requires us to delete or insert at different positions. We can use a doubly linked list because deletion and insertions are constant time, but we also need the linked list for proper ordering. | |
- When we delete things, we pop it off the right (most recent used) | |
left <-> [1,1] <-> [2,2] <-> right | |
lru mru |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
3[a2[c]] | |
- Number in front of a character shows how many times that character will be multiplied | |
- 3 applies to everything in the bracket | |
- acc * 3 ==> accaccacc | |
- Nested brackets will make the solution tricky/complicated | |
- We will have to solve the inner brackets before the outer brackets --> recursive solution |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation I: Brute-force solution | |
- Convert the transactions to individual item of the valid data types and store it in an array. i.e. alice = string, 20 = integer, 800 = integer and mtv = string | |
- Loop through the new array and find where amount > 1000. Append that to result array and convert the transaction to a string splitting each item by comma | |
- Nested loop to check for same name, and if time difference is less than or equal to 60 and if different city, | |
- Append that to the res array and convert the transaction to a string splitting each item by comma | |
- TC - O(n-squared) | |
- SC - O(n) | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- deeedbbcccbdaa | |
[[d,1], [e,3]] | |
[[d,2]] | |
TC - O(2n) = O(n) where n is the size of the input string | |
SC - O(n) | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- Major steps to determine which candies to crush, | |
1. Check three or more candies to be crushed in rows | |
2. Check three or more candies to be crushed in columns | |
3. Dropping the candies | |
Steps 1 & 2: * This is an issue when considering columns --> [3,1,1,1,1,5] = [3,0,0,0,0,5] | |
Can we check for rows and columns simultaneously? Yes. We can use a slider of three at a time and check for the absolute values of the three integers. If the absolute values are the same, then we can change the integers to negative. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- We have to pick equal number of A and B | |
Initially, | |
- Can we see if we can send all candidates to city A? | |
- Now, which of the candidates can we send to B? We will pick the candidate for which we gained the maximum (the most negative number) | |
10 + 30 + 400 + 30 | |
+10 +170 -350 -10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- We will have a recursive helper (flatten_helper) function that takes in the head pointer (curr = head, tail = head) | |
- While curr is not null, | |
- Here we can store curr, next and tail on the first node (head) | |
- If no child, | |
current = next and next will point to next of current | |
- If it has child, | |
We flatten the child. i.e. child_tail will be flatten_helper function called on child. Recursive function here | |
curr --> next = child |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- id passed in checkIn and checkOut: unique identifier to our customer | |
- Each customer can only be checked into a station one place at a time | |
1. Create a map called arrivals. key = id, value = tuple containing stationName and time --> (stationName, t) | |
2. To keep track of all previous travels between any two stations, we create another map called travels. key = stationNames separated by a delimiter, value = count, total --> [count, total] | |
Example: | |
Calls Arrivals Travels |