Skip to content
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

feat: add solutions to lc problem: No.3043 #2351

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -50,24 +50,117 @@

## 解法

### 方法一
### 方法一:哈希表

我们可以使用哈希表来存储 `arr1` 中的所有数字的前缀,然后遍历 `arr2` 中的所有数字 $x$,对于每个数字 $x$,我们从最高位开始逐渐减小,判断是否存在于哈希表中,如果存在,那么我们就找到了一个公共前缀,此时更新答案即可。

时间复杂度 $O(m \times \log M + n \times \log N)$,空间复杂度 $O(m \times \log M)$。其中 $m$ 和 $n$ 分别是 `arr1` 和 `arr2` 的长度,而 $M$ 和 $N$ 分别是 `arr1` 和 `arr2` 中的最大值。

<!-- tabs:start -->

```python

class Solution:
def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
s = set()
for x in arr1:
while x:
s.add(x)
x //= 10
ans = 0
for x in arr2:
while x:
if x in s:
ans = max(ans, len(str(x)))
break
x //= 10
return ans
```

```java

class Solution {
public int longestCommonPrefix(int[] arr1, int[] arr2) {
Set<Integer> s = new HashSet<>();
for (int x : arr1) {
for (; x > 0; x /= 10) {
s.add(x);
}
}
int ans = 0;
for (int x : arr2) {
for (; x > 0; x /= 10) {
if (s.contains(x)) {
ans = Math.max(ans, String.valueOf(x).length());
break;
}
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
unordered_set<int> s;
for (int x : arr1) {
for (; x; x /= 10) {
s.insert(x);
}
}
int ans = 0;
for (int x : arr2) {
for (; x; x /= 10) {
if (s.count(x)) {
ans = max(ans, (int) log10(x) + 1);
break;
}
}
}
return ans;
}
};
```

```go
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
s := map[int]bool{}
for _, x := range arr1 {
for ; x > 0; x /= 10 {
s[x] = true
}
}
for _, x := range arr2 {
for ; x > 0; x /= 10 {
if s[x] {
ans = max(ans, int(math.Log10(float64(x)))+1)
break
}
}
}
return
}
```

```ts
function longestCommonPrefix(arr1: number[], arr2: number[]): number {
const s: Set<number> = new Set<number>();
for (let x of arr1) {
for (; x; x = (x / 10) | 0) {
s.add(x % 10);
}
}
let ans: number = 0;
for (let x of arr2) {
for (; x; x = (x / 10) | 0) {
if (s.has(x % 10)) {
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
}
}
}
return ans;
}
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -46,24 +46,117 @@ Note that common prefixes between elements of the same array do not count.

## Solutions

### Solution 1
### Solution 1: Hash Table

We can use a hash table to store all the prefixes of the numbers in `arr1`. Then, we traverse all the numbers $x$ in `arr2`. For each number $x$, we start from the highest bit and gradually decrease, checking whether it exists in the hash table. If it does, we have found a common prefix, and we can update the answer accordingly.

The time complexity is $O(m \times \log M + n \times \log N)$, and the space complexity is $O(m \times \log M)$. Here, $m$ and $n$ are the lengths of `arr1` and `arr2` respectively, and $M$ and $N$ are the maximum values in `arr1` and `arr2` respectively.

<!-- tabs:start -->

```python

class Solution:
def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
s = set()
for x in arr1:
while x:
s.add(x)
x //= 10
ans = 0
for x in arr2:
while x:
if x in s:
ans = max(ans, len(str(x)))
break
x //= 10
return ans
```

```java

class Solution {
public int longestCommonPrefix(int[] arr1, int[] arr2) {
Set<Integer> s = new HashSet<>();
for (int x : arr1) {
for (; x > 0; x /= 10) {
s.add(x);
}
}
int ans = 0;
for (int x : arr2) {
for (; x > 0; x /= 10) {
if (s.contains(x)) {
ans = Math.max(ans, String.valueOf(x).length());
break;
}
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
unordered_set<int> s;
for (int x : arr1) {
for (; x; x /= 10) {
s.insert(x);
}
}
int ans = 0;
for (int x : arr2) {
for (; x; x /= 10) {
if (s.count(x)) {
ans = max(ans, (int) log10(x) + 1);
break;
}
}
}
return ans;
}
};
```

```go
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
s := map[int]bool{}
for _, x := range arr1 {
for ; x > 0; x /= 10 {
s[x] = true
}
}
for _, x := range arr2 {
for ; x > 0; x /= 10 {
if s[x] {
ans = max(ans, int(math.Log10(float64(x)))+1)
break
}
}
}
return
}
```

```ts
function longestCommonPrefix(arr1: number[], arr2: number[]): number {
const s: Set<number> = new Set<number>();
for (let x of arr1) {
for (; x; x = (x / 10) | 0) {
s.add(x % 10);
}
}
let ans: number = 0;
for (let x of arr2) {
for (; x; x = (x / 10) | 0) {
if (s.has(x % 10)) {
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
}
}
}
return ans;
}
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
unordered_set<int> s;
for (int x : arr1) {
for (; x; x /= 10) {
s.insert(x);
}
}
int ans = 0;
for (int x : arr2) {
for (; x; x /= 10) {
if (s.count(x)) {
ans = max(ans, (int) log10(x) + 1);
break;
}
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
s := map[int]bool{}
for _, x := range arr1 {
for ; x > 0; x /= 10 {
s[x] = true
}
}
for _, x := range arr2 {
for ; x > 0; x /= 10 {
if s[x] {
ans = max(ans, int(math.Log10(float64(x)))+1)
break
}
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int longestCommonPrefix(int[] arr1, int[] arr2) {
Set<Integer> s = new HashSet<>();
for (int x : arr1) {
for (; x > 0; x /= 10) {
s.add(x);
}
}
int ans = 0;
for (int x : arr2) {
for (; x > 0; x /= 10) {
if (s.contains(x)) {
ans = Math.max(ans, String.valueOf(x).length());
break;
}
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
s = set()
for x in arr1:
while x:
s.add(x)
x //= 10
ans = 0
for x in arr2:
while x:
if x in s:
ans = max(ans, len(str(x)))
break
x //= 10
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function longestCommonPrefix(arr1: number[], arr2: number[]): number {
const s: Set<number> = new Set<number>();
for (let x of arr1) {
for (; x; x = (x / 10) | 0) {
s.add(x % 10);
}
}
let ans: number = 0;
for (let x of arr2) {
for (; x; x = (x / 10) | 0) {
if (s.has(x % 10)) {
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
}
}
}
return ans;
}