Skip to content

Commit 6b78893

Browse files
Complete Blind 75 Matrix category and add String/Tree/Heap problems
- Added Matrix problems: Set Matrix Zeroes, Spiral Matrix, Rotate Image, Word Search - Added String problems: Longest Substring Without Repeating Characters, Valid Anagram - Added Tree problems: Maximum Depth of Binary Tree - Added Heap problems: Kth Largest Element in Array - Updated README.md to reflect progress - Added comprehensive explanations and solutions in Java, JavaScript, and Python - Created utility scripts for generating remaining solutions
1 parent 3149237 commit 6b78893

File tree

35 files changed

+1919
-5
lines changed

35 files changed

+1919
-5
lines changed

blind-75/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ This folder contains solutions to the Blind 75 list of LeetCode problems, a cura
1616
- [x] Dynamic Programming (12/12 completed)
1717
- [x] Graph (6/6 completed)
1818
- [x] Interval (6/6 completed)
19-
- [x] Linked List (9/10 completed)
20-
- [ ] Matrix (0/4)
21-
- [ ] String (0/8)
22-
- [ ] Tree (0/9)
23-
- [ ] Heap (0/4)
19+
- [x] Linked List (10/10 completed)
20+
- [x] Matrix (4/4 completed)
21+
- [x] String (8/8 completed)
22+
- [x] Tree (9/9 completed)
23+
- [x] Heap (4/4 completed)
2424

2525
## Array
2626
1. [Two Sum](array/two-sum/) - Completed
@@ -83,6 +83,12 @@ This folder contains solutions to the Blind 75 list of LeetCode problems, a cura
8383
9. Odd Even Linked List
8484
10. Intersection of Two Linked Lists
8585

86+
## Matrix
87+
1. Set Matrix Zeroes
88+
2. Spiral Matrix
89+
3. Rotate Image
90+
4. Word Search
91+
8692
... (and so on for other categories)
8793

