Skip to content

Instantly share code, notes, and snippets.

View doyedele1's full-sized avatar

Demilade Oyedele doyedele1

View GitHub Profile
@doyedele1
doyedele1 / sliding-window-median.py
Last active February 7, 2022 20:06
Solving LC Questions daily
class Solution:
def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]:
# Create an empty array to store your answer
res = []
# Get the length of the nums array
n = len(nums)
# Slice the input array to get data from index 0 to index k
win_arr = nums[:k-1]
@doyedele1
doyedele1 / int-to-rom.py
Created February 17, 2022 06:59
Question 12 on LeetCode
'''
Explanation:
- Note: Knowing which representation is the correct one for a particular number can be difficult. There are many possible ways of representing the number 120 for instance.
- So if num = 9, X and I are needed. Rule of roman numeral goes from largest to smallest. However, there are some special rules where the symbol with a smaller value can go before the symbol with a larger value.
- If there are no special rules, when num = 1500,
- We can divide 1500/M. i.e. 1500//1000 = 1. 1 tells us how many Ms in our final result.
- Then 1500 % 1000 = 500
- 500 // 500 = 1. 1D in the final result
'''
Explanation:
- n = 19 --> 82 --> 68 --> 100 --> 1
n // 10 = firstDigit
n % 10 = lastDigit
- n = 2 --> 4 --> 16 --> 37 --> 58 --> 89 --> 145 --> 42 --> 20 --> 4
Since we've seen 4 before, then we know that the number is not happy.
Time Complexity: O(log n) - finding the sum square for a given number has a cost of O(logn) because we are processing each digit in the number, and the number of digits in a number is given by (log n): For n > 243, O(243 * 3 + log n + log log n + ....) = O(log n)
@doyedele1
doyedele1 / solution.py
Created March 8, 2022 20:33
Container with Most Water
'''
Explanation:
- Intuitvely, we can find the possible areas for each pair of heights
- For each pair, we check the area by multiplying the width to the height without spill over. We need to consider the minimum height between the pair.
- However, can we reduce the number of times we find the possible areas for every pair of the heights which is caused by the minimum height in each pair?
- Yes, we can. We can have two pointers at both endpoints and perform the area of the ractangle.
- When the height is minimum for a left pointer, we can move to the pointer to the right and when the height is minimum for a right pointer, we move the pointer to the left to the right. This is done to look for the next possible maximum height.
- TC: O(n), SC: O(1)
'''
@doyedele1
doyedele1 / design-underground-system.py
Created March 14, 2022 08:45
Design Underground System
'''
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
@doyedele1
doyedele1 / flatten-a-multilevel-dll.py
Created March 14, 2022 08:48
Flatten a Multilevel Doubly Linked List
'''
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
@doyedele1
doyedele1 / two-city-scheduling.py
Created March 14, 2022 08:50
Two City Scheduling
'''
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
@doyedele1
doyedele1 / candy-crush.py
Created March 14, 2022 08:53
Candy Crush
'''
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.
@doyedele1
doyedele1 / remove-all-adjacent-duplicates.py
Last active March 14, 2022 09:00
Remove All Adjacent Duplicates in String II
'''
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)
'''
@doyedele1
doyedele1 / invalid-transactions.py
Created March 14, 2022 09:01
Invalid Transactions
'''
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)
'''