Skip to content

Instantly share code, notes, and snippets.

View codecakes's full-sized avatar
💭
I may be slow to respond.

codecakes codecakes

💭
I may be slow to respond.
View GitHub Profile
@codecakes
codecakes / object_instantiation.js
Created November 13, 2024 09:35
People have forgotten classes object instantiation in JS
Hi = (name) => {
x = Object.create(Hi.prototype);
x.name = name
return x;
}
Hi.prototype = {
name: null,
greet: function () {
return `Hello ${this.name}`;
@codecakes
codecakes / word_break_2.py
Created August 14, 2024 21:12
Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word.
"""
Input
s =
"pineapplepenapple"
wordDict =
["apple","pen","applepen","pine","pineapple"]
"""
# This works
@codecakes
codecakes / Top interview leetcode.md
Created August 6, 2024 20:52
Top interview leetcode

Here are the solutions for the remaining problems on the list:


Reverse Linked List

Approach: Iteratively reverse the pointers of the linked list nodes.

@codecakes
codecakes / minimum_output_triangle.py
Created August 6, 2024 20:26
Leetcode 120. Triangle
# top-down: Larger Number TLE
class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
if len(triangle) == 1:
return triangle[0][0]
res = tuple()
level = 0
curr_list = triangle[level]
next_level = level + 1
@codecakes
codecakes / 4sum.py
Created July 29, 2024 20:53
Foursum
# This is one way
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
nums.sort()
result = []
for idx, num in enumerate(nums):
for res3list in self.threeSum(nums[idx+1:], target-num):
if [num] + res3list not in result:
result += [[num] + res3list]
return result
@codecakes
codecakes / remove_duplicates_sorted_arr.py
Created July 22, 2024 10:19
Remove Duplicates from Sorted Array in place
# naive solution
class Solution:
POS_NUM = 1000
def removeDuplicates(self, nums: List[int]) -> int:
start = 0
n = len(nums)
end = n - 1
for start in iter(lambda: start, end):
if start > end:
break
@codecakes
codecakes / natorize.py
Created July 4, 2024 06:41
speak in nato phonetic tongue
NATO_BAG = {
'A': 'Alpha',
'B': 'Bravo',
'C': 'Charlie',
'D': 'Delta',
'E': 'Echo',
'F': 'Foxtrot',
'G': 'Golf',
'H': 'Hotel',
'I': 'India',
@codecakes
codecakes / level_order_traversal.ex
Created June 27, 2024 13:32
level order traversal
# Definition for a binary tree node.
#
# defmodule TreeNode do
# @type t :: %__MODULE__{
# val: integer,
# left: TreeNode.t() | nil,
# right: TreeNode.t() | nil
# }
# defstruct val: 0, left: nil, right: nil
# end
@codecakes
codecakes / subset_combination.py
Created June 9, 2024 10:47
array nums of unique integers, return all possible combination of subsets of nums.
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
def do_generate(result, slate, subset):
result += [slate]
for idx, num in enumerate(subset):
new_subset = subset[idx+1:]
do_generate(result, slate + [num], new_subset)
return result
return do_generate([], [], nums)
@codecakes
codecakes / recursive_combination.py
Created June 8, 2024 10:06
N choose K combinations
def find_combinations(n, k):
"""
Args:
n(int32)
k(int32)
Returns:
list_list_int32
"""
array = list(range(1,n+1))
result = set()