vault backup: 2026-05-30 16:02:33

This commit is contained in:
Rainyy21
2026-05-30 16:02:33 -04:00
commit 96449f8968
43 changed files with 2837 additions and 0 deletions
@@ -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
```
+82
View File
@@ -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)$).
+13
View File
@@ -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
---
+12
View File
@@ -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
---
+13
View File
@@ -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
---
+13
View File
@@ -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
---
+14
View File
@@ -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
---