Skip to content

feat: add solutions to lc problem: No.1477 #4409

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
May 17, 2025
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 @@ -85,9 +85,9 @@ tags:

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

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

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

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

Expand Down Expand Up @@ -199,6 +199,33 @@ func minSumOfLengths(arr []int, target int) int {
}
```

#### TypeScript

```ts
function minSumOfLengths(arr: number[], target: number): number {
const d = new Map<number, number>();
d.set(0, 0);
let s = 0;
const n = arr.length;
const f: number[] = Array(n + 1);
const inf = 1 << 30;
f[0] = inf;
let ans = inf;
for (let i = 1; i <= n; ++i) {
const v = arr[i - 1];
s += v;
f[i] = f[i - 1];
if (d.has(s - target)) {
const j = d.get(s - target)!;
f[i] = Math.min(f[i], i - j);
ans = Math.min(ans, f[j] + i - j);
}
d.set(s, i);
}
return ans > n ? -1 : ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ tags:

<!-- solution:start -->

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

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$.

Define $f[i]$ as the minimum length of a subarray with sum equal to $target$ among the first $i$ elements. Initially, $f[0]=\infty$.

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.

Finally, if the answer is greater than the array length, return $-1$; otherwise, return the answer.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -176,6 +186,33 @@ func minSumOfLengths(arr []int, target int) int {
}
```

#### TypeScript

```ts
function minSumOfLengths(arr: number[], target: number): number {
const d = new Map<number, number>();
d.set(0, 0);
let s = 0;
const n = arr.length;
const f: number[] = Array(n + 1);
const inf = 1 << 30;
f[0] = inf;
let ans = inf;
for (let i = 1; i <= n; ++i) {
const v = arr[i - 1];
s += v;
f[i] = f[i - 1];
if (d.has(s - target)) {
const j = d.get(s - target)!;
f[i] = Math.min(f[i], i - j);
ans = Math.min(ans, f[j] + i - j);
}
d.set(s, i);
}
return ans > n ? -1 : ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function minSumOfLengths(arr: number[], target: number): number {
const d = new Map<number, number>();
d.set(0, 0);
let s = 0;
const n = arr.length;
const f: number[] = Array(n + 1);
const inf = 1 << 30;
f[0] = inf;
let ans = inf;
for (let i = 1; i <= n; ++i) {
const v = arr[i - 1];
s += v;
f[i] = f[i - 1];
if (d.has(s - target)) {
const j = d.get(s - target)!;
f[i] = Math.min(f[i], i - j);
ans = Math.min(ans, f[j] + i - j);
}
d.set(s, i);
}
return ans > n ? -1 : ans;
}
54 changes: 34 additions & 20 deletions solution/1400-1499/1487.Making File Names Unique/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ tags:
<p><strong>Constraints:</strong></p>

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

<!-- description:end -->
Expand All @@ -74,7 +74,21 @@ tags:

<!-- solution:start -->

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

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.

Next, we iterate through the folder names array. For each file name $name$:

- 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)$.
- If $name$ is not in $d$, we can directly add $name$ to $d$ and set $d[name]$ to $1$.
- Then, we add $name$ to the answer array and continue to the next file name.

After traversing all file names, we obtain the answer array.

> In the code implementation below, we directly modify the $names$ array without using an extra answer array.

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.

<!-- tabs:start -->

Expand Down Expand Up @@ -144,22 +158,22 @@ public:

```go
func getFolderNames(names []string) []string {
d := map[string]int{}
for i, name := range names {
if k, ok := d[name]; ok {
for {
newName := fmt.Sprintf("%s(%d)", name, k)
if d[newName] == 0 {
d[name] = k + 1
names[i] = newName
break
}
k++
}
}
d[names[i]] = 1
}
return names
d := map[string]int{}
for i, name := range names {
if k, ok := d[name]; ok {
for {
newName := fmt.Sprintf("%s(%d)", name, k)
if d[newName] == 0 {
d[name] = k + 1
names[i] = newName
break
}
k++
}
}
d[names[i]] = 1
}
return names
}
```

Expand Down