Compare commits

...

2 commits

Author SHA1 Message Date
Yudong Jin
da95ac3025
Update replace_linear_by_hashing.md 2024-12-04 18:04:11 +08:00
Yudong Jin
b471dda18c
Update replace_linear_by_hashing.md 2024-12-04 18:01:57 +08:00

View file

@ -1,6 +1,6 @@
# Hash optimization strategies
In algorithm problems, **we often reduce an algorithm's time complexity 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 an algorithm by replacing a linear search with a hash-based search**. Let's use an algorithm problem to deepen the understanding.
!!! question
@ -18,7 +18,7 @@ The code is shown below:
[file]{two_sum}-[class]{}-[func]{two_sum_brute_force}
```
This method has a time complexity of $O(n^2)$ and a space complexity of $O(1)$, which becomes very time-consuming with large data volumes.
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.
## Hash search: trading space for time
@ -42,6 +42,6 @@ The implementation code is shown below, requiring only a single loop:
[file]{two_sum}-[class]{}-[func]{two_sum_hash_table}
```
This method reduces the time complexity from $O(n^2)$ to $O(n)$ by using hash search, significantly improving the running efficiency.
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**.