Compare commits

...

3 commits

Author SHA1 Message Date
K3v123
a20b1c4379
Merge 3e40c1153d into 894e3d536b 2024-11-24 01:27:27 -04:00
sslmj2020
894e3d536b
Update array_binary_tree.cpp (#1568)
Some checks failed
C++ / build (Release, clang, clang++, ubuntu-latest) (push) Failing after 16s
C++ / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 14s
C++ / build (Release, cl, cl, windows-latest) (push) Has been cancelled
打印的小错误
2024-11-24 00:39:33 +08:00
K3v123
3e40c1153d
translation: Update replace_linear_by_hashing.md
refined some parts of it.
2024-11-10 20:00:55 +13:00
2 changed files with 8 additions and 8 deletions

View file

@ -115,9 +115,9 @@ int main() {
int i = 1; int i = 1;
int l = abt.left(i), r = abt.right(i), p = abt.parent(i); int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
cout << "\n当前节点的索引为 " << i << ",值为 " << abt.val(i) << "\n"; cout << "\n当前节点的索引为 " << i << ",值为 " << abt.val(i) << "\n";
cout << "其左子节点的索引为 " << l << ",值为 " << (l != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n"; cout << "其左子节点的索引为 " << l << ",值为 " << (abt.val(l) != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
cout << "其右子节点的索引为 " << r << ",值为 " << (r != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n"; cout << "其右子节点的索引为 " << r << ",值为 " << (abt.val(r) != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
cout << "其父节点的索引为 " << p << ",值为 " << (p != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n"; cout << "其父节点的索引为 " << p << ",值为 " << (abt.val(p) != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
// 遍历树 // 遍历树
vector<int> res = abt.levelOrder(); vector<int> res = abt.levelOrder();

View file

@ -1,6 +1,6 @@
# Hash optimization strategies # Hash optimization strategies
In algorithm problems, **we often reduce the time complexity of algorithms by replacing linear search with hash search**. Let's use an algorithm problem to deepen understanding. 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.
!!! question !!! question
@ -8,7 +8,7 @@ In algorithm problems, **we often reduce the time complexity of algorithms by re
## Linear search: trading time for space ## Linear search: trading time for space
Consider traversing all possible combinations directly. As shown in the figure below, we initiate a two-layer loop, and in each round, we determine whether the sum of the two integers equals `target`. If so, we return their indices. Consider traversing through all possible combinations directly. As shown in the figure below, 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.
![Linear search solution for two-sum problem](replace_linear_by_hashing.assets/two_sum_brute_force.png) ![Linear search solution for two-sum problem](replace_linear_by_hashing.assets/two_sum_brute_force.png)
@ -18,11 +18,11 @@ The code is shown below:
[file]{two_sum}-[class]{}-[func]{two_sum_brute_force} [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 is 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 becomes very time-consuming with large data volumes.
## Hash search: trading space for time ## Hash search: trading space for time
Consider using a hash table, with key-value pairs being the array elements and their indices, respectively. Loop through the array, performing the steps shown in the figure below each round. 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 the figure below during each iteration.
1. Check if the number `target - nums[i]` is in the hash table. If so, directly return the indices of these two elements. 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. 2. Add the key-value pair `nums[i]` and index `i` to the hash table.
@ -42,6 +42,6 @@ The implementation code is shown below, requiring only a single loop:
[file]{two_sum}-[class]{}-[func]{two_sum_hash_table} [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, greatly improving the running efficiency. This method reduces the time complexity from $O(n^2)$ to $O(n)$ by using hash search, significantly improving the running 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**. 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**.