Skip to content

Instantly share code, notes, and snippets.

View danielcodes's full-sized avatar

Daniel Chia danielcodes

View GitHub Profile
// problem 3
const products = [
{name: "Stainless 10\"", weight: 1.5, price: 65, size: 10, id: 12},
{name: "Stainless 12\"", weight: 1.8, price: 85, size: 12, id: 13},
{name: "Carbon Steel 10\"", weight: 1.5, price: 75, size: 10, id: 23},
{name: "Carbon Steel 12\"", weight: 1.95, price: 75, size: 12, id: 24},
{name: "Nonstick 10\"", weight: 2.0, price: 70, size: 10, id: 3},
{name: "Nonstick 12\"", weight: 2.0, price: 70, size: 12, id: 4}
];
// problem 2
function lookAndSay(seq) {
let ans = [];
let start = 0;
while (start < seq.length) {
curr = seq[start];
step = start;
// problem 1
function palindromeCheck(s) {
// remove non alphanumeric chars
let formatted = s.replace(/[^a-z0-9]/gi,'').toLowerCase();
let start = 0;
let end = formatted.length-1;
while (start < end) {
@danielcodes
danielcodes / solve.py
Created April 6, 2020 16:10
269. Alien dictionary
def solve(words):
def build_graph(words, indegree, adj):
# initialize map with all possible nodes
for x in range(len(words)):
for y in range(len(words[x])):
if y not in adj:
adj[words[x][y]] = set()
for i in range(1, len(words)):
@danielcodes
danielcodes / solve.py
Created April 6, 2020 13:47
271. Encode and decode strings
def encode(words):
ans = ''
# count-word is the encoding
for i in range(len(words)):
ans += str(len(words[i])) + '-' + words[i]
return ans
@danielcodes
danielcodes / solve.py
Created April 6, 2020 12:38
261. Graph vaild tree
from collections import defaultdict
def solve(n, edges):
def has_cycle(node, parent, adj, visited):
visited.add(node)
for n in adj[node]:
# node has been visited and not the parent
if n in visited and n != parent:
@danielcodes
danielcodes / solve.py
Created April 2, 2020 03:13
Heapify and heapsort
# implement a priority queue
# specifically heapify part
def heapify(A, i, size):
# given a node index and upper bound
# sinks value down until heap invariant is met
while i < size:
index = i
left = 2*i + 1
@danielcodes
danielcodes / solve.py
Last active March 28, 2020 17:49
323. Number of Connected Components in an Undirected Graph
from collections import deque, defaultdict
# 323. Number of Connected Components in an Undirected Graph
def solve(n, edges):
def bfs(i, adj, visited):
q = deque()
q.append(i)
@danielcodes
danielcodes / solve.py
Last active March 27, 2020 20:45
253. Meeting rooms ii
from heapq import heapify, heappush, heappop
# 253. Meeting rooms ii
def solve(ints):
if not ints:
return True
ints.sort(key=lambda x: x[0])
@danielcodes
danielcodes / solve.py
Created March 27, 2020 18:54
252. Meeting rooms
# 252. Meeting rooms
def solve(ints):
# alternate approach is to check current with next
# in this case iteration stops on the second to last interval
if not ints:
return True