Skip to content

Instantly share code, notes, and snippets.

View rakibulalam's full-sized avatar

rakibulalam rakibulalam

View GitHub Profile
@rakibulalam
rakibulalam / BalancedBrackets.js
Last active May 5, 2020 22:53
Balanced Brackets Hacker Rank
/*stack solutions*/
function isBalanced(s) {
const brackets={'{':'}','(':')','[':']'};
const arr=s.split('');
const stack=[];
/* if the length is odd*/
if(arr.length%2!==0) return 'NO';
for(let i=0; i<arr.length; i++)
{
@rakibulalam
rakibulalam / PriorityQueue.js
Last active May 5, 2020 10:53
Descending based Priority Queue with less complexity
/**
* Descending based Priority Queue with less complexity
**/
class PriorityQueue {
constructor() {
this.queue = [];
}
enqueue(element) {
this.queue=[...this.queue, element].sort(([ v, p ], [ v1, p1 ]) => p1 - p);
@rakibulalam
rakibulalam / CountingValleys.js
Last active May 5, 2020 22:56
Counting Valleys Hacker Rank
function countingValleys(n, s) {
let level=0,vally=0;
for(let i=0; i<=n; i++)
{
if(s[i]==='D')
{
--level
}else if(s[i]==='U'){
if(++level===0) vally++;
}
@rakibulalam
rakibulalam / SockMerchant.js
Last active May 5, 2020 22:56
Sock Merchant Hacker Rank
function sockMerchant(n, ar) {
const pairs={};
return ar.reduce((memo,value)=>{
if(pairs.hasOwnProperty(value))
{
pairs[value]=pairs[value]+1;
memo+=pairs[value]%2===0?1:0
}else{
pairs[value]=1
}
@rakibulalam
rakibulalam / MaxMin.js
Last active May 5, 2020 22:56
Max Min Hacker Rank
function maxMin(k, arr) {
const sortArray=arr.sort((a,b)=>a-b);
const lengthArray=sortArray.length;
let unfairValue=Infinity;
for(let i=0; i<=lengthArray-k; i++)
{
const groupArray=sortArray.slice(i,i+k);
const value=groupArray[k-1]-groupArray[0];
unfairValue=unfairValue<value?unfairValue:value;
@rakibulalam
rakibulalam / GreedyFlorist.js
Last active May 5, 2020 22:55
Greedy Florist Hacker Rank
function getMinimumCost(k, c) {
return c.sort((a,b)=>b-a).reduce((memo,value,key)=>{
const i=Math.floor(key/k);
memo+=(i+1)*value;
return memo;
},0)
}
@rakibulalam
rakibulalam / LuckBalance.js
Last active May 5, 2020 22:55
Luck Balance Hacker Rank
function luckBalance(k, contests) {
return contests.sort(([a,x],[b,y])=>y-x || b-a)
.reduce((memo,[l,t],key)=>{
memo += t===1 && key<k? l: t===1 && key>=k?-l:l
return memo;
},0)
}
@rakibulalam
rakibulalam / httpBuildQuery.js
Created January 11, 2019 19:26
httpBuildQuery
static httpBuildQuery(data = {}) {
return Object.keys(data)
.map((k) => {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
})
.join('&');
}
@rakibulalam
rakibulalam / ClientOnly.js
Created September 23, 2018 09:23
React.js component render for Client Only
import React from 'react';
import ExecutionEnvironment from 'exenv';
/*
* Author: Md. Rakibulalam
* Dependencies : npm install --save exenv
* If you want to exclude render dom releated components from server side render
*/
export default (WrappedComponent) => {
const hocComponent = ({ ...props }) => {
@rakibulalam
rakibulalam / fibo.js
Created October 27, 2017 08:32
fibo.js
for (var sum = 0, i = 0, j = 1, k = 2; 4e6 > j + k; i++) i = j + k, j = k, k = i, sum += 0 == i % 2 ? i : 0;
result: '4613730'