Skip to content

Latest commit

Β 

History

90 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LeetCode-DSA subtitle



GitHub Kaggle LinkedIn Language Notebook


πŸ“Š Live LeetCode Stats

Pulled live from my LeetCode profile β€” updates automatically every time this page loads. No manual edits, no fake numbers.

Problems Solved Easy Medium Hard


LeetCode Stats Card


🧠 What Is This Repo?

A structured daily grind journal β€” not a solution dump.

Every notebook follows a consistent format so each session actually teaches a pattern, not just a solution.

πŸ“Œ Problem Statement
πŸ’‘ Intuition & Thought Process
🐒 Brute Force 
πŸš€ Optimized
πŸ“Š Complexity Breakdown

Written in plain readable Python β€” Kaggle notebook style. Zero over-engineering.


πŸ“‚ Repository Structure

# Notebook
09 Leetcode_09.ipynb
50 Leetcode_50.ipynb
231 Leetcode_231.ipynb
326 Leetcode_326.ipynb
342 Leetcode_342.ipynb
509 Leetcode_509.ipynb
1137 LeetCode_1137.ipynb
1281 Leetcode_1281.ipynb
1431 Leetcode_1431.ipynb
2520 Leetcode_2520.ipynb
- Recursion_Practice.ipynb
- Time_&_Space_Complexity.ipynb

πŸ—ΊοΈ Topics Covered

# Topic Core Patterns Status
01 Arrays & Hashing HashMap, frequency count, prefix sum βœ…
02 Two Pointers Shrink window, fast-slow, 3Sum βœ…
03 Sliding Window Fixed & variable window βœ…
04 Stack / Monotonic LIFO, next greater/smaller βœ…
05 Binary Search Halving, rotated arrays βœ…
06 Linked Lists Floyd's cycle, reversal, merge βœ…
07 Trees DFS, BFS, recursion, LCA βœ…
08 Graphs Union-Find, BFS/DFS traversal βœ…
09 Dynamic Programming Memoization, tabulation πŸ”„
β€” Backtracking Permutations, subsets πŸ“‹
β€” Heaps & Priority Queue Top-K, merge K sorted πŸ“‹
β€” Tries Prefix tree, word search πŸ“‹

βœ… Complete Β Β·Β  πŸ”„ In Progress Β Β·Β  πŸ“‹ Upcoming


πŸ’‘ Pattern Cheatsheets

πŸ” Two Pointers β€” O(n) time Β· O(1) space
left, right = 0, len(arr) - 1
while left < right:
    s = arr[left] + arr[right]
    if s == target:   return [left, right]
    elif s < target:  left += 1
    else:             right -= 1

When to use: sorted array, pair sum, palindrome check, 3Sum, container with most water.

πŸͺŸ Sliding Window β€” O(n) time Β· O(k) space
left, best = 0, 0
window = {}
for right, ch in enumerate(s):
    window[ch] = window.get(ch, 0) + 1
    while len(window) > k:        # shrink condition
        window[s[left]] -= 1
        if window[s[left]] == 0:
            del window[s[left]]
        left += 1
    best = max(best, right - left + 1)
return best

When to use: max/min subarray, longest substring with constraint, contains duplicate in window.

πŸ“š Monotonic Stack β€” O(n) time Β· O(n) space
stack, result = [], [-1] * len(arr)
for i, num in enumerate(arr):
    while stack and arr[stack[-1]] < num:
        result[stack.pop()] = num
    stack.append(i)
return result

When to use: next greater element, daily temperatures, histogram max area, bracket matching.

πŸ” Binary Search β€” O(log n) time Β· O(1) space
lo, hi = 0, len(arr) - 1
while lo <= hi:
    mid = lo + (hi - lo) // 2
    if arr[mid] == target:  return mid
    elif arr[mid] < target: lo = mid + 1
    else:                   hi = mid - 1
return -1

When to use: sorted array, rotated sorted array, search on answer (min/max valid value).

🌲 DFS on Tree β€” O(n) time Β· O(h) space
def dfs(node):
    if not node:
        return 0
    left  = dfs(node.left)
    right = dfs(node.right)
    return 1 + max(left, right)   # max depth example

When to use: height/depth, path sum, LCA, subtree problems, serialization.

πŸ”— BFS Level-Order β€” O(n) time Β· O(n) space
from collections import deque
q, result = deque([root]), []
while q:
    level = []
    for _ in range(len(q)):
        node = q.popleft()
        level.append(node.val)
        if node.left:  q.append(node.left)
        if node.right: q.append(node.right)
    result.append(level)
return result

When to use: shortest path, level-by-level traversal, multi-source BFS, word ladder.

πŸ”— Union-Find (DSU) β€” O(Ξ±Β·n) β‰ˆ O(1) per op
parent = list(range(n))
rank   = [0] * n

def find(x):
    if parent[x] != x:
        parent[x] = find(parent[x])   # path compression
    return parent[x]

def union(x, y):
    px, py = find(x), find(y)
    if px == py: return False
    if rank[px] < rank[py]: px, py = py, px
    parent[py] = px
    if rank[px] == rank[py]: rank[px] += 1
    return True

When to use: number of islands, redundant connection, accounts merge, connected components.

πŸ’° Dynamic Programming (bottom-up)
# Climbing stairs template
dp = [0] * (n + 1)
dp[1] = 1
dp[2] = 2
for i in range(3, n + 1):
    dp[i] = dp[i-1] + dp[i-2]
return dp[n]

# 0/1 Knapsack template
dp = [[0] * (W + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    for w in range(W + 1):
        dp[i][w] = dp[i-1][w]
        if weights[i-1] <= w:
            dp[i][w] = max(dp[i][w],
                           dp[i-1][w - weights[i-1]] + values[i-1])
return dp[n][W]

When to use: overlapping subproblems, optimal substructure, count ways, min/max cost.


πŸ† Complexity Reference

Algorithm Time Space Notes
Two Pointers O(n) O(1) Requires sorted input
Sliding Window O(n) O(k) k = window size
Binary Search O(log n) O(1) Sorted / monotonic
BFS / DFS O(V + E) O(V) Graph & tree traversal
Merge Sort O(n log n) O(n) Stable, divide & conquer
Quick Sort O(n log n) avg O(log n) In-place, unstable
Heap Push/Pop O(log n) O(n) Priority queue ops
Union-Find O(Ξ±Β·n) O(n) Near-constant per op
Dijkstra O((V+E) log V) O(V) Non-negative weights
Topological Sort O(V + E) O(V) DAG only

πŸš€ Getting Started

# 1. Clone
git clone https://github.com/Kushagra524/LeetCode-DSA.git
cd LeetCode-DSA

# 2. Install Jupyter
pip install notebook

# 3. Open any notebook
jupyter notebook Leetcode_09.ipynb

Or open directly in VS Code with the Jupyter extension β€” zero extra setup.


🀝 Connect


╔══════════════════════════════════════════════════════════╗
β•‘    "First, solve the problem. Then, write the code."    β•‘
β•‘                        β€” John Johnson                   β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Found it useful? Drop a ⭐ β€” it keeps the grind going.

About

All my Leetcode DSA Questions stacked together under a single repo.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages