From e5d7edfdd558fe2bd2589c430c08acf42ff25627 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 25 Sep 2024 09:21:19 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.1560~1561 --- .../README.md | 42 +++++++++++++++-- .../README_EN.md | 44 ++++++++++++++++-- .../Solution.cpp | 14 ++++-- .../Solution.ts | 17 +++++++ .../README.md | 46 +++++++++---------- .../README_EN.md | 46 ++++++++++--------- .../Solution.c | 14 +++--- .../Solution.cpp | 8 ++-- .../Solution.go | 9 ++-- .../Solution.java | 5 +- .../Solution.py | 2 +- .../Solution.rs | 5 +- .../Solution.ts | 5 +- .../2300-2399/2306.Naming a Company/README.md | 43 ++--------------- .../2306.Naming a Company/README_EN.md | 45 +++--------------- .../2306.Naming a Company/Solution.ts | 28 ----------- 16 files changed, 187 insertions(+), 186 deletions(-) create mode 100644 solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.ts delete mode 100644 solution/2300-2399/2306.Naming a Company/Solution.ts diff --git a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/README.md b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/README.md index f99453ef12efd..ddf06acb0535c 100644 --- a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/README.md +++ b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/README.md @@ -69,6 +69,14 @@ tags: ### 方法一:考虑开始、结束的位置关系 +由于每个阶段的结束位置是下一个阶段的开始位置,并且每个阶段都是逆时针方向的,所以我们可以根据开始和结束的位置关系来确定每个扇区的经过次数。 + +如果 $\textit{rounds}[0] \leq \textit{rounds}[m]$,那么从 $\textit{rounds}[0]$ 开始,到 $\textit{rounds}[m]$ 结束的所有扇区经过的次数是最多的,我们可以直接返回这个区间内的所有扇区。 + +否则,从 $1$ 开始,到 $\textit{rounds}[m]$ 结束的所有扇区和从 $\textit{rounds}[0]$ 开始,到 $n$ 结束的所有扇区的并集是经过次数最多的,我们可以返回这两个区间的并集。 + +时间复杂度 $O(n)$,其中 $n$ 是扇区的个数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 + #### Python3 @@ -114,10 +122,16 @@ public: int m = rounds.size() - 1; vector ans; if (rounds[0] <= rounds[m]) { - for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i); + for (int i = rounds[0]; i <= rounds[m]; ++i) { + ans.push_back(i); + } } else { - for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i); - for (int i = rounds[0]; i <= n; ++i) ans.push_back(i); + for (int i = 1; i <= rounds[m]; ++i) { + ans.push_back(i); + } + for (int i = rounds[0]; i <= n; ++i) { + ans.push_back(i); + } } return ans; } @@ -146,6 +160,28 @@ func mostVisited(n int, rounds []int) []int { } ``` +#### TypeScript + +```ts +function mostVisited(n: number, rounds: number[]): number[] { + const ans: number[] = []; + const m = rounds.length - 1; + if (rounds[0] <= rounds[m]) { + for (let i = rounds[0]; i <= rounds[m]; ++i) { + ans.push(i); + } + } else { + for (let i = 1; i <= rounds[m]; ++i) { + ans.push(i); + } + for (let i = rounds[0]; i <= n; ++i) { + ans.push(i); + } + } + return ans; +} +``` + diff --git a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/README_EN.md b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/README_EN.md index 0e1ba33cf82ad..daf6bce342e90 100644 --- a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/README_EN.md +++ b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/README_EN.md @@ -66,7 +66,15 @@ We can see that both sectors 1 and 2 are visited twice and they are the most vis -### Solution 1 +### Solution 1: Considering the Relationship Between Start and End Positions + +Since the end position of each stage is the start position of the next stage, and each stage is in a counterclockwise direction, we can determine the number of times each sector is passed based on the relationship between the start and end positions. + +If $\textit{rounds}[0] \leq \textit{rounds}[m]$, then the sectors from $\textit{rounds}[0]$ to $\textit{rounds}[m]$ are passed the most times, and we can directly return all sectors within this interval. + +Otherwise, the sectors from $1$ to $\textit{rounds}[m]$ and the sectors from $\textit{rounds}[0]$ to $n$ form the union of the most passed sectors, and we can return the union of these two intervals. + +The time complexity is $O(n)$, where $n$ is the number of sectors. Ignoring the space consumption of the answer array, the space complexity is $O(1)$. @@ -113,10 +121,16 @@ public: int m = rounds.size() - 1; vector ans; if (rounds[0] <= rounds[m]) { - for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i); + for (int i = rounds[0]; i <= rounds[m]; ++i) { + ans.push_back(i); + } } else { - for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i); - for (int i = rounds[0]; i <= n; ++i) ans.push_back(i); + for (int i = 1; i <= rounds[m]; ++i) { + ans.push_back(i); + } + for (int i = rounds[0]; i <= n; ++i) { + ans.push_back(i); + } } return ans; } @@ -145,6 +159,28 @@ func mostVisited(n int, rounds []int) []int { } ``` +#### TypeScript + +```ts +function mostVisited(n: number, rounds: number[]): number[] { + const ans: number[] = []; + const m = rounds.length - 1; + if (rounds[0] <= rounds[m]) { + for (let i = rounds[0]; i <= rounds[m]; ++i) { + ans.push(i); + } + } else { + for (let i = 1; i <= rounds[m]; ++i) { + ans.push(i); + } + for (let i = rounds[0]; i <= n; ++i) { + ans.push(i); + } + } + return ans; +} +``` + diff --git a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp index 32cea27587cd8..2d1219a0eebaa 100644 --- a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp +++ b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp @@ -4,11 +4,17 @@ class Solution { int m = rounds.size() - 1; vector ans; if (rounds[0] <= rounds[m]) { - for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i); + for (int i = rounds[0]; i <= rounds[m]; ++i) { + ans.push_back(i); + } } else { - for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i); - for (int i = rounds[0]; i <= n; ++i) ans.push_back(i); + for (int i = 1; i <= rounds[m]; ++i) { + ans.push_back(i); + } + for (int i = rounds[0]; i <= n; ++i) { + ans.push_back(i); + } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.ts b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.ts new file mode 100644 index 0000000000000..4aef02a74ae68 --- /dev/null +++ b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.ts @@ -0,0 +1,17 @@ +function mostVisited(n: number, rounds: number[]): number[] { + const ans: number[] = []; + const m = rounds.length - 1; + if (rounds[0] <= rounds[m]) { + for (let i = rounds[0]; i <= rounds[m]; ++i) { + ans.push(i); + } + } else { + for (let i = 1; i <= rounds[m]; ++i) { + ans.push(i); + } + for (let i = rounds[0]; i <= n; ++i) { + ans.push(i); + } + } + return ans; +} diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/README.md b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/README.md index 4addb84130633..1ce268e16b21c 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/README.md +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/README.md @@ -76,9 +76,11 @@ tags: -### 方法一:贪心 +### 方法一:贪心 + 排序 -Bob 取走最小的 1/3,剩余的硬币堆由 Alice 和我按硬币数从高到低依次取走每一堆。 +为了让我们获得的硬币数量最多,我们可以贪心地让 Bob 拿走最少的 $n$ 堆硬币。我们每次先让 Alice 拿走最多的一堆硬币,然后让我们拿走第二多的一堆硬币,依次循环,直到没有硬币可拿。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是硬币堆数。 @@ -88,18 +90,17 @@ Bob 取走最小的 1/3,剩余的硬币堆由 Alice 和我按硬币数从高 class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() - return sum(piles[-2 : len(piles) // 3 - 1 : -2]) + return sum(piles[len(piles) // 3 :][::2]) ``` #### Java ```java class Solution { - public int maxCoins(int[] piles) { Arrays.sort(piles); int ans = 0; - for (int i = piles.length - 2; i >= piles.length / 3; i -= 2) { + for (int i = piles.length / 3; i < piles.length; i += 2) { ans += piles[i]; } return ans; @@ -113,9 +114,11 @@ class Solution { class Solution { public: int maxCoins(vector& piles) { - sort(piles.begin(), piles.end()); + ranges::sort(piles); int ans = 0; - for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i]; + for (int i = piles.size() / 3; i < piles.size(); i += 2) { + ans += piles[i]; + } return ans; } }; @@ -124,13 +127,12 @@ public: #### Go ```go -func maxCoins(piles []int) int { +func maxCoins(piles []int) (ans int) { sort.Ints(piles) - ans, n := 0, len(piles) - for i := n - 2; i >= n/3; i -= 2 { + for i := len(piles) / 3; i < len(piles); i += 2 { ans += piles[i] } - return ans + return } ``` @@ -139,10 +141,9 @@ func maxCoins(piles []int) int { ```ts function maxCoins(piles: number[]): number { piles.sort((a, b) => a - b); - const n = piles.length; let ans = 0; - for (let i = 1; i <= Math.floor(n / 3); i++) { - ans += piles[n - 2 * i]; + for (let i = piles.length / 3; i < piles.length; i += 2) { + ans += piles[i]; } return ans; } @@ -154,10 +155,9 @@ function maxCoins(piles: number[]): number { impl Solution { pub fn max_coins(mut piles: Vec) -> i32 { piles.sort(); - let n = piles.len(); let mut ans = 0; - for i in 1..=n / 3 { - ans += piles[n - 2 * i]; + for i in (piles.len() / 3..piles.len()).step_by(2) { + ans += piles[i]; } ans } @@ -167,16 +167,16 @@ impl Solution { #### C ```c -int cmp(const void* a, const void* b) { - return *(int*) a - *(int*) b; +int compare(const void* a, const void* b) { + return (*(int*) a - *(int*) b); } int maxCoins(int* piles, int pilesSize) { - qsort(piles, pilesSize, sizeof(int), cmp); + qsort(piles, pilesSize, sizeof(int), compare); int ans = 0; - for (int i = 1; i <= pilesSize / 3; i++) { - ans += piles[pilesSize - 2 * i]; - }; + for (int i = pilesSize / 3; i < pilesSize; i += 2) { + ans += piles[i]; + } return ans; } ``` diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/README_EN.md b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/README_EN.md index cdae8d6f3af99..f84cd5b2dfd4e 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/README_EN.md +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/README_EN.md @@ -77,7 +77,11 @@ On the other hand if we choose this arrangement (1, 2, 8), (2, -### Solution 1 +### Solution 1: Greedy + Sorting + +To maximize the number of coins we get, we can greedily let Bob take the smallest $n$ piles of coins. Each time, we let Alice take the largest pile of coins, then we take the second largest pile of coins, and so on, until there are no more coins to take. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of piles of coins. @@ -87,18 +91,17 @@ On the other hand if we choose this arrangement (1, 2, 8), (2, class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() - return sum(piles[-2 : len(piles) // 3 - 1 : -2]) + return sum(piles[len(piles) // 3 :][::2]) ``` #### Java ```java class Solution { - public int maxCoins(int[] piles) { Arrays.sort(piles); int ans = 0; - for (int i = piles.length - 2; i >= piles.length / 3; i -= 2) { + for (int i = piles.length / 3; i < piles.length; i += 2) { ans += piles[i]; } return ans; @@ -112,9 +115,11 @@ class Solution { class Solution { public: int maxCoins(vector& piles) { - sort(piles.begin(), piles.end()); + ranges::sort(piles); int ans = 0; - for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i]; + for (int i = piles.size() / 3; i < piles.size(); i += 2) { + ans += piles[i]; + } return ans; } }; @@ -123,13 +128,12 @@ public: #### Go ```go -func maxCoins(piles []int) int { +func maxCoins(piles []int) (ans int) { sort.Ints(piles) - ans, n := 0, len(piles) - for i := n - 2; i >= n/3; i -= 2 { + for i := len(piles) / 3; i < len(piles); i += 2 { ans += piles[i] } - return ans + return } ``` @@ -138,10 +142,9 @@ func maxCoins(piles []int) int { ```ts function maxCoins(piles: number[]): number { piles.sort((a, b) => a - b); - const n = piles.length; let ans = 0; - for (let i = 1; i <= Math.floor(n / 3); i++) { - ans += piles[n - 2 * i]; + for (let i = piles.length / 3; i < piles.length; i += 2) { + ans += piles[i]; } return ans; } @@ -153,10 +156,9 @@ function maxCoins(piles: number[]): number { impl Solution { pub fn max_coins(mut piles: Vec) -> i32 { piles.sort(); - let n = piles.len(); let mut ans = 0; - for i in 1..=n / 3 { - ans += piles[n - 2 * i]; + for i in (piles.len() / 3..piles.len()).step_by(2) { + ans += piles[i]; } ans } @@ -166,16 +168,16 @@ impl Solution { #### C ```c -int cmp(const void* a, const void* b) { - return *(int*) a - *(int*) b; +int compare(const void* a, const void* b) { + return (*(int*) a - *(int*) b); } int maxCoins(int* piles, int pilesSize) { - qsort(piles, pilesSize, sizeof(int), cmp); + qsort(piles, pilesSize, sizeof(int), compare); int ans = 0; - for (int i = 1; i <= pilesSize / 3; i++) { - ans += piles[pilesSize - 2 * i]; - }; + for (int i = pilesSize / 3; i < pilesSize; i += 2) { + ans += piles[i]; + } return ans; } ``` diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.c b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.c index 03c4ca746fd4f..a8f6df9a259f3 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.c +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.c @@ -1,12 +1,12 @@ -int cmp(const void* a, const void* b) { - return *(int*) a - *(int*) b; +int compare(const void* a, const void* b) { + return (*(int*) a - *(int*) b); } int maxCoins(int* piles, int pilesSize) { - qsort(piles, pilesSize, sizeof(int), cmp); + qsort(piles, pilesSize, sizeof(int), compare); int ans = 0; - for (int i = 1; i <= pilesSize / 3; i++) { - ans += piles[pilesSize - 2 * i]; - }; + for (int i = pilesSize / 3; i < pilesSize; i += 2) { + ans += piles[i]; + } return ans; -} \ No newline at end of file +} diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.cpp b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.cpp index 4da1c57716f10..ea3b3ba6fee8a 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.cpp +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.cpp @@ -1,9 +1,11 @@ class Solution { public: int maxCoins(vector& piles) { - sort(piles.begin(), piles.end()); + ranges::sort(piles); int ans = 0; - for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i]; + for (int i = piles.size() / 3; i < piles.size(); i += 2) { + ans += piles[i]; + } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.go b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.go index f6b94a1c22a26..3afe054b26b13 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.go +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.go @@ -1,8 +1,7 @@ -func maxCoins(piles []int) int { +func maxCoins(piles []int) (ans int) { sort.Ints(piles) - ans, n := 0, len(piles) - for i := n - 2; i >= n/3; i -= 2 { + for i := len(piles) / 3; i < len(piles); i += 2 { ans += piles[i] } - return ans -} \ No newline at end of file + return +} diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.java b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.java index 37427886c5322..66674d9378a62 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.java +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.java @@ -1,11 +1,10 @@ class Solution { - public int maxCoins(int[] piles) { Arrays.sort(piles); int ans = 0; - for (int i = piles.length - 2; i >= piles.length / 3; i -= 2) { + for (int i = piles.length / 3; i < piles.length; i += 2) { ans += piles[i]; } return ans; } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.py b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.py index 4acd66f1aba0c..c8113dec93482 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.py +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.py @@ -1,4 +1,4 @@ class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() - return sum(piles[-2 : len(piles) // 3 - 1 : -2]) + return sum(piles[len(piles) // 3 :][::2]) diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.rs b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.rs index cca8213b54801..fd259f82e37b7 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.rs +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.rs @@ -1,10 +1,9 @@ impl Solution { pub fn max_coins(mut piles: Vec) -> i32 { piles.sort(); - let n = piles.len(); let mut ans = 0; - for i in 1..=n / 3 { - ans += piles[n - 2 * i]; + for i in (piles.len() / 3..piles.len()).step_by(2) { + ans += piles[i]; } ans } diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.ts b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.ts index e1b929cfc6e28..3149a184879c3 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.ts +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.ts @@ -1,9 +1,8 @@ function maxCoins(piles: number[]): number { piles.sort((a, b) => a - b); - const n = piles.length; let ans = 0; - for (let i = 1; i <= Math.floor(n / 3); i++) { - ans += piles[n - 2 * i]; + for (let i = piles.length / 3; i < piles.length; i += 2) { + ans += piles[i]; } return ans; } diff --git a/solution/2300-2399/2306.Naming a Company/README.md b/solution/2300-2399/2306.Naming a Company/README.md index c56b0c39063f3..8df5ab83fb3c1 100644 --- a/solution/2300-2399/2306.Naming a Company/README.md +++ b/solution/2300-2399/2306.Naming a Company/README.md @@ -80,15 +80,15 @@ tags: ### 方法一:枚举计数 -我们定义 $f[i][j]$ 表示 $ideas$ 中以第 $i$ 个字母开头,替换为第 $j$ 个字母后,不在 $ideas$ 中的字符串的个数。初始时 $f[i][j] = 0$。另外,用一个哈希表 $s$ 记录 $ideas$ 中的字符串,方便我们开快速判断某个字符串是否在 $ideas$ 中。 +我们定义 $f[i][j]$ 表示 $\textit{ideas}$ 中以第 $i$ 个字母开头,替换为第 $j$ 个字母后,不在 $\textit{ideas}$ 中的字符串的个数。初始时 $f[i][j] = 0$。另外,用一个哈希表 $s$ 记录 $\textit{ideas}$ 中的字符串,方便我们快速判断某个字符串是否在 $\textit{ideas}$ 中。 -接下来,我们遍历 $ideas$ 中字符串,对于当前遍历到的字符串 $v$,我们枚举替换后的第一个字母 $j$,如果 $v$ 替换后的字符串不在 $ideas$ 中,那么我们就更新 $f[i][j] = f[i][j] + 1$。 +接下来,我们遍历 $\textit{ideas}$ 中字符串,对于当前遍历到的字符串 $v$,我们枚举替换后的第一个字母 $j$,如果 $v$ 替换后的字符串不在 $\textit{ideas}$ 中,那么我们就更新 $f[i][j] = f[i][j] + 1$。 -最后,我们再次遍历 $ideas$ 中字符串,对于当前遍历到的字符串 $v$,我们枚举替换后的第一个字母 $j$,如果 $v$ 替换后的字符串不在 $ideas$ 中,那么我们就更新答案 $ans = ans + f[j][i]$。 +最后,我们再次遍历 $\textit{ideas}$ 中字符串,对于当前遍历到的字符串 $v$,我们枚举替换后的第一个字母 $j$,如果 $v$ 替换后的字符串不在 $\textit{ideas}$ 中,那么我们就更新答案 $\textit{ans} = \textit{ans} + f[j][i]$。 -最终答案即为 $ans$。 +最终答案即为 $\textit{ans}$。 -时间复杂度 $O(n \times m \times |\Sigma|)$,空间复杂度 $O(|\Sigma|^2)$。其中 $n$ 和 $m$ 分别是 $ideas$ 中字符串的个数和字符串的最大长度,而 $|\Sigma|$ 是字符串中出现的字符集,本题中 $|\Sigma| \leq 26$。 +时间复杂度 $O(n \times m \times |\Sigma|)$,空间复杂度 $O(|\Sigma|^2)$。其中 $n$ 和 $m$ 分别是 $\textit{ideas}$ 中字符串的个数和字符串的最大长度,而 $|\Sigma|$ 是字符串中出现的字符集,本题中 $|\Sigma| \leq 26$。 @@ -219,39 +219,6 @@ func distinctNames(ideas []string) (ans int64) { } ``` -#### TypeScript - -```ts -function distinctNames(ideas: string[]): number { - const s = new Set(ideas); - const f: number[][] = Array(26) - .fill(0) - .map(() => Array(26).fill(0)); - for (const v of s) { - const i = v.charCodeAt(0) - 'a'.charCodeAt(0); - const t = [...v]; - for (let j = 0; j < 26; ++j) { - t[0] = String.fromCharCode('a'.charCodeAt(0) + j); - if (!s.has(t.join(''))) { - f[i][j]++; - } - } - } - let ans = 0; - for (const v of s) { - const i = v.charCodeAt(0) - 'a'.charCodeAt(0); - const t = [...v]; - for (let j = 0; j < 26; ++j) { - t[0] = String.fromCharCode('a'.charCodeAt(0) + j); - if (!s.has(t.join(''))) { - ans += f[j][i]; - } - } - } - return ans; -} -``` - diff --git a/solution/2300-2399/2306.Naming a Company/README_EN.md b/solution/2300-2399/2306.Naming a Company/README_EN.md index 9d0016530900a..96af6032c5b0c 100644 --- a/solution/2300-2399/2306.Naming a Company/README_EN.md +++ b/solution/2300-2399/2306.Naming a Company/README_EN.md @@ -78,17 +78,17 @@ The following are some examples of invalid selections: -### Solution 1: Enumeration Counting +### Solution 1: Enumeration and Counting -We define $f[i][j]$ to represent the number of strings in $ideas$ that start with the $i$th letter and are not in $ideas$ after being replaced with the $j$th letter. Initially, $f[i][j] = 0$. Additionally, we use a hash table $s$ to record the strings in $ideas$, which allows us to quickly determine whether a string is in $ideas$. +We define $f[i][j]$ to represent the number of strings in $\textit{ideas}$ that start with the $i$-th letter and, when replaced with the $j$-th letter, do not exist in $\textit{ideas}$. Initially, $f[i][j] = 0$. Additionally, we use a hash table $s$ to record the strings in $\textit{ideas}$, allowing us to quickly determine whether a string is in $\textit{ideas}$. -Next, we traverse the strings in $ideas$. For the current string $v$, we enumerate the first letter $j$ after replacement. If the string after $v$ is replaced is not in $ideas$, then we update $f[i][j] = f[i][j] + 1$. +Next, we traverse the strings in $\textit{ideas}$. For the current string $v$, we enumerate the first letter $j$ after replacement. If the string obtained by replacing $v$ is not in $\textit{ideas}$, we update $f[i][j] = f[i][j] + 1$. -Finally, we traverse the strings in $ideas$ again. For the current string $v$, we enumerate the first letter $j$ after replacement. If the string after $v$ is replaced is not in $ideas$, then we update the answer $ans = ans + f[j][i]$. +Finally, we traverse the strings in $\textit{ideas}$ again. For the current string $v$, we enumerate the first letter $j$ after replacement. If the string obtained by replacing $v$ is not in $\textit{ideas}$, we update the answer $\textit{ans} = \textit{ans} + f[j][i]$. -The final answer is $ans$. +The final answer is $\textit{ans}$. -The time complexity is $O(n \times m \times |\Sigma|)$, and the space complexity is $O(|\Sigma|^2)$. Here, $n$ and $m$ are the number of strings in $ideas$ and the maximum length of the strings, respectively, and $|\Sigma|$ is the character set that appears in the string. In this problem, $|\Sigma| \leq 26$. +The time complexity is $O(n \times m \times |\Sigma|)$, and the space complexity is $O(|\Sigma|^2)$. Here, $n$ and $m$ are the number of strings in $\textit{ideas}$ and the maximum length of the strings, respectively, and $|\Sigma|$ is the character set of the strings, with $|\Sigma| \leq 26$ in this problem. @@ -219,39 +219,6 @@ func distinctNames(ideas []string) (ans int64) { } ``` -#### TypeScript - -```ts -function distinctNames(ideas: string[]): number { - const s = new Set(ideas); - const f: number[][] = Array(26) - .fill(0) - .map(() => Array(26).fill(0)); - for (const v of s) { - const i = v.charCodeAt(0) - 'a'.charCodeAt(0); - const t = [...v]; - for (let j = 0; j < 26; ++j) { - t[0] = String.fromCharCode('a'.charCodeAt(0) + j); - if (!s.has(t.join(''))) { - f[i][j]++; - } - } - } - let ans = 0; - for (const v of s) { - const i = v.charCodeAt(0) - 'a'.charCodeAt(0); - const t = [...v]; - for (let j = 0; j < 26; ++j) { - t[0] = String.fromCharCode('a'.charCodeAt(0) + j); - if (!s.has(t.join(''))) { - ans += f[j][i]; - } - } - } - return ans; -} -``` - diff --git a/solution/2300-2399/2306.Naming a Company/Solution.ts b/solution/2300-2399/2306.Naming a Company/Solution.ts deleted file mode 100644 index 3dfe1e5d41b13..0000000000000 --- a/solution/2300-2399/2306.Naming a Company/Solution.ts +++ /dev/null @@ -1,28 +0,0 @@ -function distinctNames(ideas: string[]): number { - const s = new Set(ideas); - const f: number[][] = Array(26) - .fill(0) - .map(() => Array(26).fill(0)); - for (const v of s) { - const i = v.charCodeAt(0) - 'a'.charCodeAt(0); - const t = [...v]; - for (let j = 0; j < 26; ++j) { - t[0] = String.fromCharCode('a'.charCodeAt(0) + j); - if (!s.has(t.join(''))) { - f[i][j]++; - } - } - } - let ans = 0; - for (const v of s) { - const i = v.charCodeAt(0) - 'a'.charCodeAt(0); - const t = [...v]; - for (let j = 0; j < 26; ++j) { - t[0] = String.fromCharCode('a'.charCodeAt(0) + j); - if (!s.has(t.join(''))) { - ans += f[j][i]; - } - } - } - return ans; -}