mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-02 21:27:14 +00:00
build
This commit is contained in:
@@ -2,21 +2,21 @@
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 10.4 Hash optimization strategies
|
||||
# 10.4 Hash Optimization Strategy
|
||||
|
||||
In algorithm problems, **we often reduce the time complexity of an algorithm by replacing a linear search with a hash-based search**. Let's use an algorithm problem to deepen the understanding.
|
||||
In algorithm problems, **we often reduce the time complexity of algorithms by replacing linear search with hash-based search**. Let's use an algorithm problem to deepen our understanding.
|
||||
|
||||
!!! question
|
||||
|
||||
Given an integer array `nums` and a target element `target`, please search for two elements in the array whose "sum" equals `target`, and return their array indices. Any solution is acceptable.
|
||||
Given an integer array `nums` and a target element `target`, search for two elements in the array whose "sum" equals `target`, and return their array indices. Any solution will do.
|
||||
|
||||
## 10.4.1 Linear search: trading time for space
|
||||
## 10.4.1 Linear Search: Trading Time for Space
|
||||
|
||||
Consider traversing through all possible combinations directly. As shown in Figure 10-9, we initiate a nested loop, and in each iteration, we determine whether the sum of the two integers equals `target`. If so, we return their indices.
|
||||
Consider directly traversing all possible combinations. As shown in Figure 10-9, we open a two-layer loop and judge in each round whether the sum of two integers equals `target`. If so, return their indices.
|
||||
|
||||
{ class="animation-figure" }
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 10-9 Linear search solution for two-sum problem </p>
|
||||
<p align="center"> Figure 10-9 Linear search solution for two sum </p>
|
||||
|
||||
The code is shown below:
|
||||
|
||||
@@ -24,8 +24,8 @@ The code is shown below:
|
||||
|
||||
```python title="two_sum.py"
|
||||
def two_sum_brute_force(nums: list[int], target: int) -> list[int]:
|
||||
"""Method one: Brute force enumeration"""
|
||||
# Two-layer loop, time complexity is O(n^2)
|
||||
"""Method 1: Brute force enumeration"""
|
||||
# Two nested loops, time complexity is O(n^2)
|
||||
for i in range(len(nums) - 1):
|
||||
for j in range(i + 1, len(nums)):
|
||||
if nums[i] + nums[j] == target:
|
||||
@@ -36,10 +36,10 @@ The code is shown below:
|
||||
=== "C++"
|
||||
|
||||
```cpp title="two_sum.cpp"
|
||||
/* Method one: Brute force enumeration */
|
||||
/* Method 1: Brute force enumeration */
|
||||
vector<int> twoSumBruteForce(vector<int> &nums, int target) {
|
||||
int size = nums.size();
|
||||
// Two-layer loop, time complexity is O(n^2)
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
@@ -53,10 +53,10 @@ The code is shown below:
|
||||
=== "Java"
|
||||
|
||||
```java title="two_sum.java"
|
||||
/* Method one: Brute force enumeration */
|
||||
/* Method 1: Brute force enumeration */
|
||||
int[] twoSumBruteForce(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
// Two-layer loop, time complexity is O(n^2)
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
@@ -70,80 +70,188 @@ The code is shown below:
|
||||
=== "C#"
|
||||
|
||||
```csharp title="two_sum.cs"
|
||||
[class]{two_sum}-[func]{TwoSumBruteForce}
|
||||
/* Method 1: Brute force enumeration */
|
||||
int[] TwoSumBruteForce(int[] nums, int target) {
|
||||
int size = nums.Length;
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
return [i, j];
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="two_sum.go"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
/* Method 1: Brute force enumeration */
|
||||
func twoSumBruteForce(nums []int, target int) []int {
|
||||
size := len(nums)
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for i := 0; i < size-1; i++ {
|
||||
for j := i + 1; j < size; j++ {
|
||||
if nums[i]+nums[j] == target {
|
||||
return []int{i, j}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="two_sum.swift"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
/* Method 1: Brute force enumeration */
|
||||
func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for i in nums.indices.dropLast() {
|
||||
for j in nums.indices.dropFirst(i + 1) {
|
||||
if nums[i] + nums[j] == target {
|
||||
return [i, j]
|
||||
}
|
||||
}
|
||||
}
|
||||
return [0]
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="two_sum.js"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
/* Method 1: Brute force enumeration */
|
||||
function twoSumBruteForce(nums, target) {
|
||||
const n = nums.length;
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
if (nums[i] + nums[j] === target) {
|
||||
return [i, j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="two_sum.ts"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
/* Method 1: Brute force enumeration */
|
||||
function twoSumBruteForce(nums: number[], target: number): number[] {
|
||||
const n = nums.length;
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = i + 1; j < n; j++) {
|
||||
if (nums[i] + nums[j] === target) {
|
||||
return [i, j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="two_sum.dart"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
/* Method 1: Brute force enumeration */
|
||||
List<int> twoSumBruteForce(List<int> nums, int target) {
|
||||
int size = nums.length;
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (var i = 0; i < size - 1; i++) {
|
||||
for (var j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target) return [i, j];
|
||||
}
|
||||
}
|
||||
return [0];
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="two_sum.rs"
|
||||
[class]{}-[func]{two_sum_brute_force}
|
||||
/* Method 1: Brute force enumeration */
|
||||
pub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
|
||||
let size = nums.len();
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for i in 0..size - 1 {
|
||||
for j in i + 1..size {
|
||||
if nums[i] + nums[j] == target {
|
||||
return Some(vec![i as i32, j as i32]);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="two_sum.c"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
/* Method 1: Brute force enumeration */
|
||||
int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {
|
||||
for (int i = 0; i < numsSize; ++i) {
|
||||
for (int j = i + 1; j < numsSize; ++j) {
|
||||
if (nums[i] + nums[j] == target) {
|
||||
int *res = malloc(sizeof(int) * 2);
|
||||
res[0] = i, res[1] = j;
|
||||
*returnSize = 2;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
*returnSize = 0;
|
||||
return NULL;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="two_sum.kt"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
/* Method 1: Brute force enumeration */
|
||||
fun twoSumBruteForce(nums: IntArray, target: Int): IntArray {
|
||||
val size = nums.size
|
||||
// Two nested loops, time complexity is O(n^2)
|
||||
for (i in 0..<size - 1) {
|
||||
for (j in i + 1..<size) {
|
||||
if (nums[i] + nums[j] == target) return intArrayOf(i, j)
|
||||
}
|
||||
}
|
||||
return IntArray(0)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="two_sum.rb"
|
||||
[class]{}-[func]{two_sum_brute_force}
|
||||
### Method 1: Brute force enumeration ###
|
||||
def two_sum_brute_force(nums, target)
|
||||
# Two nested loops, time complexity is O(n^2)
|
||||
for i in 0...(nums.length - 1)
|
||||
for j in (i + 1)...nums.length
|
||||
return [i, j] if nums[i] + nums[j] == target
|
||||
end
|
||||
end
|
||||
|
||||
[]
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
This method has a time complexity of $O(n^2)$ and a space complexity of $O(1)$, which is very time-consuming with large data volumes.
|
||||
|
||||
```zig title="two_sum.zig"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
```
|
||||
## 10.4.2 Hash-Based Search: Trading Space for Time
|
||||
|
||||
This method has a time complexity of $O(n^2)$ and a space complexity of $O(1)$, which can be very time-consuming with large data volumes.
|
||||
|
||||
## 10.4.2 Hash search: trading space for time
|
||||
|
||||
Consider using a hash table, where the key-value pairs are the array elements and their indices, respectively. Loop through the array, performing the steps shown in Figure 10-10 during each iteration.
|
||||
Consider using a hash table where key-value pairs are array elements and element indices respectively. Loop through the array, performing the steps shown in Figure 10-10 in each round:
|
||||
|
||||
1. Check if the number `target - nums[i]` is in the hash table. If so, directly return the indices of these two elements.
|
||||
2. Add the key-value pair `nums[i]` and index `i` to the hash table.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
{ class="animation-figure" }
|
||||
|
||||
=== "<2>"
|
||||
{ class="animation-figure" }
|
||||
@@ -151,7 +259,7 @@ Consider using a hash table, where the key-value pairs are the array elements an
|
||||
=== "<3>"
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 10-10 Help hash table solve two-sum </p>
|
||||
<p align="center"> Figure 10-10 Hash table solution for two sum </p>
|
||||
|
||||
The implementation code is shown below, requiring only a single loop:
|
||||
|
||||
@@ -159,10 +267,10 @@ The implementation code is shown below, requiring only a single loop:
|
||||
|
||||
```python title="two_sum.py"
|
||||
def two_sum_hash_table(nums: list[int], target: int) -> list[int]:
|
||||
"""Method two: Auxiliary hash table"""
|
||||
"""Method 2: Auxiliary hash table"""
|
||||
# Auxiliary hash table, space complexity is O(n)
|
||||
dic = {}
|
||||
# Single-layer loop, time complexity is O(n)
|
||||
# Single loop, time complexity is O(n)
|
||||
for i in range(len(nums)):
|
||||
if target - nums[i] in dic:
|
||||
return [dic[target - nums[i]], i]
|
||||
@@ -173,12 +281,12 @@ The implementation code is shown below, requiring only a single loop:
|
||||
=== "C++"
|
||||
|
||||
```cpp title="two_sum.cpp"
|
||||
/* Method two: Auxiliary hash table */
|
||||
/* Method 2: Auxiliary hash table */
|
||||
vector<int> twoSumHashTable(vector<int> &nums, int target) {
|
||||
int size = nums.size();
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
unordered_map<int, int> dic;
|
||||
// Single-layer loop, time complexity is O(n)
|
||||
// Single loop, time complexity is O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.find(target - nums[i]) != dic.end()) {
|
||||
return {dic[target - nums[i]], i};
|
||||
@@ -192,12 +300,12 @@ The implementation code is shown below, requiring only a single loop:
|
||||
=== "Java"
|
||||
|
||||
```java title="two_sum.java"
|
||||
/* Method two: Auxiliary hash table */
|
||||
/* Method 2: Auxiliary hash table */
|
||||
int[] twoSumHashTable(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
Map<Integer, Integer> dic = new HashMap<>();
|
||||
// Single-layer loop, time complexity is O(n)
|
||||
// Single loop, time complexity is O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.containsKey(target - nums[i])) {
|
||||
return new int[] { dic.get(target - nums[i]), i };
|
||||
@@ -211,71 +319,218 @@ The implementation code is shown below, requiring only a single loop:
|
||||
=== "C#"
|
||||
|
||||
```csharp title="two_sum.cs"
|
||||
[class]{two_sum}-[func]{TwoSumHashTable}
|
||||
/* Method 2: Auxiliary hash table */
|
||||
int[] TwoSumHashTable(int[] nums, int target) {
|
||||
int size = nums.Length;
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
Dictionary<int, int> dic = [];
|
||||
// Single loop, time complexity is O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.ContainsKey(target - nums[i])) {
|
||||
return [dic[target - nums[i]], i];
|
||||
}
|
||||
dic.Add(nums[i], i);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="two_sum.go"
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
/* Method 2: Auxiliary hash table */
|
||||
func twoSumHashTable(nums []int, target int) []int {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
hashTable := map[int]int{}
|
||||
// Single loop, time complexity is O(n)
|
||||
for idx, val := range nums {
|
||||
if preIdx, ok := hashTable[target-val]; ok {
|
||||
return []int{preIdx, idx}
|
||||
}
|
||||
hashTable[val] = idx
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="two_sum.swift"
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
/* Method 2: Auxiliary hash table */
|
||||
func twoSumHashTable(nums: [Int], target: Int) -> [Int] {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
var dic: [Int: Int] = [:]
|
||||
// Single loop, time complexity is O(n)
|
||||
for i in nums.indices {
|
||||
if let j = dic[target - nums[i]] {
|
||||
return [j, i]
|
||||
}
|
||||
dic[nums[i]] = i
|
||||
}
|
||||
return [0]
|
||||
}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="two_sum.js"
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
/* Method 2: Auxiliary hash table */
|
||||
function twoSumHashTable(nums, target) {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
let m = {};
|
||||
// Single loop, time complexity is O(n)
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
if (m[target - nums[i]] !== undefined) {
|
||||
return [m[target - nums[i]], i];
|
||||
} else {
|
||||
m[nums[i]] = i;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="two_sum.ts"
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
/* Method 2: Auxiliary hash table */
|
||||
function twoSumHashTable(nums: number[], target: number): number[] {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
let m: Map<number, number> = new Map();
|
||||
// Single loop, time complexity is O(n)
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
let index = m.get(target - nums[i]);
|
||||
if (index !== undefined) {
|
||||
return [index, i];
|
||||
} else {
|
||||
m.set(nums[i], i);
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="two_sum.dart"
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
/* Method 2: Auxiliary hash table */
|
||||
List<int> twoSumHashTable(List<int> nums, int target) {
|
||||
int size = nums.length;
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
Map<int, int> dic = HashMap();
|
||||
// Single loop, time complexity is O(n)
|
||||
for (var i = 0; i < size; i++) {
|
||||
if (dic.containsKey(target - nums[i])) {
|
||||
return [dic[target - nums[i]]!, i];
|
||||
}
|
||||
dic.putIfAbsent(nums[i], () => i);
|
||||
}
|
||||
return [0];
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="two_sum.rs"
|
||||
[class]{}-[func]{two_sum_hash_table}
|
||||
/* Method 2: Auxiliary hash table */
|
||||
pub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
let mut dic = HashMap::new();
|
||||
// Single loop, time complexity is O(n)
|
||||
for (i, num) in nums.iter().enumerate() {
|
||||
match dic.get(&(target - num)) {
|
||||
Some(v) => return Some(vec![*v as i32, i as i32]),
|
||||
None => dic.insert(num, i as i32),
|
||||
};
|
||||
}
|
||||
None
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="two_sum.c"
|
||||
[class]{HashTable}-[func]{}
|
||||
/* Hash table */
|
||||
typedef struct {
|
||||
int key;
|
||||
int val;
|
||||
UT_hash_handle hh; // Implemented using uthash.h
|
||||
} HashTable;
|
||||
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
/* Hash table lookup */
|
||||
HashTable *find(HashTable *h, int key) {
|
||||
HashTable *tmp;
|
||||
HASH_FIND_INT(h, &key, tmp);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/* Hash table element insertion */
|
||||
void insert(HashTable **h, int key, int val) {
|
||||
HashTable *t = find(*h, key);
|
||||
if (t == NULL) {
|
||||
HashTable *tmp = malloc(sizeof(HashTable));
|
||||
tmp->key = key, tmp->val = val;
|
||||
HASH_ADD_INT(*h, key, tmp);
|
||||
} else {
|
||||
t->val = val;
|
||||
}
|
||||
}
|
||||
|
||||
/* Method 2: Auxiliary hash table */
|
||||
int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
|
||||
HashTable *hashtable = NULL;
|
||||
for (int i = 0; i < numsSize; i++) {
|
||||
HashTable *t = find(hashtable, target - nums[i]);
|
||||
if (t != NULL) {
|
||||
int *res = malloc(sizeof(int) * 2);
|
||||
res[0] = t->val, res[1] = i;
|
||||
*returnSize = 2;
|
||||
return res;
|
||||
}
|
||||
insert(&hashtable, nums[i], i);
|
||||
}
|
||||
*returnSize = 0;
|
||||
return NULL;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="two_sum.kt"
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
/* Method 2: Auxiliary hash table */
|
||||
fun twoSumHashTable(nums: IntArray, target: Int): IntArray {
|
||||
val size = nums.size
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
val dic = HashMap<Int, Int>()
|
||||
// Single loop, time complexity is O(n)
|
||||
for (i in 0..<size) {
|
||||
if (dic.containsKey(target - nums[i])) {
|
||||
return intArrayOf(dic[target - nums[i]]!!, i)
|
||||
}
|
||||
dic[nums[i]] = i
|
||||
}
|
||||
return IntArray(0)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="two_sum.rb"
|
||||
[class]{}-[func]{two_sum_hash_table}
|
||||
### Method 2: Auxiliary hash table ###
|
||||
def two_sum_hash_table(nums, target)
|
||||
# Auxiliary hash table, space complexity is O(n)
|
||||
dic = {}
|
||||
# Single loop, time complexity is O(n)
|
||||
for i in 0...nums.length
|
||||
return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i])
|
||||
|
||||
dic[nums[i]] = i
|
||||
end
|
||||
|
||||
[]
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
This method reduces the time complexity from $O(n^2)$ to $O(n)$ through hash-based search, greatly improving runtime efficiency.
|
||||
|
||||
```zig title="two_sum.zig"
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
```
|
||||
|
||||
This method reduces the time complexity from $O(n^2)$ to $O(n)$ by using hash search, significantly enhancing runtime efficiency.
|
||||
|
||||
As it requires maintaining an additional hash table, the space complexity is $O(n)$. **Nevertheless, this method has a more balanced time-space efficiency overall, making it the optimal solution for this problem**.
|
||||
Since an additional hash table needs to be maintained, the space complexity is $O(n)$. **Nevertheless, this method achieves a more balanced overall time-space efficiency, making it the optimal solution for this problem**.
|
||||
|
||||
Reference in New Issue
Block a user