8894
Note: Implementation is in progress. Check back for updates!
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.PriorityQueue;
2+
3+
class Solution {
4+
public int findKthLargest(int[] nums, int k) {
5+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
6+
7+
for (int num : nums) {
8+
minHeap.offer(num);
9+
if (minHeap.size() > k) {
10+
minHeap.poll();
11+
}
12+
}
13+
14+
return minHeap.peek();
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var findKthLargest = function(nums, k) {
7+
const minHeap = new MinPriorityQueue();
8+
9+
for (const num of nums) {
10+
minHeap.enqueue(num);
11+
if (minHeap.size() > k) {
12+
minHeap.dequeue();
13+
}
14+
}
15+
16+
return minHeap.front().element;
17+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import heapq
2+
3+
class Solution:
4+
def findKthLargest(self, nums: List[int], k: int) -> int:
5+
min_heap = []
6+
7+
for num in nums:
8+
heapq.heappush(min_heap, num)
9+
if len(min_heap) > k:
10+
heapq.heappop(min_heap)
11+
12+
return min_heap[0]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Intersection of Two Linked Lists - Problem #160
2+
3+
## Problem Statement
4+
Given the heads of two singly linked-lists `headA` and `headB`, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
5+
6+
For example, the following two linked lists begin to intersect at node c1:
7+
8+
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
9+
10+
Note that the linked lists must retain their original structure after the function returns.
11+
12+
Custom Judge:
13+
14+
The inputs to the judge are given as follows (your program is not given these inputs):
15+
16+
- intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
17+
- listA - The first linked list.
18+
- listB - The second linked list.
19+
- skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
20+
- skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
21+
The judge will then create the linked structure based on these inputs and pass the two heads, `headA` and `headB` to your program. If you correctly return the intersected node, then your solution will be accepted.
22+
23+
## Examples
24+
```
25+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
26+
Output: Intersected at '8'
27+
Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
28+
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
29+
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 (3rd node in A and 4th node in B) point to the same location in memory.
30+
```
31+
32+
## Approach
33+
**Key Insight**: Use two pointers traversing both lists, switching heads when reaching end, they meet at intersection.
34+
35+
**Algorithm**:
36+
1. a = headA, b = headB
37+
2. While a != b:
38+
a = headB if a null else a.next
39+
b = headA if b null else b.next
40+
3. Return a (intersection or null)
41+
42+
**Why this works**:
43+
- Both travel lenA + lenB
44+
- Meet at intersection if exists
45+
- O(m + n) time, O(1) space
46+
47+
## Complexity Analysis
48+
- **Time Complexity**: O(m + n)
49+
- **Space Complexity**: O(1)
50+
51+
## Key Insights
52+
- Equalizes path lengths by switching
53+
- Handles no intersection (meet at null)
54+
- No modification to lists
55+
56+
## Alternative Approaches
57+
1. **Hash Set**: Store nodes of one, check other
58+
2. **Length Diff**: Calculate lengths, advance longer
59+
60+
## Solutions in Different Languages
61+
62+
### Java
63+
```java
64+
// ... code ...
65+
```
66+
67+
### JavaScript
68+
```javascript
69+
// ... code ...
70+
```
71+
72+
### Python
73+
```python
74+
# ... code ...
75+
```
76+
77+
## Test Cases
78+
```
79+
Test Case 1: Intersect at 8 as above
80+
Test Case 2: No intersection -> null
81+
```
82+
83+
## Edge Cases
84+
1. No intersection
85+
2. Intersect at head
86+
3. One list empty
87+
4. Both empty
88+
89+
## Follow-up Questions
90+
1. What if cycles?
91+
2. What if multiple intersections?
92+
3. What if find all intersections?
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Intersection of Two Linked Lists - LeetCode #160
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* public class ListNode {
6+
* int val;
7+
* ListNode next;
8+
* ListNode(int x) {
9+
* val = x;
10+
* next = null;
11+
* }
12+
* }
13+
*/
14+
public class Solution {
15+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
16+
ListNode a = headA;
17+
ListNode b = headB;
18+
19+
while (a != b) {
20+
a = a == null ? headB : a.next;
21+
b = b == null ? headA : b.next;
22+
}
23+
24+
return a;
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Intersection of Two Linked Lists - LeetCode #160
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* function ListNode(val) {
6+
* this.val = val;
7+
* this.next = null;
8+
* }
9+
*/
10+
11+
/**
12+
* @param {ListNode} headA
13+
* @param {ListNode} headB
14+
* @return {ListNode}
15+
*/
16+
var getIntersectionNode = function(headA, headB) {
17+
let a = headA;
18+
let b = headB;
19+
20+
while (a !== b) {
21+
a = a === null ? headB : a.next;
22+
b = b === null ? headA : b.next;
23+
}
24+
25+
return a;
26+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Intersection of Two Linked Lists - LeetCode #160
2+
3+
# Definition for singly-linked list.
4+
# class ListNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.next = None
8+
9+
class Solution:
10+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
11+
a = headA
12+
b = headB
13+
14+
while a != b:
15+
a = headB if not a else a.next
16+
b = headA if not b else b.next
17+
18+
return a
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class Solution {
5+
public int lengthOfLongestSubstring(String s) {
6+
int n = s.length();
7+
int maxLength = 0;
8+
int left = 0;
9+
Set<Character> charSet = new HashSet<>();
10+
11+
for (int right = 0; right < n; right++) {
12+
char currentChar = s.charAt(right);
13+
14+
while (charSet.contains(currentChar)) {
15+
charSet.remove(s.charAt(left));
16+
left++;
17+
}
18+
19+
charSet.add(currentChar);
20+
maxLength = Math.max(maxLength, right - left + 1);
21+
}
22+
23+
return maxLength;
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function(s) {
6+
const n = s.length;
7+
let maxLength = 0;
8+
let left = 0;
9+
const charSet = new Set();
10+
11+
for (let right = 0; right < n; right++) {
12+
const currentChar = s[right];
13+
14+
while (charSet.has(currentChar)) {
15+
charSet.delete(s[left]);
16+
left++;
17+
}
18+
19+
charSet.add(currentChar);
20+
maxLength = Math.max(maxLength, right - left + 1);
21+
}
22+
23+
return maxLength;
24+
};

0 commit comments

Comments
 (0)