This file contains hidden or 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
// https://practice.geeksforgeeks.org/problems/stickler-theif/0 | |
function sticklerTheif(houses) { | |
function getAmount(condition) { | |
return (acc, house, idx) => { | |
if (condition(idx)) { | |
acc += house; | |
} | |
return acc; | |
} | |
} |
This file contains hidden or 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
// https://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/ | |
// https://practice.geeksforgeeks.org/problems/k-largest-elements/0 | |
function kLargest(arr, n, k) { | |
return arr.sort((a, b) => b - a).splice(0, k); | |
} | |
const arrEx1 = [12,5,787,1,23]; | |
const resEx1 = kLargest(arrEx1, arrEx1.length, 2); | |
console.log(resEx1.join(',') === '787,23'); | |
const arrEx2 = [1,23,12,9,30,2,50]; | |
const resEx2 = kLargest(arrEx2, arrEx2.length, 3); |
This file contains hidden or 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
// Program for array rotation | |
// https://www.geeksforgeeks.org/array-rotation/ | |
function rotate(arr, d, n) { | |
// Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. | |
const stash = arr.splice(0, d); | |
return arr.concat(stash); | |
} | |
const arr = [1,2,3,4,5,6,7]; | |
const res = rotate(arr, 2, arr.length); |
This file contains hidden or 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
// Given a sorted circularly linked list of Nodes that store integers and a new Node, Insert the new Node into the correct position. (Duplicates allowed) | |
class Node { | |
constructor(val, next) { | |
this.val = val; | |
this.next = next; | |
} | |
} | |
function printList(head) { | |
let tmp = head; | |
let arr = []; |
This file contains hidden or 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
// https://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ | |
class Node { | |
constructor(val, next) { | |
this.val = val; | |
this.next = next; | |
} | |
} | |
function printList(head) { | |
let tmp = head; | |
let arr = []; |
This file contains hidden or 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
// https://www.geeksforgeeks.org/flattening-a-linked-list/ | |
// https://www.youtube.com/watch?v=PSKZJDtitZw | |
function printList(root) { | |
let tmp = root; | |
let arr = []; | |
while (tmp) { | |
arr.push(tmp.val); | |
tmp = tmp.down; | |
} | |
return arr; |
This file contains hidden or 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
// https://www.geeksforgeeks.org/detect-and-remove-loop-in-a-linked-list/ | |
function printList(root) { | |
let tmp = root; | |
let arr = []; | |
while (tmp) { | |
arr.push(tmp.val); | |
tmp = tmp.next; | |
} | |
return arr; | |
} |
This file contains hidden or 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
// https://www.geeksforgeeks.org/add-two-numbers-represented-by-linked-lists-set-3/ | |
// Same - https://www.geeksforgeeks.org/add-two-numbers-represented-by-linked-lists/ | |
function printList(root) { | |
let tmp = root; | |
let arr = []; | |
while (tmp) { | |
arr.push(tmp.val); | |
tmp = tmp.next; | |
} | |
return arr; |
This file contains hidden or 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
// https://www.geeksforgeeks.org/linked-list-set-1-introduction/ | |
class Node { | |
constructor(val, next) { | |
this.val = val; | |
this.next = next; | |
} | |
} | |
function printList(list) { | |
// O(N) |
This file contains hidden or 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
// Design an algorithm to compute a square root that handles perfect and non-perfect squares | |
function getSqrt(square) { | |
return Math.round(Math.pow(square, 1/2) * 100) / 100; | |
} | |
console.log('getSqrt', getSqrt(25) === 5 && getSqrt(10) === 3.16); | |
// Given a sequence A of size N, find the length of the longest increasing subsequence from a given sequence. | |
function findLongsetIncreasing(arr) { | |
arr.unshift(0); | |
const res = arr.reduce((acc, mem, idx, arr) => { |