-
Notifications
You must be signed in to change notification settings - Fork 0
3043 find the length of the longest common prefix #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kitano-kazuki
wants to merge
2
commits into
main
Choose a base branch
from
3043-find-the-length-of-the-longest-common-prefix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,248 @@ | ||
| # Step1 | ||
|
|
||
| ## アプローチ | ||
|
|
||
| * `len(arr1) = N` , `len(arr2) = M`, `len(arr1[i]) = L`とする | ||
| * `arr1`からTrie木を作っておいて, `arr2`の各要素に対してprefixとなっている文字数を測る | ||
| * 計算量: O(NL + ML) | ||
| * 実行時間: 10^4 * 10^8 / 10^6 ~= 10^6 sec | ||
| * 時間はかかりそうだけど, 他に方法が思いつかないので実装する | ||
|
|
||
| ## Code1-1 (Trie) | ||
|
|
||
| * TLEになると思ったがAC. | ||
| * 10^8は桁数としては8桁だからだ!!! | ||
|
|
||
| ```python | ||
| class Node: | ||
| def __init__(self): | ||
| self.children = [None] * 10 | ||
|
|
||
| class Trie: | ||
| def __init__(self): | ||
| self.root = Node() | ||
|
|
||
| def _get_digits(self, num: int) -> list[int]: | ||
| digits = [] | ||
| if num == 0: | ||
| digits.append(0) | ||
| else: | ||
| while num > 0: | ||
| digits.append(num % 10) | ||
| num = num // 10 | ||
|
|
||
| digits.reverse() | ||
| return digits | ||
|
|
||
|
|
||
| def add(self, num: int) -> None: | ||
| digits = self._get_digits(num) | ||
|
|
||
| node = self.root | ||
| for d in digits: | ||
| if node.children[d] is None: | ||
| node.children[d] = Node() | ||
| node = node.children[d] | ||
|
|
||
| return | ||
|
|
||
|
|
||
| def get_common_prefix_length(self, num: int) -> int: | ||
| digits = self._get_digits(num) | ||
|
|
||
| node = self.root | ||
| length = 0 | ||
| for d in digits: | ||
| if node.children[d] is None: | ||
| return length | ||
| length += 1 | ||
| node = node.children[d] | ||
|
|
||
| return length | ||
|
|
||
| class Solution: | ||
| def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int: | ||
| trie = Trie() | ||
| for num1 in arr1: | ||
| trie.add(num1) | ||
|
|
||
| longest_length = float("-inf") | ||
| for num2 in arr2: | ||
| prefix_length = trie.get_common_prefix_length(num2) | ||
| longest_length = max(longest_length, prefix_length) | ||
|
|
||
| return longest_length | ||
|
|
||
| ``` | ||
|
|
||
| # Step2 | ||
|
|
||
| ## Code2-1 (Trie) | ||
|
|
||
| ```python | ||
| class IntTrieNode: | ||
| def __init__(self): | ||
| self.children = [None] * 10 | ||
|
|
||
| class IntTrieTree: | ||
| def __init__(self): | ||
| self.root = IntTrieNode() | ||
|
|
||
| def _get_digits(self, num: int) -> list[int]: | ||
| digits = [] | ||
| if num == 0: | ||
| digits.append(0) | ||
| else: | ||
| while num > 0: | ||
| digits.append(num % 10) | ||
| num = num // 10 | ||
|
|
||
| digits.reverse() | ||
| return digits | ||
|
|
||
|
|
||
| def add(self, num: int) -> None: | ||
| digits = self._get_digits(num) | ||
|
|
||
| node = self.root | ||
| for d in digits: | ||
| if node.children[d] is None: | ||
| node.children[d] = IntTrieNode() | ||
| node = node.children[d] | ||
|
|
||
| return | ||
|
|
||
|
|
||
| def get_longest_common_prefix_length(self, num: int) -> int: | ||
| digits = self._get_digits(num) | ||
|
|
||
| node = self.root | ||
| length = 0 | ||
| for d in digits: | ||
| if node.children[d] is None: | ||
| return length | ||
| length += 1 | ||
| node = node.children[d] | ||
|
|
||
| return length | ||
|
|
||
| class Solution: | ||
| def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int: | ||
| trie_tree = IntTrieTree() | ||
|
|
||
| for num1 in arr1: | ||
| trie_tree.add(num1) | ||
|
|
||
| max_prefix_length = float("-inf") | ||
| for num2 in arr2: | ||
| prefix_length = trie_tree.get_longest_common_prefix_length(num2) | ||
| max_prefix_length = max(max_prefix_length, prefix_length) | ||
|
|
||
| return max_prefix_length | ||
|
|
||
| ``` | ||
|
|
||
| ## Code2-2 (Hash) | ||
|
|
||
| * Hashを使う方法もあるらしい | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hash を使うと書いてしまうと、ハッシュ関数を用いて prefix のハッシュ値を求めて何かを行うのかと誤解させてしまうかもしれません。集合を使うと書いたほうが、読み手にとって正確に理解できると思います。 |
||
| * `arr1`から存在しうる`prefix`をすべて`set`に追加する | ||
| * O(Nd^2) => O(N) | ||
| * `arr2`の存在しうる`prefix`が`set`にあるかどうかを探る | ||
| * O(Md^2) => O(M) | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int: | ||
| prefixes = set() | ||
|
|
||
| for num1 in arr1: | ||
| num1_str = str(num1) | ||
| for i in range(1, len(num1_str) + 1): | ||
| prefixes.add(num1_str[:i]) | ||
|
|
||
| longest_common_prefix = 0 | ||
| for num2 in arr2: | ||
| num2_str = str(num2) | ||
| for i in range(len(num2_str), 0, -1): | ||
| if num2_str[:i] not in prefixes: | ||
| continue | ||
| longest_common_prefix = max(longest_common_prefix, i) | ||
| break | ||
|
|
||
| return longest_common_prefix | ||
|
|
||
| ``` | ||
|
|
||
| # Step3 | ||
|
|
||
| ## Code3-1 (Trie) | ||
|
|
||
| * 1st: 5:26 | ||
| * 2nd: 4:22 | ||
| * 3rd: 3:29 | ||
|
|
||
| ```python | ||
|
|
||
| class Node: | ||
| def __init__(self): | ||
| self.children = [None] * 10 | ||
|
|
||
|
|
||
| class Trie: | ||
| def __init__(self): | ||
| self.root = Node() | ||
|
|
||
| def _get_digits(self, num: int) -> list[int]: | ||
| if num == 0: | ||
| return [0] | ||
|
|
||
| digits_reversed = [] | ||
| while num > 0: | ||
| digits_reversed.append(num % 10) | ||
| num //= 10 | ||
|
|
||
| return reversed(digits_reversed) | ||
|
|
||
|
|
||
| def add(self, num: int) -> None: | ||
| digits = self._get_digits(num) | ||
|
|
||
| node = self.root | ||
| for d in digits: | ||
| if node.children[d] is None: | ||
| node.children[d] = Node() | ||
| node = node.children[d] | ||
|
|
||
| return | ||
|
|
||
|
|
||
| def get_longest_common_prefix_length(self, num: int) -> int: | ||
| digits = self._get_digits(num) | ||
|
|
||
| node = self.root | ||
| prefix_length = 0 | ||
| for d in digits: | ||
| if node.children[d] is None: | ||
| return prefix_length | ||
| node = node.children[d] | ||
| prefix_length += 1 | ||
|
|
||
| return prefix_length | ||
|
|
||
|
|
||
| class Solution: | ||
| def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int: | ||
| trie = Trie() | ||
|
|
||
| for num1 in arr1: | ||
| trie.add(num1) | ||
|
|
||
| max_prefix_length = 0 | ||
| for num2 in arr2: | ||
| prefix_length = trie.get_longest_common_prefix_length(num2) | ||
| max_prefix_length = max(max_prefix_length, prefix_length) | ||
|
|
||
| return max_prefix_length | ||
|
|
||
|
|
||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分も解いてみました。 Trie 木を 2 つ作り、同時に dfs し、最大の高さを求めました。業務のコードでは、メモリを解放しないとまずいです。