Skip to content

Commit 23e2943

Browse files
initialized repo
1 parent 5954aa4 commit 23e2943

File tree

18 files changed

+460
-0
lines changed

18 files changed

+460
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Generate README
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
generate-readme:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
# Checkout the repo
14+
- name: Checkout repository
15+
uses: actions/checkout@v3
16+
17+
# Set up Node.js
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v3
20+
with:
21+
node-version: '18'
22+
23+
# Run your README generator
24+
- name: Run generateReadme.js
25+
run: node generateReadme.js
26+
27+
# Commit the updated README
28+
- name: Commit changes
29+
run: |
30+
git config --local user.name "github-actions[bot]"
31+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
32+
git add README.md
33+
git diff --quiet || git commit -m "chore: update README.md"
34+
git push
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Detect cycle in directed graph using DFS
2+
function hasCycle(n, edges) {
3+
const graph = Array.from({ length: n }, () => [])
4+
for (const [u, v] of edges) graph[u].push(v)
5+
6+
const visited = new Set()
7+
const inStack = new Set()
8+
9+
function dfs(node) {
10+
if (inStack.has(node)) return true
11+
if (visited.has(node)) return false
12+
13+
visited.add(node)
14+
inStack.add(node)
15+
16+
for (const nei of graph[node]) {
17+
if (dfs(nei)) return true
18+
}
19+
20+
inStack.delete(node)
21+
return false
22+
}
23+
24+
for (let i = 0; i < n; i++) {
25+
if (!visited.has(i) && dfs(i)) return true
26+
}
27+
28+
return false
29+
}

Algorithms/dijkstra.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const MinHeap = require('../data_structures/minHeap')
2+
3+
// Dijkstra's Algorithm
4+
function dijkstra(graph, src) {
5+
const dist = Array(graph.length).fill(Infinity)
6+
dist[src] = 0
7+
8+
const pq = new MinHeap()
9+
pq.push([0, src]) // [dist, node]
10+
11+
while (pq.h.length) {
12+
const [d, node] = pq.pop()
13+
if (d > dist[node]) continue
14+
15+
for (const [nei, w] of graph[node]) {
16+
const nd = d + w
17+
if (nd < dist[nei]) {
18+
dist[nei] = nd
19+
pq.push([nd, nei])
20+
}
21+
}
22+
}
23+
24+
return dist
25+
}

Algorithms/kadane.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Kadane’s Algorithm
2+
function kadane(arr) {
3+
let best = -Infinity
4+
let cur = 0
5+
6+
for (const x of arr) {
7+
cur = Math.max(x, cur + x)
8+
best = Math.max(best, cur)
9+
}
10+
return best
11+
}

Algorithms/topoSort.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Kahn's Algorithm (BFS)
2+
function topoSort(n, edges) {
3+
const graph = Array.from({ length: n }, () => [])
4+
const indegree = Array(n).fill(0)
5+
6+
for (const [u, v] of edges) {
7+
graph[u].push(v)
8+
indegree[v]++
9+
}
10+
11+
const q = []
12+
for (let i = 0; i < n; i++) {
13+
if (indegree[i] === 0) q.push(i)
14+
}
15+
16+
const res = []
17+
18+
while (q.length) {
19+
const node = q.shift()
20+
res.push(node)
21+
22+
for (const nei of graph[node]) {
23+
indegree[nei]--
24+
if (indegree[nei] === 0) q.push(nei)
25+
}
26+
}
27+
28+
return res.length === n ? res : []
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Binary Search on Answer pattern
2+
function minCapacity(weights, days) {
3+
let l = Math.max(...weights),
4+
r = weights.reduce((a, b) => a + b)
5+
6+
const can = (cap) => {
7+
let need = 1,
8+
load = 0
9+
for (const w of weights) {
10+
if (load + w > cap) {
11+
need++
12+
load = 0
13+
}
14+
load += w
15+
}
16+
return need <= days
17+
}
18+
19+
while (l < r) {
20+
const mid = Math.floor((l + r) / 2)
21+
if (can(mid)) r = mid
22+
else l = mid + 1
23+
}
24+
return l
25+
}

DSA patterns/monotonicStack.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Next Greater Element using Monotonic Stack
2+
function nextGreater(arr) {
3+
const res = Array(arr.length).fill(-1)
4+
const stack = [] // indexes
5+
6+
for (let i = 0; i < arr.length; i++) {
7+
while (stack.length && arr[i] > arr[stack[stack.length - 1]]) {
8+
const idx = stack.pop()
9+
res[idx] = arr[i]
10+
}
11+
stack.push(i)
12+
}
13+
14+
return res
15+
}

DSA patterns/prefixSumPattern.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Subarray sum == k using prefix sums
2+
function subarraySum(nums, k) {
3+
let count = 0
4+
const map = { 0: 1 }
5+
let sum = 0
6+
7+
for (const n of nums) {
8+
sum += n
9+
if (map[sum - k]) count += map[sum - k]
10+
map[sum] = (map[sum] || 0) + 1
11+
}
12+
13+
return count
14+
}
15+
16+
module.exports = subarraySum

DSA patterns/twoPointers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Two pointers for sorted array pair sum
2+
function pairSumSorted(arr, target) {
3+
let l = 0,
4+
r = arr.length - 1
5+
6+
while (l < r) {
7+
const sum = arr[l] + arr[r]
8+
if (sum === target) return [l, r]
9+
if (sum < target) l++
10+
else r--
11+
}
12+
13+
return -1
14+
}
15+
16+
module.exports = pairSumSorted

Data Structures/graph/trie.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class TrieNode {
2+
constructor() {
3+
this.children = {}
4+
this.isEnd = false
5+
}
6+
}
7+
8+
class Trie {
9+
constructor() {
10+
this.root = new TrieNode()
11+
}
12+
13+
insert(word) {
14+
let cur = this.root
15+
for (const ch of word) {
16+
if (!cur.children[ch]) cur.children[ch] = new TrieNode()
17+
cur = cur.children[ch]
18+
}
19+
cur.isEnd = true
20+
}
21+
22+
search(word) {
23+
let cur = this.root
24+
for (const ch of word) {
25+
if (!cur.children[ch]) return false
26+
cur = cur.children[ch]
27+
}
28+
return cur.isEnd
29+
}
30+
31+
startsWith(prefix) {
32+
let cur = this.root
33+
for (const ch of prefix) {
34+
if (!cur.children[ch]) return false
35+
cur = cur.children[ch]
36+
}
37+
return true
38+
}
39+
}

0 commit comments

Comments
 (0)