mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 03:56:28 +08:00
Compare commits
2 commits
3e40c1153d
...
da95ac3025
Author | SHA1 | Date | |
---|---|---|---|
|
da95ac3025 | ||
|
b471dda18c |
1 changed files with 3 additions and 3 deletions
|
@ -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**.
|
||||
|
|
Loading…
Reference in a new issue