diff --git a/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README.md b/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README.md index f02ab74dd4299..c96b41716e53e 100644 --- a/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README.md +++ b/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README.md @@ -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$,否则返回答案。 @@ -199,6 +199,33 @@ func minSumOfLengths(arr []int, target int) int { } ``` +#### TypeScript + +```ts +function minSumOfLengths(arr: number[], target: number): number { + const d = new Map(); + 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; +} +``` + diff --git a/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README_EN.md b/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README_EN.md index 74b866b75256c..ab81135d97097 100644 --- a/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README_EN.md +++ b/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/README_EN.md @@ -68,7 +68,17 @@ tags: -### 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. @@ -176,6 +186,33 @@ func minSumOfLengths(arr []int, target int) int { } ``` +#### TypeScript + +```ts +function minSumOfLengths(arr: number[], target: number): number { + const d = new Map(); + 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; +} +``` + diff --git a/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/Solution.ts b/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/Solution.ts new file mode 100644 index 0000000000000..cb28fda619329 --- /dev/null +++ b/solution/1400-1499/1477.Find Two Non-overlapping Sub-arrays Each With Target Sum/Solution.ts @@ -0,0 +1,22 @@ +function minSumOfLengths(arr: number[], target: number): number { + const d = new Map(); + 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; +} diff --git a/solution/1400-1499/1487.Making File Names Unique/README_EN.md b/solution/1400-1499/1487.Making File Names Unique/README_EN.md index da002d0577b6c..471de05c8dbb7 100644 --- a/solution/1400-1499/1487.Making File Names Unique/README_EN.md +++ b/solution/1400-1499/1487.Making File Names Unique/README_EN.md @@ -63,9 +63,9 @@ tags:

Constraints:

    -
  • 1 <= names.length <= 5 * 104
  • -
  • 1 <= names[i].length <= 20
  • -
  • names[i] consists of lowercase English letters, digits, and/or round brackets.
  • +
  • 1 <= names.length <= 5 * 104
  • +
  • 1 <= names[i].length <= 20
  • +
  • names[i] consists of lowercase English letters, digits, and/or round brackets.
@@ -74,7 +74,21 @@ tags: -### 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. @@ -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 } ```