This file contains 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
// 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} | |
]; |
This file contains 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
// problem 2 | |
function lookAndSay(seq) { | |
let ans = []; | |
let start = 0; | |
while (start < seq.length) { | |
curr = seq[start]; | |
step = start; |
This file contains 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
// 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) { |
This file contains 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
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)): |
This file contains 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
def encode(words): | |
ans = '' | |
# count-word is the encoding | |
for i in range(len(words)): | |
ans += str(len(words[i])) + '-' + words[i] | |
return ans | |
This file contains 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
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: |
This file contains 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
# 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 |
This file contains 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
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) |
This file contains 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
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]) |
This file contains 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
# 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 | |
NewerOlder