Skip to content

Instantly share code, notes, and snippets.

View Worldofwbdesign's full-sized avatar

Worldofwbdesign Worldofwbdesign

View GitHub Profile
@Worldofwbdesign
Worldofwbdesign / materializedView.sql
Created April 23, 2021 20:12
Postgres materialized view creating
CREATE MATERIALIZED VIEW weekly_likes AS (
SELECT
date_trunc('week', COALESCE(posts.created_at, comments.created_at)) AS week,
COUNT (posts.id) AS posts_likes_count,
COUNT (comments.id) AS comments_likes_count
FROM likes
LEFT JOIN posts on likes.post_id = posts.id
LEFT JOIN comments ON likes.comment_id = comments.id
GROUP BY week
ORDER BY week
@Worldofwbdesign
Worldofwbdesign / recursive.sql
Created April 23, 2021 18:55
Postgres recursive query
WITH RECURSIVE suggestions(leader_id, follower_id, depth) AS (
SELECT leader_id, follower_id, 1 AS depth
FROM followers
WHERE follower_id = 1000
UNION
SELECT followers.leader_id, followers.follower_id, depth + 1
FROM followers
JOIN suggestions ON suggestions.leader_id = followers.follower_id
WHERE depth < 3
)
const quickSort = arr => {
if (arr.length < 2) return arr
const pivot = arr[0]
const left = []
const right = []
for (let i = 1; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i])
function solution(A) {
let result = 0
let leader
const hash = {}
let stackSize = 0
let candidate
A.forEach((num, index) => {
if (!hash[num]) {
hash[num] = { index, count: 0 }
function solution(A, B) {
let dead = 0
const stack = []
A.forEach((weight, index) => {
if (B[index] === 0) {
while (stack.length > 0) {
dead += 1
if (stack[stack.length - 1] > weight) {
break
@Worldofwbdesign
Worldofwbdesign / prefixSum.js
Created March 14, 2021 09:11
Prefix sum algorithm
const impacts = {
A: 1,
C: 2,
G: 3,
T: 4
}
const prefixSum = arr => {
const result = []
@Worldofwbdesign
Worldofwbdesign / knapsack.js
Created April 13, 2020 20:35
Algorithm. Knapsack (recursive)
const knapsackCount = (w, v, capacity) => {
w.unshift(0)
v.unshift(0)
const memo = w.map(item => [])
const knapsackRec = (n, C) => {
if (memo[n][C] !== undefined) return memo[n][C]
if (n === 0 || C === 0) return 0
let result
@Worldofwbdesign
Worldofwbdesign / editString.js
Created April 11, 2020 08:36
Algorithm. Levenshtein's distance (edit strings distance)
const editDistance = (s1, s2) => {
if (s1.length === 0) return s2.length
if (s2.length === 0) return s1.length
const matrix = []
// Fill first row and column of matrix as it will take {i} changes to transform empty string to every string
for (let i = 0; i <= s1.length; i++) {
matrix[i] = [i]
}
@Worldofwbdesign
Worldofwbdesign / transformToOne.js
Created April 1, 2020 20:34
Algorithm. Transform to 1. Dynamic programming
const minOneBottomUp = n => {
const cache = []
cache[1] = 0
for (let i = 2; i <= n; i++) {
let b = 1000, c = 1000
const a = 1 + cache[i - 1]
if (i % 2 === 0) {
@Worldofwbdesign
Worldofwbdesign / fib.js
Created March 27, 2020 11:09
Algorithm. Fibonacci
const fib = n => {
if (n < 1) throw new Error('Pass in integer greater then 0!')
let result
if (n === 1 || n === 2) {
result = 1
} else {
result = fib(n - 1) + fib(n - 2)
}