This repository contains Java and Python solutions to selected LeetCode problems, each with a short explanation, approach, and complexity analysis.
# | Title | Level | Java Solution File | Python Solution File | Approaches |
---|---|---|---|---|---|
1 | Two Sum | 🟢 Easy | 0001_TwoSum.java | HashMap (2 versions) | |
3 | Longest Substring Without Repeating Chars | 🟡 Medium | 0003_LongestSubstringWithoutRepeating.java | Sliding Window, HashMap, HashSet | |
15 | 3Sum | 🟡 Medium | 0015_3Sum.java | 0015_3Sum.py | Sorting + Two Pointers |
20 | Valid Parentheses | 🟢 Easy | 0020_ValidParentheses.java | Stack, HashMap | |
21 | Merge Two Sorted Lists | 🟢 Easy | 0021_MergeTwoSortedLists.java | 0021_MergeTwoSortedLists.py | Linked List, Two Pointers, Dummy Head |
23 | Merge k Sorted Lists | 🔴 Hard | 0023_MergeKSortedLists.java | 0023_MergeKSortedLists.py | Min Heap (PriorityQueue) |
33 | Search in Rotated Sorted Array | 🟡 Medium | 0033_SearchInRotatedSortedArray.java | 0033_SearchInRotatedSortedArray.py | Binary Search (check sorted half) |
42 | Trapping Rain Water | 🔴 Hard | 0042_TrappingRainWater.java | Two Pointers | |
49 | Group Anagrams | 🟡 Medium | 0049_GroupAnagrams.java | HashMap, Char Sorting | |
91 | Decode Ways | 🟡 Medium | 0091_DecodeWays.java | 0091_DecodeWays.py | Dynamic Programming (DP Array + Rolling) |
121 | Best Time to Buy and Sell Stock | 🟢 Easy | 0121_BestTimeToBuyAndSellStock.java | 0121_BestTimeToBuyAndSellStock.py | Single Pass, Min Price Tracking |
128 | Longest Consecutive Sequence | 🟡 Medium | 0128_LongestConsecutiveSequence.java | 0128_LongestConsecutiveSequence.py | HashSet, Start-of-Run Check |
200 | Number of Islands | 🟡 Medium | 0200_NumberOfIslands.java | DFS (recursive) | |
242 | Valid Anagram | 🟢 Easy | 0242_ValidAnagram.java | 0242_ValidAnagram.py | Count Array (O(n)), HashMap alternative |
347 | Top K Frequent Elements | 🟡 Medium | 0347_TopKFrequentElements.java | HashMap, Min Heap | |
1004 | Max Consecutive Ones III | 🟡 Medium | 1004_MaxConsecutiveOnesIII.java | Sliding Window |
Approach: One-pass HashMap (recommended) or Pre-insert First Element
Complexity:
- ⏱ Time: O(n)
- 📦 Space: O(n)
Approach: Sliding Window, HashMap, HashSet
Complexity:
- ⏱ Time: O(n)
- 📦 Space: O(min(n, k))
Approach: Sorting + Two Pointers
Complexity:
- ⏱ Time: O(n²) (sorting O(n log n) + two-pointer per anchor)
- 📦 Space: O(1) extra (ignoring output)
Approach: Stack, HashMap (matching pairs)
Complexity:
- ⏱ Time: O(n)
- 📦 Space: O(n)
Approach: Linked List, Two Pointers + Dummy Head
Complexity:
- ⏱ Time: O(n + m) — each node is processed once.
- 📦 Space: O(1) — merge is done in-place using existing nodes.
Approach: Min-Heap (PriorityQueue)
- Push all non-null list heads to a min-heap.
- Pop smallest element, append it to result, and push its next (if exists).
Complexity: - ⏱ Time: O(N log k), where N = total nodes and k = number of lists
- 📦 Space: O(k) for the heap
Approach: Binary Search with “which half is sorted” check
- At each step, one half (left or right) is guaranteed sorted.
- Decide if target lies in the sorted half → shrink search range accordingly.
Complexity: - ⏱ Time: O(log n)
- 📦 Space: O(1)
Approach: Two Pointers with running left/right max
Complexity:
- ⏱ Time: O(n)
- 📦 Space: O(1)
Approach: HashMap keyed by sorted-character signature
Complexity:
- ⏱ Time: O(n × k log k)
- 📦 Space: O(n × k)
Approach: Dynamic Programming
Complexity:
- ⏱ Time: O(n)
- 📦 Space: O(n) or O(1) with rolling variables
Approach: Single Pass, Min Price Tracking
Complexity:
- ⏱ Time: O(n)
- 📦 Space: O(1)
Approach: HashSet, Start-of-Run Check
Complexity:
- ⏱ Time: O(n) average (each number visited at most twice)
- 📦 Space: O(n) for the set
Approach: DFS (recursive) to mark visited land
Complexity:
- ⏱ Time: O(rows × cols)
- 📦 Space: O(rows × cols) recursion stack (worst-case)
Approach: Count Array (size 26) or HashMap
Complexity:
- ⏱ Time: O(n)
- 📦 Space: O(1) for fixed alphabet (or O(k) for general Unicode)
Approach: HashMap, Min Heap
Complexity:
- ⏱ Time: O(n log k)
- 📦 Space: O(n)
Approach: Sliding Window
Complexity:
- ⏱ Time: O(n)
- 📦 Space: O(1)