Skip to content

Commit d9999f6

Browse files
committed
feat: add solutions to lc problem: No.1477
No.1477.Find Two Non-overlapping Sub-arrays Each With Target Sum
1 parent 52f89c8 commit d9999f6

File tree

4 files changed

+123
-23
lines changed

4 files changed

+123
-23
lines changed

solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ tags:
8585

8686
我们可以使用哈希表 $d$ 记录前缀和最近一次出现的位置,初始时 $d[0]=0$。
8787

88-
定义 $f[i]$ 表示前 $i$ 个元素中,长度和为 $target$ 的最短子数组的长度。初始时 $f[0]=inf$。
88+
定义 $f[i]$ 表示前 $i$ 个元素中,长度和为 $target$ 的最短子数组的长度。初始时 $f[0]= \infty$。
8989

90-
遍历数组 `arr`,对于当前位置 $i$,计算前缀和 $s$,如果 $s-target$ 在哈希表中,记 $j=d[s-target]$,则 $f[i]=min(f[i],i-j)$,答案为 $ans=min(ans,f[j]+i-j)$。继续遍历下个位置。
90+
遍历数组 $\textit{arr}$,对于当前位置 $i$,计算前缀和 $s$,如果 $s - \textit{target}$ 在哈希表中,记 $j=d[s - \textit{target}]$,则 $f[i]=\min(f[i], i - j)$,答案为 $ans=\min(ans, f[j] + i - j)$。继续遍历下个位置。
9191

9292
最后,如果答案大于数组长度,则返回 $-1$,否则返回答案。
9393

@@ -199,6 +199,33 @@ func minSumOfLengths(arr []int, target int) int {
199199
}
200200
```
201201

202+
#### TypeScript
203+
204+
```ts
205+
function minSumOfLengths(arr: number[], target: number): number {
206+
const d = new Map<number, number>();
207+
d.set(0, 0);
208+
let s = 0;
209+
const n = arr.length;
210+
const f: number[] = Array(n + 1);
211+
const inf = 1 << 30;
212+
f[0] = inf;
213+
let ans = inf;
214+
for (let i = 1; i <= n; ++i) {
215+
const v = arr[i - 1];
216+
s += v;
217+
f[i] = f[i - 1];
218+
if (d.has(s - target)) {
219+
const j = d.get(s - target)!;
220+
f[i] = Math.min(f[i], i - j);
221+
ans = Math.min(ans, f[j] + i - j);
222+
}
223+
d.set(s, i);
224+
}
225+
return ans > n ? -1 : ans;
226+
}
227+
```
228+
202229
<!-- tabs:end -->
203230

204231
<!-- solution:end -->

solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README_EN.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,17 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Hash Table + Prefix Sum + Dynamic Programming
72+
73+
We can use a hash table $d$ to record the most recent position where each prefix sum appears, with the initial value $d[0]=0$.
74+
75+
Define $f[i]$ as the minimum length of a subarray with sum equal to $target$ among the first $i$ elements. Initially, $f[0]=\infty$.
76+
77+
Iterate through the array $\textit{arr}$. For the current position $i$, calculate the prefix sum $s$. If $s - \textit{target}$ exists in the hash table, let $j = d[s - \textit{target}]$, then $f[i] = \min(f[i], i - j)$, and the answer is $ans = \min(ans, f[j] + i - j)$. Continue to the next position.
78+
79+
Finally, if the answer is greater than the array length, return $-1$; otherwise, return the answer.
80+
81+
The complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array.
7282

7383
<!-- tabs:start -->
7484

@@ -176,6 +186,33 @@ func minSumOfLengths(arr []int, target int) int {
176186
}
177187
```
178188

189+
#### TypeScript
190+
191+
```ts
192+
function minSumOfLengths(arr: number[], target: number): number {
193+
const d = new Map<number, number>();
194+
d.set(0, 0);
195+
let s = 0;
196+
const n = arr.length;
197+
const f: number[] = Array(n + 1);
198+
const inf = 1 << 30;
199+
f[0] = inf;
200+
let ans = inf;
201+
for (let i = 1; i <= n; ++i) {
202+
const v = arr[i - 1];
203+
s += v;
204+
f[i] = f[i - 1];
205+
if (d.has(s - target)) {
206+
const j = d.get(s - target)!;
207+
f[i] = Math.min(f[i], i - j);
208+
ans = Math.min(ans, f[j] + i - j);
209+
}
210+
d.set(s, i);
211+
}
212+
return ans > n ? -1 : ans;
213+
}
214+
```
215+
179216
<!-- tabs:end -->
180217

