mirror of
https://github.com/Rainyy21/framework_note.git
synced 2026-10-11 01:10:28 -04:00
vault backup: 2026-05-30 16:02:33
This commit is contained in:
@@ -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
|
||||
---
|
||||
Reference in New Issue
Block a user