Skip to content

Instantly share code, notes, and snippets.

View wataruoguchi's full-sized avatar
🦥
Curiosity Driven

Wataru Oguchi wataruoguchi

🦥
Curiosity Driven
View GitHub Profile
// 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;
}
}
// 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);
@wataruoguchi
wataruoguchi / array-rotation.js
Created July 30, 2019 03:04
Program for array rotation
// 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);
@wataruoguchi
wataruoguchi / add-int-in-circular-linked-list.js
Last active July 29, 2019 06:10
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)
// 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 = [];
// 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 = [];
// 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;
@wataruoguchi
wataruoguchi / detect-and-remove-loop-in-a-linked-list.js
Created July 28, 2019 06:05
Detect and Remove Loop in a Linked List
// 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;
}
@wataruoguchi
wataruoguchi / AddTwoNumsLinkedList.js
Last active July 28, 2019 05:37
Add Two Numbers Represented by Linked Lists
// 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;
@wataruoguchi
wataruoguchi / LinkedList.js
Last active July 27, 2019 21:28
Linked List
// 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)
// 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) => {