181218
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function minSumOfLengths(arr: number[], target: number): number {
2+
const d = new Map<number, number>();
3+
d.set(0, 0);
4+
let s = 0;
5+
const n = arr.length;
6+
const f: number[] = Array(n + 1);
7+
const inf = 1 << 30;
8+
f[0] = inf;
9+
let ans = inf;
10+
for (let i = 1; i <= n; ++i) {
11+
const v = arr[i - 1];
12+
s += v;
13+
f[i] = f[i - 1];
14+
if (d.has(s - target)) {
15+
const j = d.get(s - target)!;
16+
f[i] = Math.min(f[i], i - j);
17+
ans = Math.min(ans, f[j] + i - j);
18+
}
19+
d.set(s, i);
20+
}
21+
return ans > n ? -1 : ans;
22+
}

solution/1400-1499/1487.Making File Names Unique/README_EN.md

+34-20
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ tags:
6363
<p><strong>Constraints:</strong></p>
6464

6565
<ul>
66-
<li><code>1 &lt;= names.length &lt;= 5 * 10<sup>4</sup></code></li>
67-
<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
68-
<li><code>names[i]</code> consists of lowercase English letters, digits, and/or round brackets.</li>
66+
<li><code>1 &lt;= names.length &lt;= 5 * 10<sup>4</sup></code></li>
67+
<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
68+
<li><code>names[i]</code> consists of lowercase English letters, digits, and/or round brackets.</li>
6969
</ul>
7070

7171
<!-- description:end -->
@@ -74,7 +74,21 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### Solution 1
77+
### Solution 1: Hash Table
78+
79+
We can use a hash table $d$ to record the minimum available index for each folder name, where $d[name] = k$ means the minimum available index for the folder $name$ is $k$. Initially, $d$ is empty since there are no folders.
80+
81+
Next, we iterate through the folder names array. For each file name $name$:
82+
83+
- If $name$ is already in $d$, it means the folder $name$ already exists, and we need to find a new folder name. We can keep trying $name(k)$, where $k$ starts from $d[name]$, until we find a folder name $name(k)$ that does not exist in $d$. We add $name(k)$ to $d$, update $d[name]$ to $k + 1$, and then update $name$ to $name(k)$.
84+
- If $name$ is not in $d$, we can directly add $name$ to $d$ and set $d[name]$ to $1$.
85+
- Then, we add $name$ to the answer array and continue to the next file name.
86+
87+
After traversing all file names, we obtain the answer array.
88+
89+
> In the code implementation below, we directly modify the $names$ array without using an extra answer array.
90+
91+
The complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the sum of the lengths of all file names in the $names$ array.
7892

7993
<!-- tabs:start -->
8094

@@ -144,22 +158,22 @@ public:
144158
145159
```go
146160
func getFolderNames(names []string) []string {
147-
d := map[string]int{}
148-
for i, name := range names {
149-
if k, ok := d[name]; ok {
150-
for {
151-
newName := fmt.Sprintf("%s(%d)", name, k)
152-
if d[newName] == 0 {
153-
d[name] = k + 1
154-
names[i] = newName
155-
break
156-
}
157-
k++
158-
}
159-
}
160-
d[names[i]] = 1
161-
}
162-
return names
161+
d := map[string]int{}
162+
for i, name := range names {
163+
if k, ok := d[name]; ok {
164+
for {
165+
newName := fmt.Sprintf("%s(%d)", name, k)
166+
if d[newName] == 0 {
167+
d[name] = k + 1
168+
names[i] = newName
169+
break
170+
}
171+
k++
172+
}
173+
}
174+
d[names[i]] = 1
175+
}
176+
return names
163177
}
164178
```
165179

0 commit comments

Comments
 (0)