Skip to content

Instantly share code, notes, and snippets.

View doyedele1's full-sized avatar

Demilade Oyedele doyedele1

View GitHub Profile
@doyedele1
doyedele1 / decode-string.py
Created March 14, 2022 09:02
Decode String
'''
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
@doyedele1
doyedele1 / lru-cache.py
Created March 14, 2022 09:04
LRU Cache
'''
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
@doyedele1
doyedele1 / number-of-islands.py
Created March 14, 2022 09:09
Number of Islands
'''
Explanation:
DFS
[
[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[1,1,1,1]
]
@doyedele1
doyedele1 / design-browser-history.py
Last active March 14, 2022 09:15
Design Browser History
'''
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
@doyedele1
doyedele1 / solution.py
Created September 7, 2023 13:48
Two City Scheduling
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
costs = sorted(costs, key = lambda x: x[0] - x[1])
res = 0
n = len(costs) // 2
for i in range(n):
res += costs[i][0] + costs[i + n][1]
@doyedele1
doyedele1 / solution.py
Created September 21, 2023 00:59
Decode String
'''
Explanation:
- There is always guaranteed an integer right in front of an opening bracket
54[ab6[cd]]
stack = 5, 4, [, a, b, 6, [, c, d
5, 4, [, a, b, 6
We pop any integer after what we've popped which is 6 --> 6 * cd
5, 4, [, a, b, 6*cd
54, ab, 6*cd
@doyedele1
doyedele1 / solution.py
Created September 26, 2023 23:14
Insert Delete GetRandom O(1)
'''
Explanation:
- We can have two data structures. An array and a hashmap
- With the array, we can easily choose a random number from it
- With the hashmap, we can achieve an insertion and removal of O(1)
hashmap --> key = value, value = index
array --> contains only the value
Insert function
@doyedele1
doyedele1 / solution.py
Created October 25, 2023 22:22
2646. Minimize the Total Price of the Trips
'''
Explanation:
- First, talk about the case where you don't half the prices of non-adjacent prices
- Important inputs
n = number of nodes
edges.length = n - 1
edgeA >= 0, edgeB <= n - 1
tripA >= 0, tripB <= n - 1
price.length = n
@doyedele1
doyedele1 / solution.py
Created October 25, 2023 22:27
797. All Paths From Source to Target
'''
Explanation I: DFS Recursion (Backtracking)
Input: [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]
f(0, 3)
/ \
f(1,3) f(2,3)
| |
f(3,3) f(3,3)
@doyedele1
doyedele1 / solution.py
Created October 25, 2023 22:28
740. Delete and Earn
'''
TC: O(n)
SC: O(n)
'''
from typing import List
class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
arrNums = [0] * (max(nums) + 1)