Skip to content

feat: add ts solution to lc problem: No.1983 #2449

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

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
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
Expand Up @@ -68,7 +68,7 @@ i和j之间的距离是j - i + 1 = 1 - 1 + 1 = 1。

我们定义一个变量 $s$ 表示当前 $nums$ 的前缀和,用一个哈希表 $d$ 保存每个前缀和第一次出现的位置。初始时 $s = 0$, $d[0] = -1$。

接下来,我们遍历数组 $nums$ 中的每个元素 $x$,计算 $s$ 的值,然后检查哈希表中是否存在 $s$,如果哈希表存在 $s$,那么说明存在一个子数组 $nums[d[s]+1,..i]$,使得子数组的和为 $0$,我们更新答案为 $max(ans, i - d[s])$。否则,我们将 $s$ 的值加入哈希表中,表示 $s$ 第一次出现的位置为 $i$。
接下来,我们遍历数组 $nums$ 中的每个元素 $x$,计算 $s$ 的值,然后检查哈希表中是否存在 $s$,如果哈希表存在 $s$,那么说明存在一个子数组 $nums[d[s]+1,..i]$,使得子数组的和为 $0$,我们更新答案为 $\max(ans, i - d[s])$。否则,我们将 $s$ 的值加入哈希表中,表示 $s$ 第一次出现的位置为 $i$。

遍历结束,即可得到最终的答案。

Expand Down Expand Up @@ -148,6 +148,25 @@ func widestPairOfIndices(nums1 []int, nums2 []int) (ans int) {
}
```

```ts
function widestPairOfIndices(nums1: number[], nums2: number[]): number {
const d: Map<number, number> = new Map();
d.set(0, -1);
const n: number = nums1.length;
let s: number = 0;
let ans: number = 0;
for (let i = 0; i < n; ++i) {
s += nums1[i] - nums2[i];
if (d.has(s)) {
ans = Math.max(ans, i - (d.get(s) as number));
} else {
d.set(s, i);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@ There are no pairs of indices that meet the requirements.

## Solutions

### Solution 1
### Solution 1: Prefix Sum + Hash Table

We observe that for any index pair $(i, j)$, if $nums1[i] + nums1[i+1] + ... + nums1[j] = nums2[i] + nums2[i+1] + ... + nums2[j]$, then $nums1[i] - nums2[i] + nums1[i+1] - nums2[i+1] + ... + nums1[j] - nums2[j] = 0$. If we subtract the corresponding elements of array $nums1$ and array $nums2$ to get a new array $nums$, the problem is transformed into finding the longest subarray in $nums$ such that the sum of the subarray is $0$. This can be solved using the prefix sum + hash table method.

We define a variable $s$ to represent the current prefix sum of $nums$, and use a hash table $d$ to store the first occurrence position of each prefix sum. Initially, $s = 0$ and $d[0] = -1$.

Next, we traverse each element $x$ in the array $nums$, calculate the value of $s$, and then check whether $s$ exists in the hash table. If $s$ exists in the hash table, it means that there is a subarray $nums[d[s]+1,..i]$ such that the sum of the subarray is $0$, and we update the answer to $\max(ans, i - d[s])$. Otherwise, we add the value of $s$ to the hash table, indicating that the first occurrence position of $s$ is $i$.

After the traversal, we can get the final answer.

The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.

<!-- tabs:start -->

Expand Down Expand Up @@ -134,6 +144,25 @@ func widestPairOfIndices(nums1 []int, nums2 []int) (ans int) {
}
```

```ts
function widestPairOfIndices(nums1: number[], nums2: number[]): number {
const d: Map<number, number> = new Map();
d.set(0, -1);
const n: number = nums1.length;
let s: number = 0;
let ans: number = 0;
for (let i = 0; i < n; ++i) {
s += nums1[i] - nums2[i];
if (d.has(s)) {
ans = Math.max(ans, i - (d.get(s) as number));
} else {
d.set(s, i);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function widestPairOfIndices(nums1: number[], nums2: number[]): number {
const d: Map<number, number> = new Map();
d.set(0, -1);
const n: number = nums1.length;
let s: number = 0;
let ans: number = 0;
for (let i = 0; i < n; ++i) {
s += nums1[i] - nums2[i];
if (d.has(s)) {
ans = Math.max(ans, i - (d.get(s) as number));
} else {
d.set(s, i);
}
}
return ans;
}