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
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 |
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
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 | |
) |
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
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]) |
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
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 } |
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
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 |
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
const impacts = { | |
A: 1, | |
C: 2, | |
G: 3, | |
T: 4 | |
} | |
const prefixSum = arr => { | |
const result = [] |
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
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 |
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
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] | |
} |
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
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) { |
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
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) | |
} |
NewerOlder