Skip to content

Instantly share code, notes, and snippets.

function bfs(graph, root) {
// Distance object returned
var nodesLen = {};
graph.forEach((_, i) => {
nodesLen[i] = Infinity;
});
nodesLen[root] = 0;
const v = {[root]: true};
const dfs = (graph, root) => {
const v = {[root]: true};
const r = [root];
const s = [graph[root]];
while(s.length) {
let cur = s.pop();
cur.forEach((c) => {
if (!v[c]) {
// check if array is sorted
function isSorted(arr) {
var check = i =>
i == arr.length - 1 ? true : arr[i] > arr[i + 1] ? false : check(i + 1);
return check(0);
}
// generate a randomly filled array
var array = new Array();
(function createArray(size = 5) {
array.push(+(Math.random() * 100).toFixed(0));
var MaxHeap = function() {
// change code below this line
this.heap = [undefined];
this.insert = (ele) => {
var index = this.heap.length;
var arr = [...this.heap];
arr.push(ele);
while (ele > arr[Math.floor(index / 2)]) {
arr[index] = arr[Math.floor(index / 2)];
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
@vitalii-komenda
vitalii-komenda / sherlock-and-anagrams.js
Created September 2, 2020 08:54
sherlock-and-anagrams
// https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem?isFullScreen=true
const charIndex = (c) => Math.abs(97 - c.charCodeAt(0));
const newSignature = () => {
const ALPHABET = "abcdefghijklmnopqrstuvwxyz";
return ALPHABET.split('').map(_ => 0);
}
const h = (arr) => arr.join('-');
@vitalii-komenda
vitalii-komenda / big-int.js
Created September 2, 2020 06:55
big int modulo
function modulo (n, p, m){
var result = 1;
while(p--) {
result = (result * n) % m;
}
return result;
}
console.log(modulo(7, 16971, 25777));
@vitalii-komenda
vitalii-komenda / s.js
Last active August 26, 2020 06:55
Google's Foo.bar chess Knight's Shortest Path Chess Puzzle Solution (Don't Get Volunteered!)
# Quiz : Don't Get Volunteered!
# ======================
# As a henchman on Commander Lambda's space station, you're expected to be resourceful, smart, and a quick thinker. It's not easy building a doomsday device and capturing bunnies at the same time, after all! In order to make sure that everyone working for her is sufficiently quick-witted, Commander Lambda has installed new flooring outside the henchman dormitories. It looks like a chessboard, and every morning and evening you have to solve a new movement puzzle in order to cross the floor. That would be fine if you got to be the rook or the queen, but instead, you have to be the knight. Worse, if you take too much time solving the puzzle, you get "volunteered" as a test subject for the LAMBCHOP doomsday device!
# To help yourself get to and from your bunk every day, write a function called answer(src, dest) which takes in two parameters: the source square, on which you start, and the destination square, which is where you need to land to solve the puz
matrix = Array.from({length: 8}, (_, row) => {
return Array(8).fill(0).map((el, index) => index + (row * 8));
});
cs = (arr) => {
const index = [];
const places = Array(arr.length).fill(0);
for(let i=0; i<arr.length; i++) index[arr[i]] = index[arr[i]] + 1 || 1;
for(let i=0; i<index.length-1; i++) index[i+1] = (index[i] || 0) + (index[i+1] || 0);
for(let i=0; i<arr.length; i++) {
places[index[ arr[i] ] - 1] = arr[i];