Files
hello-algo/en/docs/chapter_searching/replace_linear_by_hashing.md
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
2025-12-31 07:44:52 +08:00

2.2 KiB

Hash Optimization Strategy

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`, search for two elements in the array whose "sum" equals `target`, and return their array indices. Any solution will do.

Linear Search: Trading Time for Space

Consider directly traversing all possible combinations. As shown in the figure below, we open a two-layer loop and judge in each round whether the sum of two integers equals target. If so, return their indices.

Linear search solution for two sum

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 is very time-consuming with large data volumes.

Hash-Based Search: Trading Space for Time

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 the figure below 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>" Hash table solution for two sum

=== "<2>" two_sum_hashtable_step2

=== "<3>" two_sum_hashtable_step3

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) through hash-based search, greatly improving runtime efficiency.

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.