Skip to content

Instantly share code, notes, and snippets.

View spiterman's full-sized avatar
🏠
Working from home

Sergey Piterman spiterman

🏠
Working from home
View GitHub Profile
// Linear Space
function houseRobber1(houses) {
let max_gold = []
for(let i = 0; i < houses.length; i++) {
let current = houses[i];
let prevMax = max_gold[i - 1] || 0;
let twoBackMax = max_gold[i - 2] || 0;
max_gold.push(Math.max(current + twoBackMax, prevMax));
function nThFibonacci(n) {
if(n <= 1) return 1
return nThFibonacci(n - 1) + nThFibonacci(n - 2)
}
function nThFibonacciTable(n) {
let table = [1, 1]
for (let i = 2; i <= n; i++) {
let nextFib = table[i - 1] + table[i - 2]
table.push(nextFib)
}
return table[n]
}
let nthFib = (n) => n <= 1 ? 1 : nthFib(n - 1) + nthFib(n - 2);
function PowerSet(str) {
let result = []
function constructSubSet(subSet, index) {
if(index >= str.length) {
result.push(subSet)
return
}
constructSubSet(subSet, index + 1)
constructSubSet(subSet + str[index], index + 1)
function addNode(graph, nodeToAdd) {
if(graph[nodeToAdd] === undefined) {
graph[nodeToAdd] = new Set()
}
return graph
}
function addConnection(graph, origin, destination) {
addNode(graph, origin)