mirror of
https://github.com/Rainyy21/framework_note.git
synced 2026-10-10 23:30:28 -04:00
vault backup: 2026-05-30 16:02:33
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
# 🗂️ LeetCode Note Index
|
||||
|
||||
Welcome to your LeetCode problem index. This dashboard automatically aggregates and sorts all your coding notes from the `note/` directory by difficulty (**Easy**, **Medium**, and **Hard**).
|
||||
|
||||
---
|
||||
|
||||
## 📂 Difficulty Categorization
|
||||
|
||||
> [!SUCCESS] 🟢 Easy Problems
|
||||
> List of all Easy difficulty questions.
|
||||
> ```dataview
|
||||
> TABLE choice(status = "Solved", "🟢 Solved", "🔴 Unsolved") AS Status
|
||||
> FROM "leetcode/note/easy" OR #leetcode/easy
|
||||
> SORT file.name ASC
|
||||
> ```
|
||||
|
||||
> [!WARNING] 🟡 Medium Problems
|
||||
> List of all Medium difficulty questions.
|
||||
> ```dataview
|
||||
> TABLE choice(status = "Solved", "🟢 Solved", "🔴 Unsolved") AS Status
|
||||
> FROM "leetcode/note/medium" OR #leetcode/medium
|
||||
> SORT file.name ASC
|
||||
> ```
|
||||
|
||||
> [!DANGER] 🔴 Hard Problems
|
||||
> List of all Hard difficulty questions.
|
||||
> ```dataview
|
||||
> TABLE choice(status = "Solved", "🟢 Solved", "🔴 Unsolved") AS Status
|
||||
> FROM "leetcode/note/hard" OR #leetcode/hard
|
||||
> SORT file.name ASC
|
||||
> ```
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
---
|
||||
id: 1346
|
||||
title: Check If N and Its Double Exist
|
||||
difficulty: Easy
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- two-pointers
|
||||
- binary-search
|
||||
- sorting
|
||||
status: Solved
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/check-if-n-and-its-double-exist/
|
||||
review_needed: false
|
||||
---
|
||||
|
||||
# 1346. Check If N and Its Double Exist
|
||||
|
||||
> [!info] **Problem Link**: [LeetCode - Check If N and Its Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist/)
|
||||
|
||||
## 📝 Problem Description
|
||||
|
||||
Given an array `arr` of integers, check if there exist two indices `i` and `j` such that :
|
||||
|
||||
- ` i != j`
|
||||
- `0 <= i, j < arr.length`
|
||||
- `arr[i] == 2 * arr[j]`
|
||||
|
||||
|
||||
---
|
||||
|
||||
### 📥 Example 1
|
||||
> **Input:** `arr = [10,2,5,3]`
|
||||
> **Output:** `true`
|
||||
> **Explanation:** For `i = 0` and `j = 2`, `arr[i] == 10 == 2 * 5 == 2 * arr[j]`
|
||||
|
||||
### 📥 Example 2
|
||||
> **Input:** `arr = [3,1,7,11]`
|
||||
> **Output:** `false`
|
||||
> **Explanation:** There is no i and j that satisfy the conditions.
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 💡 Approaches & Explanations
|
||||
Have a mem that hold int that you have see before. for each int in the array you check if you seen double or half in the mem if it is than return True. In the end return False
|
||||
|
||||
## 💻 Code Implementations
|
||||
|
||||
### Python3
|
||||
```python
|
||||
class Solution:
|
||||
def checkIfExist(self, arr: List[int]) -> bool:
|
||||
mem = []
|
||||
for idx , i in enumerate(arr):
|
||||
if i*2 in mem or i/2 in mem:
|
||||
return True
|
||||
mem.append(i)
|
||||
return False
|
||||
```
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
id: 169
|
||||
title: Majority Element
|
||||
difficulty: Easy
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- sorting
|
||||
- counting
|
||||
status: Solved
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/majority-element/
|
||||
review_needed: false
|
||||
---
|
||||
|
||||
# 169. Majority Element
|
||||
|
||||
> [!info] **Problem Link**: [LeetCode - Majority Element](https://leetcode.com/problems/majority-element/)
|
||||
|
||||
## 📝 Problem Description
|
||||
|
||||
Given an array `nums` of size `n`, return the majority element.
|
||||
|
||||
The majority element is the element that appears more than `[n / 2]` times. You may assume that the majority element always exists in the array.
|
||||
|
||||
|
||||
---
|
||||
|
||||
### 📥 Example 1
|
||||
> **Input:** `nums = [3,2,3]`
|
||||
> **Output:** `3`
|
||||
|
||||
### 📥 Example 2
|
||||
> **Input:** `nums = [2,2,1,1,1,2,2]`
|
||||
> **Output:** `2`
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 💡 Approaches & Explanations
|
||||
have cont, if cont is equal to 0 the res become the highest amount
|
||||
if i equal to the res than add one to count anything else cont -1
|
||||
return res at the end
|
||||
|
||||
## 💻 Code Implementations
|
||||
|
||||
### Python3
|
||||
```python
|
||||
class Solution:
|
||||
def majorityElement(self, nums: List[int]) -> int:
|
||||
cont = 0
|
||||
res = None
|
||||
for i in nums:
|
||||
if cont == 0:
|
||||
res = i
|
||||
cont +=1 if res == i else -1
|
||||
return res
|
||||
```
|
||||
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: 1
|
||||
title: Two Sum
|
||||
difficulty: Easy
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
status: Solved
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/two-sum/
|
||||
review_needed: false
|
||||
---
|
||||
|
||||
# 1. Two Sum
|
||||
|
||||
> [!info] **Problem Link**: [LeetCode - Two Sum](https://leetcode.com/problems/two-sum/)
|
||||
|
||||
## 📝 Problem Description
|
||||
|
||||
Given an array of integers `nums` and an integer `target`, return *indices of the two numbers such that they add up to `target`*.
|
||||
|
||||
You may assume that each input would have ***exactly* one solution**, and you may not use the *same* element twice.
|
||||
|
||||
You can return the answer in any order.
|
||||
|
||||
---
|
||||
|
||||
### 📥 Example 1
|
||||
> **Input:** `nums = [2,7,11,15]`, `target = 9`
|
||||
> **Output:** `[0,1]`
|
||||
> **Explanation:** Because `nums[0] + nums[1] == 9`, we return `[0, 1]`.
|
||||
|
||||
### 📥 Example 2
|
||||
> **Input:** `nums = [3,2,4]`, `target = 6`
|
||||
> **Output:** `[1,2]`
|
||||
|
||||
### 📥 Example 3
|
||||
> **Input:** `nums = [3,3]`, `target = 6`
|
||||
> **Output:** `[0,1]`
|
||||
|
||||
---
|
||||
|
||||
## 💡 Approaches & Explanations
|
||||
|
||||
### Approach 1: Hash Map (One-Pass) — *Optimal*
|
||||
The optimal approach is to use a hash map to keep track of the numbers we have seen so far and their indices. As we iterate through the array, we check if the complement (`target - nums[i]`) already exists in our hash map.
|
||||
- If it does, we found the pair and return their indices.
|
||||
- If it doesn't, we add the current number and its index to the hash map.
|
||||
|
||||
#### 📊 Complexity Analysis
|
||||
- **Time Complexity:** $\mathcal{O}(N)$ where $N$ is the number of elements in the array. We traverse the list containing $N$ elements only once, and lookup in the hash table takes $\mathcal{O}(1)$ time.
|
||||
- **Space Complexity:** $\mathcal{O}(N)$ since we store at most $N$ elements in the hash map.
|
||||
|
||||
---
|
||||
|
||||
### Approach 2: Brute Force
|
||||
Compare every pair of numbers to see if their sum equals the target.
|
||||
- **Time Complexity:** $\mathcal{O}(N^2)$
|
||||
- **Space Complexity:** $\mathcal{O}(1)$
|
||||
|
||||
---
|
||||
|
||||
## 💻 Code Implementations
|
||||
|
||||
### Python3
|
||||
```python
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
seen = {} # val -> index
|
||||
for i, num in enumerate(nums):
|
||||
complement = target - num
|
||||
if complement in seen:
|
||||
return [seen[complement], i]
|
||||
seen[num] = i
|
||||
return []
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🧠 Key Takeaways & Lessons
|
||||
- **The Complement Trick:** When looking for a pair that sums to a target, rephrase the search: instead of looking for $A + B = \text{target}$, look for $\text{complement} = \text{target} - A$ that is already stored.
|
||||
- **Hash Map for $\mathcal{O}(1)$ Lookups:** Trading memory (space complexity) for time complexity is a common pattern in array search problems.
|
||||
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 217
|
||||
title: Contains Duplicate
|
||||
difficulty: Easy
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
status: Solved
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/contains-duplicate/
|
||||
review_needed: false
|
||||
---
|
||||
|
||||
# 217. Contains Duplicate
|
||||
|
||||
> [!info] **Problem Link**: [LeetCode - Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)
|
||||
|
||||
## 📝 Problem Description
|
||||
Given an integer array `nums`, return `true` if *any value appears at least twice in the array*, and return `false` if every element is distinct.
|
||||
|
||||
---
|
||||
|
||||
### 📥 Example 1
|
||||
> **Input:** `nums = [1,2,3,1]`
|
||||
> **Output:** `true`
|
||||
> **Explanation:** The element 1 occurs at the indices 0 and 3.
|
||||
|
||||
### 📥 Example 2
|
||||
> **Input:** `nums = [1,2,3,4]`
|
||||
> **Output:** `false`
|
||||
|
||||
### 📥 Example 3
|
||||
> **Input:** `nums = [1,1,1,3,3,4,3,2,4,2]`
|
||||
> **Output:** `true`
|
||||
|
||||
---
|
||||
|
||||
## 💡 Approaches & Explanations
|
||||
|
||||
### Approach 1: Hash Set (Length Comparison)
|
||||
The simplest way to check for duplicates in Python is to convert the array `nums` into a set. A set only contains unique elements, so:
|
||||
- If there are duplicates, the length of the set will be less than the length of the array.
|
||||
- If all elements are unique, the lengths will be equal.
|
||||
|
||||
#### 📊 Complexity Analysis
|
||||
- **Time Complexity:** $\mathcal{O}(N)$ where $N$ is the number of elements in the array. Converting an array to a set requires traversing the entire array and inserting each element.
|
||||
- **Space Complexity:** $\mathcal{O}(N)$ as we store up to $N$ unique elements in the set.
|
||||
|
||||
---
|
||||
|
||||
### Approach 2: Hash Set (Early Return / One-Pass) — *Alternative*
|
||||
Instead of converting the entire array to a set, we can iterate through the array and store elements in a set as we go. If we encounter an element that is already in the set, we can return `true` immediately. This avoids processing the rest of the array.
|
||||
|
||||
#### 📊 Complexity Analysis
|
||||
- **Time Complexity:** $\mathcal{O}(N)$ in the worst case (no duplicates). In the best case, it can be $\mathcal{O}(1)$ if a duplicate is found at the beginning.
|
||||
- **Space Complexity:** $\mathcal{O}(N)$ to store the visited elements.
|
||||
|
||||
---
|
||||
|
||||
## 💻 Code Implementations
|
||||
|
||||
### Python3
|
||||
|
||||
#### Option A: Length Comparison (Concise)
|
||||
```python
|
||||
class Solution:
|
||||
def containsDuplicate(self, nums: List[int]) -> bool:
|
||||
return len(nums) != len(set(nums))
|
||||
```
|
||||
|
||||
#### Option B: Early Return (Optimal for large lists with early duplicates)
|
||||
```python
|
||||
class Solution:
|
||||
def containsDuplicate(self, nums: List[int]) -> bool:
|
||||
seen = set()
|
||||
for num in nums:
|
||||
if num in seen:
|
||||
return True
|
||||
seen.add(num)
|
||||
return False
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🧠 Key Takeaways & Lessons
|
||||
- **Hash Set for Uniqueness:** Sets are the go-to data structure when you need to verify uniqueness or look up elements in $\mathcal{O}(1)$ time.
|
||||
- **Early Return Optimization:** While converting the whole list to a set is clean and concise, iterating and returning early when a duplicate is found can save time and memory in practice.
|
||||
- **Time-Space Trade-off:** We use extra space ($\mathcal{O}(N)$ memory) to achieve linear time complexity ($\mathcal{O}(N)$) instead of a brute-force search ($\mathcal{O}(N^2)$).
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
id: 242
|
||||
title: Valid Anagram
|
||||
difficulty: Easy
|
||||
tags:
|
||||
- hash-table
|
||||
- string
|
||||
- sorting
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/valid-anagram/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
id: 290
|
||||
title: Word Pattern
|
||||
difficulty: Easy
|
||||
tags:
|
||||
- hash-table
|
||||
- string
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/word-pattern/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
id: 724
|
||||
title: Find Pivot Index
|
||||
difficulty: Easy
|
||||
tags:
|
||||
- array
|
||||
- prefix-sum
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/find-pivot-index/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
id: 30
|
||||
title: Substring with Concatenation of All Words
|
||||
difficulty: Hard
|
||||
tags:
|
||||
- hash-table
|
||||
- string
|
||||
- sliding-window
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/substring-with-concatenation-of-all-words/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
id: 381
|
||||
title: Insert Delete GetRandom O(1) - Duplicates allowed
|
||||
difficulty: Hard
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- math
|
||||
- randomized
|
||||
- design
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
id: 41
|
||||
title: First Missing Positive
|
||||
difficulty: Hard
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/first-missing-positive/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
id: 128
|
||||
title: Longest Consecutive Sequence
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- union-find
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/longest-consecutive-sequence/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
id: 2017
|
||||
title: Grid Game
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- matrix
|
||||
- prefix-sum
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/grid-game/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
id: 238
|
||||
title: Product of Array Except Self
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- prefix-sum
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/product-of-array-except-self/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,14 @@
|
||||
---
|
||||
id: 271
|
||||
title: Encode and Decode Strings
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- string
|
||||
- design
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/encode-and-decode-strings/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
id: 347
|
||||
title: Top K Frequent Elements
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- divide-and-conquer
|
||||
- sorting
|
||||
- heap-priority-queue
|
||||
- bucket-sort
|
||||
- quickselect
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/top-k-frequent-elements/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
id: 36
|
||||
title: Valid Sudoku
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- matrix
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/valid-sudoku/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
id: 380
|
||||
title: Insert Delete GetRandom O(1)
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- math
|
||||
- randomized
|
||||
- design
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/insert-delete-getrandom-o1/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
id: 442
|
||||
title: Find All Duplicates in an Array
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/find-all-duplicates-in-an-array/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,14 @@
|
||||
---
|
||||
id: 49
|
||||
title: Group Anagrams
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- string
|
||||
- sorting
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/group-anagrams/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
id: 560
|
||||
title: Subarray Sum Equals K
|
||||
difficulty: Medium
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
- prefix-sum
|
||||
status: unsolve
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/subarray-sum-equals-k/
|
||||
review_needed: false
|
||||
---
|
||||
@@ -0,0 +1,375 @@
|
||||
---
|
||||
tags:
|
||||
- leetcode/array-hashing
|
||||
- study-guide
|
||||
- coding-practice
|
||||
difficulty_distribution:
|
||||
easy: 7
|
||||
medium: 10
|
||||
hard: 3
|
||||
total_problems: 20
|
||||
completed_problems: 1
|
||||
progress_percentage: 5%
|
||||
last_updated: 2026-05-26
|
||||
---
|
||||
|
||||
# 🚀 Array & Hashing: LeetCode Master List
|
||||
|
||||
Welcome to your study guide for **Array and Hashing**! This topic is the bedrock of coding interviews, establishing the core patterns for lookups, frequency counting, prefix arrays, and space-time tradeoffs.
|
||||
|
||||
---
|
||||
|
||||
## 📊 Progress Tracker
|
||||
|
||||
| Status | # | Problem | Difficulty | Key Technique | Links |
|
||||
| :----: | :--: | :------------------------------------------------------------------------------------------------------------------------ | :--------: | :----------------------------- | :--------------------------------------------------------------------------------------: |
|
||||
| [x] | 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | 🟢 Easy | Hash Map Complement | [LeetCode](https://leetcode.com/problems/two-sum/) |
|
||||
| [ ] | 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | 🟢 Easy | Hash Set Presence | [LeetCode](https://leetcode.com/problems/contains-duplicate/) |
|
||||
| [ ] | 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | 🟢 Easy | Frequency Count / Sorting | [LeetCode](https://leetcode.com/problems/valid-anagram/) |
|
||||
| [ ] | 169 | [Majority Element](https://leetcode.com/problems/majority-element/) | 🟢 Easy | Boyer-Moore Voting / Hash Map | [LeetCode](https://leetcode.com/problems/majority-element/) |
|
||||
| [ ] | 290 | [Word Pattern](https://leetcode.com/problems/word-pattern/) | 🟢 Easy | Bijective Hash Mapping | [LeetCode](https://leetcode.com/problems/word-pattern/) |
|
||||
| [ ] | 1346 | [Check If N and Its Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist/) | 🟢 Easy | Hash Set Double-Lookup | [LeetCode](https://leetcode.com/problems/check-if-n-and-its-double-exist/) |
|
||||
| [ ] | 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | 🟢 Easy | Prefix Sum Balance | [LeetCode](https://leetcode.com/problems/find-pivot-index/) |
|
||||
| [ ] | 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | 🟡 Medium | Categorization Key Mapping | [LeetCode](https://leetcode.com/problems/group-anagrams/) |
|
||||
| [ ] | 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | 🟡 Medium | Bucket Sort / Heap Tracking | [LeetCode](https://leetcode.com/problems/top-k-frequent-elements/) |
|
||||
| [ ] | 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | 🟡 Medium | Left/Right Prefix Products | [LeetCode](https://leetcode.com/problems/product-of-array-except-self/) |
|
||||
| [ ] | 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | 🟡 Medium | Sub-grid Hashing (Bitmask/Set) | [LeetCode](https://leetcode.com/problems/valid-sudoku/) |
|
||||
| [ ] | 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) | 🟡 Medium | Hash Set Boundary Scan | [LeetCode](https://leetcode.com/problems/longest-consecutive-sequence/) |
|
||||
| [ ] | 560 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | 🟡 Medium | Prefix Sum + Frequency Map | [LeetCode](https://leetcode.com/problems/subarray-sum-equals-k/) |
|
||||
| [ ] | 271 | [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/) | 🟡 Medium | Length-Prefix Chunking | [LeetCode](https://leetcode.com/problems/encode-and-decode-strings/) |
|
||||
| [ ] | 442 | [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | 🟡 Medium | In-place Sign Negation | [LeetCode](https://leetcode.com/problems/find-all-duplicates-in-an-array/) |
|
||||
| [ ] | 380 | [Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | 🟡 Medium | Array + Map Index Swapping | [LeetCode](https://leetcode.com/problems/insert-delete-getrandom-o1/) |
|
||||
| [ ] | 2017 | [Grid Game](https://leetcode.com/problems/grid-game/) | 🟡 Medium | 2-Row Prefix Sum Selection | [LeetCode](https://leetcode.com/problems/grid-game/) |
|
||||
| [ ] | 41 | [First Missing Positive](https://leetcode.com/problems/first-missing-positive/) | 🔴 Hard | Cyclic In-place Sorting | [LeetCode](https://leetcode.com/problems/first-missing-positive/) |
|
||||
| [ ] | 30 | [Substring with Concatenation](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | 🔴 Hard | Sliding Window + Count Maps | [LeetCode](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) |
|
||||
| [ ] | 381 | [Insert Delete GetRandom O(1) - Duplicates](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/) | 🔴 Hard | Array + Map to Set of Indices | [LeetCode](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/) |
|
||||
|
||||
---
|
||||
|
||||
## 🟢 Easy Problems (7)
|
||||
|
||||
### 1. Two Sum (LC 1)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Instead of checking all pairs, store the numbers you have seen in a hash map mapping `value -> index`. For each `num`, check if its complement (`target - num`) is already in the map.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(n)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def twoSum(nums: list[int], target: int) -> list[int]:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 2. Contains Duplicate (LC 217)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Iterate through the array and store each element in a Hash Set. If the element is already in the set, a duplicate has been found.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(n)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def containsDuplicate(nums: list[int]) -> bool:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 3. Valid Anagram (LC 242)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Count the frequency of characters in both strings. You can use two hash maps, or a single array size 26 if constraints are purely lowercase a-z. Compare the frequency distributions.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(1)$ Space (since lowercase English alphabet count is fixed at 26)
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def isAnagram(s: str, t: str) -> bool:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 4. Majority Element (LC 169)
|
||||
> [!TIP]
|
||||
> **Core Concept:** While a Hash Map works, you can solve this in $O(1)$ space using **Boyer-Moore Voting Algorithm**. Keep a `candidate` and a `count`. When `count == 0`, pick the current element as candidate. Increment `count` if the element matches the candidate, else decrement it.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(1)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def majorityElement(nums: list[int]) -> int:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 5. Word Pattern (LC 290)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Establish a bijective (two-way) mapping between character in `pattern` and words in string `s` using two hash maps. If a key maps to a different value in either map, return `False`.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(n)$ Space (where $n$ is total chars/words)
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def wordPattern(pattern: str, s: str) -> bool:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 6. Check If N and Its Double Exist (LC 1346)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Iterate through the array. Check if `2 * num` or `num / 2` (if divisible by 2) exists in your Hash Set of previously visited values.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(n)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def checkIfExist(arr: list[int]) -> bool:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 7. Find Pivot Index (LC 724)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Compute the total sum of the array. Track the running `left_sum`. For each index, check if `left_sum == total_sum - left_sum - num`. If it is, that's the pivot.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(1)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def pivotIndex(nums: list[int]) -> int:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
## 🟡 Medium Problems (10)
|
||||
|
||||
### 8. Group Anagrams (LC 49)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Group words by their character signature. The signature can be a sorted string, or a character count tuple `[0] * 26` mapped to list of matching strings.
|
||||
|
||||
- **Complexity Target:** $O(n \cdot m)$ Time (where $m$ is max word length) | $O(n \cdot m)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def groupAnagrams(strs: list[str]) -> list[list[str]]:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 9. Top K Frequent Elements (LC 347)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Count frequencies using a hash map. Instead of sorting (which is $O(n \log n)$), use **Bucket Sort** where index represents frequencies. Since the max frequency is capped at `len(nums)`, we can assemble the result in linear time.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(n)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def topKFrequent(nums: list[int], k: int) -> list[int]:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 10. Product of Array Except Self (LC 238)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Create an output array. Do a forward pass to store the prefix product at each index. Then do a backward pass, keeping a running suffix product, multiplying it into your result.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(1)$ Extra Space (excluding output array)
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def productExceptSelf(nums: list[int]) -> list[int]:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 11. Valid Sudoku (LC 36)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Track duplicates in rows, columns, and 3x3 sub-grids. Use a set for each row, column, and sub-grid. The subgrid index can be identified using `(r // 3, c // 3)`.
|
||||
|
||||
- **Complexity Target:** $O(1)$ Time & Space (since grid size is constant $9 \times 9$)
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def isValidSudoku(board: list[list[str]]) -> bool:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 12. Longest Consecutive Sequence (LC 128)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Convert the array to a Hash Set. Loop through each number; if `num - 1` is not in the set, it means `num` is the *start* of a new sequence. Scan forward (`num + 1`, `num + 2`...) to find the sequence length. This guarantees each element is processed at most twice.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(n)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def longestConsecutive(nums: list[int]) -> int:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 13. Subarray Sum Equals K (LC 560)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Use prefix sum properties. If the difference between current prefix sum and target `k` (i.e. `prefix_sum - k`) was seen previously as a prefix sum, the subarray between those indices sums to `k`. Store prefix sum frequencies in a map.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(n)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def subarraySum(nums: list[int], k: int) -> int:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 14. Encode and Decode Strings (LC 271 / Premium)
|
||||
> [!TIP]
|
||||
> **Core Concept:** To combine strings safely, prepend each string with its length followed by a delimiter (e.g. `"4#neet"`). When decoding, parse the number, jump past the delimiter, and slice that exact length.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(1)$ Extra Space (excluding output list)
|
||||
- **Starter Template:**
|
||||
```python
|
||||
class Codec:
|
||||
def encode(self, strs: list[str]) -> str:
|
||||
pass
|
||||
|
||||
def decode(self, s: str) -> list[str]:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 15. Find All Duplicates in an Array (LC 442)
|
||||
> [!TIP]
|
||||
> **Core Concept:** The array contains elements from `1` to `n`. You can use the values as indices. As you iterate, look up `nums[abs(x) - 1]`. If it is positive, negate it. If it is already negative, then `abs(x)` has been seen before.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(1)$ Extra Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def findDuplicates(nums: list[int]) -> list[int]:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 16. Insert Delete GetRandom O(1) (LC 380)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Store values in an Array List to achieve $O(1)$ random lookups. Maintain a Hash Map `val -> index` to achieve $O(1)$ insertions and updates. When deleting, swap the element to delete with the last element in the array, update the map, and pop from the array.
|
||||
|
||||
- **Complexity Target:** $O(1)$ Time average | $O(n)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
class RandomizedSet:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def insert(self, val: int) -> bool:
|
||||
pass
|
||||
|
||||
def remove(self, val: int) -> bool:
|
||||
pass
|
||||
|
||||
def getRandom(self) -> int:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 17. Grid Game (LC 2017)
|
||||
> [!TIP]
|
||||
> **Core Concept:** The first robot splits the grid into two paths. Compute prefix sums for row 0 and row 1. Robot 1 wants to minimize Robot 2's maximum score, which will always be the max of the remaining top-right elements or bottom-left elements after Robot 1 pivots.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(n)$ Space (or $O(1)$ if prefix sums are computed on-the-fly)
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def gridGame(grid: list[list[int]]) -> int:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
## 🔴 Hard Problems (3)
|
||||
|
||||
### 18. First Missing Positive (LC 41)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Use **Cyclic Sort** style placement. Iterate through the array and try to place each number `x` (if it lies in range `[1, n]`) at its correct index `x - 1`. Perform this swap in a loop. Finally, scan the array to find the first index `i` where `nums[i] != i + 1`.
|
||||
|
||||
- **Complexity Target:** $O(n)$ Time | $O(1)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def firstMissingPositive(nums: list[int]) -> int:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 19. Substring with Concatenation of All Words (LC 30)
|
||||
> [!TIP]
|
||||
> **Core Concept:** All words are of equal length `L`. Use a sliding window starting at each offset `0 <= i < L`. For each window, use a hash map to count occurrences of words of length `L` and compare it with the count map of the target `words` list.
|
||||
|
||||
- **Complexity Target:** $O(n \cdot L)$ Time (where $n$ is string length, $L$ is word length) | $O(m \cdot L)$ Space (where $m$ is number of words)
|
||||
- **Starter Template:**
|
||||
```python
|
||||
def findSubstring(s: str, words: list[str]) -> list[int]:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
|
||||
---
|
||||
|
||||
### 20. Insert Delete GetRandom O(1) - Duplicates Allowed (LC 381)
|
||||
> [!TIP]
|
||||
> **Core Concept:** Similar to LC 380, but the Hash Map now maps `val -> Set of indices` where the value resides in our dynamic list. During deletion, lookup any index from the value's set, swap with the list's last element, and update the set of the swapped element accordingly.
|
||||
|
||||
- **Complexity Target:** $O(1)$ Time average | $O(n)$ Space
|
||||
- **Starter Template:**
|
||||
```python
|
||||
class RandomizedCollection:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def insert(self, val: int) -> bool:
|
||||
pass
|
||||
|
||||
def remove(self, val: int) -> bool:
|
||||
pass
|
||||
|
||||
def getRandom(self) -> int:
|
||||
pass
|
||||
```
|
||||
- **My Notes / Solution:**
|
||||
*
|
||||
Reference in New Issue
Block a user