diff --git a/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md b/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md index e3b471d11db24..8a2a10385c29e 100644 --- a/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md +++ b/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md @@ -73,11 +73,11 @@ tags: ### 方法一:栈 -我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 `"abc"`,所以每次插入操作之后,字符串的长度都会增加 $3$。如果字符串 $s$ 有效,那么它的长度一定是 $3$ 的倍数。因此,我们先对字符串 $s$ 的长度进行判断,如果不是 $3$ 的倍数,那么 $s$ 一定无效,可以直接返回 `false`。 +我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 $\textit{"abc"}$,所以每次插入操作之后,字符串的长度都会增加 $3$。如果字符串 $s$ 有效,那么它的长度一定是 $3$ 的倍数。因此,我们先对字符串 $s$ 的长度进行判断,如果不是 $3$ 的倍数,那么 $s$ 一定无效,可以直接返回 $\textit{false}$。 -接下来我们遍历字符串 $s$ 的每个字符 $c$,我们先将字符 $c$ 压入栈 $t$ 中。如果此时栈 $t$ 的长度大于等于 $3$,并且栈顶的三个元素组成了字符串 `"abc"`,那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 $s$ 的下一个字符。 +接下来我们遍历字符串 $s$ 的每个字符 $c$,我们先将字符 $c$ 压入栈 $t$ 中。如果此时栈 $t$ 的长度大于等于 $3$,并且栈顶的三个元素组成了字符串 $\textit{"abc"}$,那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 $s$ 的下一个字符。 -遍历结束之后,如果栈 $t$ 为空,那么说明字符串 $s$ 有效,返回 `true`,否则返回 `false`。 +遍历结束之后,如果栈 $t$ 为空,那么说明字符串 $s$ 有效,返回 $\textit{true}$;否则,返回 $\textit{false}$。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。 diff --git a/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md b/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md index 448573b651d0d..e16aa8b35cebd 100644 --- a/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md +++ b/solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md @@ -73,13 +73,13 @@ Thus, "abcabcababcc" is valid. ### Solution 1: Stack -If the string is valid, it's length must be the multiple of $3$. +We observe the operations in the problem and find that each time a string $\textit{"abc"}$ is inserted at any position in the string. Therefore, after each insertion operation, the length of the string increases by $3$. If the string $s$ is valid, its length must be a multiple of $3$. Thus, we first check the length of the string $s$. If it is not a multiple of $3$, then $s$ must be invalid, and we can directly return $\textit{false}$. -We traverse the string and push every character into the stack $t$. If the size of stack $t$ is greater than or equal to $3$ and the top three elements of stack $t$ constitute the string `"abc"`, we pop the top three elements. Then we continue to traverse the next character of the string $s$. +Next, we traverse each character $c$ in the string $s$. We first push the character $c$ onto the stack $t$. If the length of the stack $t$ is greater than or equal to $3$, and the top three elements of the stack form the string $\textit{"abc"}$, then we pop the top three elements from the stack. We then continue to traverse the next character in the string $s$. -When the traversal is over, if the stack $t$ is empty, the string $s$ is valid, return `true`, otherwise return `false`. +After the traversal, if the stack $t$ is empty, it means the string $s$ is valid, and we return $\textit{true}$; otherwise, we return $\textit{false}$. -The time complexity is $O(n)$ and the space complexity is $O(n)$. Where $n$ is the length of the string $s$. +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$. diff --git a/solution/1300-1399/1395.Count Number of Teams/README.md b/solution/1300-1399/1395.Count Number of Teams/README.md index 12b866f755861..13ac64ce256e9 100644 --- a/solution/1300-1399/1395.Count Number of Teams/README.md +++ b/solution/1300-1399/1395.Count Number of Teams/README.md @@ -527,4 +527,108 @@ function numTeams(rating: number[]): number { + + +### 方法三:记忆化搜索 + + + +#### TypeScript + +```ts +function numTeams(rating: number[]): number { + const n = rating.length; + const f: Record = { + asc: Array.from({ length: n }, () => Array(3).fill(-1)), + desc: Array.from({ length: n }, () => Array(3).fill(-1)), + }; + + const fn = (i: number, available: number, type: Type) => { + if (!available) { + return 1; + } + if (f[type][i][available] !== -1) { + return f[type][i][available]; + } + + let ans = 0; + for (let j = i + 1; j < n; j++) { + if (rating[j] > rating[i]) { + if (type === 'asc') { + ans += fn(j, available - 1, 'asc'); + } + } else { + if (type === 'desc') { + ans += fn(j, available - 1, 'desc'); + } + } + } + f[type][i][available] = ans; + + return ans; + }; + + let ans = 0; + for (let i = 0; i < n; i++) { + ans += fn(i, 2, 'asc') + fn(i, 2, 'desc'); + } + + return ans; +} + +type Type = 'asc' | 'desc'; +``` + +#### JavaScript + +```js +/** + * @param {number[]} rating + * @return {number} + */ +var numTeams = function (rating) { + const n = rating.length; + const f = { + asc: Array.from({ length: n }, () => Array(3).fill(-1)), + desc: Array.from({ length: n }, () => Array(3).fill(-1)), + }; + + const fn = (i, available, type) => { + if (!available) { + return 1; + } + if (f[type][i][available] !== -1) { + return f[type][i][available]; + } + + let ans = 0; + for (let j = i + 1; j < n; j++) { + if (rating[j] > rating[i]) { + if (type === 'asc') { + ans += fn(j, available - 1, 'asc'); + } + } else { + if (type === 'desc') { + ans += fn(j, available - 1, 'desc'); + } + } + } + f[type][i][available] = ans; + + return ans; + }; + + let ans = 0; + for (let i = 0; i < n; i++) { + ans += fn(i, 2, 'asc') + fn(i, 2, 'desc'); + } + + return ans; +}; +``` + + + + + diff --git a/solution/1300-1399/1395.Count Number of Teams/README_EN.md b/solution/1300-1399/1395.Count Number of Teams/README_EN.md index bb1d7b846fc65..bf4b79c3da5a9 100644 --- a/solution/1300-1399/1395.Count Number of Teams/README_EN.md +++ b/solution/1300-1399/1395.Count Number of Teams/README_EN.md @@ -37,7 +37,7 @@ tags:
 Input: rating = [2,5,3,4,1]
 Output: 3
-Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). 
+Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
 

Example 2:

@@ -525,4 +525,108 @@ function numTeams(rating: number[]): number { + + +### Solution 3: Recursion + Memoization + + + +#### TypeScript + +```ts +function numTeams(rating: number[]): number { + const n = rating.length; + const f: Record = { + asc: Array.from({ length: n }, () => Array(3).fill(-1)), + desc: Array.from({ length: n }, () => Array(3).fill(-1)), + }; + + const fn = (i: number, available: number, type: Type) => { + if (!available) { + return 1; + } + if (f[type][i][available] !== -1) { + return f[type][i][available]; + } + + let ans = 0; + for (let j = i + 1; j < n; j++) { + if (rating[j] > rating[i]) { + if (type === 'asc') { + ans += fn(j, available - 1, 'asc'); + } + } else { + if (type === 'desc') { + ans += fn(j, available - 1, 'desc'); + } + } + } + f[type][i][available] = ans; + + return ans; + }; + + let ans = 0; + for (let i = 0; i < n; i++) { + ans += fn(i, 2, 'asc') + fn(i, 2, 'desc'); + } + + return ans; +} + +type Type = 'asc' | 'desc'; +``` + +#### JavaScript + +```js +/** + * @param {number[]} rating + * @return {number} + */ +var numTeams = function (rating) { + const n = rating.length; + const f = { + asc: Array.from({ length: n }, () => Array(3).fill(-1)), + desc: Array.from({ length: n }, () => Array(3).fill(-1)), + }; + + const fn = (i, available, type) => { + if (!available) { + return 1; + } + if (f[type][i][available] !== -1) { + return f[type][i][available]; + } + + let ans = 0; + for (let j = i + 1; j < n; j++) { + if (rating[j] > rating[i]) { + if (type === 'asc') { + ans += fn(j, available - 1, 'asc'); + } + } else { + if (type === 'desc') { + ans += fn(j, available - 1, 'desc'); + } + } + } + f[type][i][available] = ans; + + return ans; + }; + + let ans = 0; + for (let i = 0; i < n; i++) { + ans += fn(i, 2, 'asc') + fn(i, 2, 'desc'); + } + + return ans; +}; +``` + + + + + diff --git a/solution/1300-1399/1395.Count Number of Teams/Solution3.js b/solution/1300-1399/1395.Count Number of Teams/Solution3.js new file mode 100644 index 0000000000000..46afa695a39a8 --- /dev/null +++ b/solution/1300-1399/1395.Count Number of Teams/Solution3.js @@ -0,0 +1,43 @@ +/** + * @param {number[]} rating + * @return {number} + */ +var numTeams = function (rating) { + const n = rating.length; + const f = { + asc: Array.from({ length: n }, () => Array(3).fill(-1)), + desc: Array.from({ length: n }, () => Array(3).fill(-1)), + }; + + const fn = (i, available, type) => { + if (!available) { + return 1; + } + if (f[type][i][available] !== -1) { + return f[type][i][available]; + } + + let ans = 0; + for (let j = i + 1; j < n; j++) { + if (rating[j] > rating[i]) { + if (type === 'asc') { + ans += fn(j, available - 1, 'asc'); + } + } else { + if (type === 'desc') { + ans += fn(j, available - 1, 'desc'); + } + } + } + f[type][i][available] = ans; + + return ans; + }; + + let ans = 0; + for (let i = 0; i < n; i++) { + ans += fn(i, 2, 'asc') + fn(i, 2, 'desc'); + } + + return ans; +}; diff --git a/solution/1300-1399/1395.Count Number of Teams/Solution3.ts b/solution/1300-1399/1395.Count Number of Teams/Solution3.ts new file mode 100644 index 0000000000000..34e3bcfc59e85 --- /dev/null +++ b/solution/1300-1399/1395.Count Number of Teams/Solution3.ts @@ -0,0 +1,41 @@ +function numTeams(rating: number[]): number { + const n = rating.length; + const f: Record = { + asc: Array.from({ length: n }, () => Array(3).fill(-1)), + desc: Array.from({ length: n }, () => Array(3).fill(-1)), + }; + + const fn = (i: number, available: number, type: Type) => { + if (!available) { + return 1; + } + if (f[type][i][available] !== -1) { + return f[type][i][available]; + } + + let ans = 0; + for (let j = i + 1; j < n; j++) { + if (rating[j] > rating[i]) { + if (type === 'asc') { + ans += fn(j, available - 1, 'asc'); + } + } else { + if (type === 'desc') { + ans += fn(j, available - 1, 'desc'); + } + } + } + f[type][i][available] = ans; + + return ans; + }; + + let ans = 0; + for (let i = 0; i < n; i++) { + ans += fn(i, 2, 'asc') + fn(i, 2, 'desc'); + } + + return ans; +} + +type Type = 'asc' | 'desc'; diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md index 68056afc595f9..8833573943b9c 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md @@ -47,7 +47,7 @@ tags: - 2 个字符串的前缀为 "a" ,2 个字符串的前缀为 "ab" 。 总计 answer[1] = 2 + 2 = 4 。 - "bc" 有 2 个前缀:"b" 和 "bc" 。 -- 2 个字符串的前缀为 "b" ,1 个字符串的前缀为 "bc" 。 +- 2 个字符串的前缀为 "b" ,1 个字符串的前缀为 "bc" 。 总计 answer[2] = 2 + 1 = 3 。 - "b" 有 1 个前缀:"b"。 - 2 个字符串的前缀为 "b" 。 @@ -81,11 +81,21 @@ tags: ### 方法一:前缀树 -用前缀树维护所有字符串的前缀以及每个前缀出现的次数。 +我们可以用前缀树来维护所有字符串的前缀以及每个前缀出现的次数。 -然后遍历每个字符串,累加每个前缀的出现次数即可。 +定义前缀树节点结构体 `Trie`,包含两个属性: -时间复杂度 $O(n \times m)$。其中 $n$, $m$ 分别为字符串数组 `words` 的长度和其中字符串的最大长度。 +- `children`:长度为 26 的数组,用于存储当前节点的子节点。 +- `cnt`:当前节点的出现次数。 + +定义前缀树的两个方法: + +- `insert`:插入一个字符串,将其前缀插入前缀树。 +- `search`:搜索一个字符串,返回其前缀的出现次数。 + +我们遍历所有字符串,将每个字符串插入前缀树中。然后再遍历所有字符串,对每个字符串调用 `search` 方法,累加每个前缀的出现次数即可。 + +时间复杂度 $O(L)$,空间复杂度 $O(L)$,其中 $L$ 是所有字符串的总长度。 @@ -93,6 +103,8 @@ tags: ```python class Trie: + __slots__ = "children", "cnt" + def __init__(self): self.children = [None] * 26 self.cnt = 0 @@ -100,7 +112,7 @@ class Trie: def insert(self, w): node = self for c in w: - idx = ord(c) - ord('a') + idx = ord(c) - ord("a") if node.children[idx] is None: node.children[idx] = Trie() node = node.children[idx] @@ -110,7 +122,7 @@ class Trie: node = self ans = 0 for c in w: - idx = ord(c) - ord('a') + idx = ord(c) - ord("a") if node.children[idx] is None: return ans node = node.children[idx] @@ -180,19 +192,17 @@ class Solution { ```cpp class Trie { private: - vector children; - int cnt; + Trie* children[26]{}; + int cnt = 0; public: - Trie() - : children(26) - , cnt(0) {} - void insert(string& w) { Trie* node = this; for (char c : w) { int idx = c - 'a'; - if (!node->children[idx]) node->children[idx] = new Trie(); + if (!node->children[idx]) { + node->children[idx] = new Trie(); + } node = node->children[idx]; ++node->cnt; } @@ -203,7 +213,9 @@ public: int ans = 0; for (char c : w) { int idx = c - 'a'; - if (!node->children[idx]) return ans; + if (!node->children[idx]) { + return ans; + } node = node->children[idx]; ans += node->cnt; } @@ -279,42 +291,6 @@ func sumPrefixScores(words []string) []int { #### TypeScript -```ts -function sumPrefixScores(words: string[]): number[] { - const map = new Map(); - - for (const word of words) { - const n = word.length; - for (let i = 1; i <= n; i++) { - const s = word.slice(0, i); - map.set(s, (map.get(s) ?? 0) + 1); - } - } - - return words.map(word => { - const n = word.length; - let count = 0; - for (let i = 1; i <= n; i++) { - const s = word.slice(0, i); - count += map.get(s); - } - return count; - }); -} -``` - - - - - - - -### 方法二 - - - -#### TypeScript - ```ts class Trie { children: Array; @@ -357,11 +333,7 @@ function sumPrefixScores(words: string[]): number[] { for (const w of words) { trie.insert(w); } - let ans = []; - for (const w of words) { - ans.push(trie.search(w)); - } - return ans; + return words.map(w => trie.search(w)); } ``` diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md index 683e263314376..808ab3f98788c 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md @@ -79,13 +79,23 @@ Each prefix has a score of one, so the total is answer[0] = 1 + 1 + 1 + 1 = 4. -### Solution 1: Trie +### Solution 1: Prefix Tree -Use a trie to maintain the prefixes of all strings and the occurrence count of each prefix. +We can use a prefix tree to maintain all prefixes of the strings and count the occurrences of each prefix. -Then, traverse each string, accumulating the occurrence count of each prefix. +Define the prefix tree node structure `Trie`, which includes two properties: -The time complexity is $O(n \times m)$. Here, $n$ and $m$ are the length of the string array `words` and the maximum length of the strings in it, respectively. +- `children`: An array of length 26 used to store the current node's children. +- `cnt`: The occurrence count of the current node. + +Define two methods for the prefix tree: + +- `insert`: Inserts a string, adding its prefixes into the prefix tree. +- `search`: Searches for a string and returns the occurrence count of its prefixes. + +We traverse all strings, inserting each string into the prefix tree. Then we traverse all strings again, calling the `search` method for each string and summing up the occurrence counts of each prefix. + +Time complexity is $O(L)$, and space complexity is $O(L)$, where $L$ is the total length of all strings. @@ -93,6 +103,8 @@ The time complexity is $O(n \times m)$. Here, $n$ and $m$ are the length of the ```python class Trie: + __slots__ = "children", "cnt" + def __init__(self): self.children = [None] * 26 self.cnt = 0 @@ -100,7 +112,7 @@ class Trie: def insert(self, w): node = self for c in w: - idx = ord(c) - ord('a') + idx = ord(c) - ord("a") if node.children[idx] is None: node.children[idx] = Trie() node = node.children[idx] @@ -110,7 +122,7 @@ class Trie: node = self ans = 0 for c in w: - idx = ord(c) - ord('a') + idx = ord(c) - ord("a") if node.children[idx] is None: return ans node = node.children[idx] @@ -180,19 +192,17 @@ class Solution { ```cpp class Trie { private: - vector children; - int cnt; + Trie* children[26]{}; + int cnt = 0; public: - Trie() - : children(26) - , cnt(0) {} - void insert(string& w) { Trie* node = this; for (char c : w) { int idx = c - 'a'; - if (!node->children[idx]) node->children[idx] = new Trie(); + if (!node->children[idx]) { + node->children[idx] = new Trie(); + } node = node->children[idx]; ++node->cnt; } @@ -203,7 +213,9 @@ public: int ans = 0; for (char c : w) { int idx = c - 'a'; - if (!node->children[idx]) return ans; + if (!node->children[idx]) { + return ans; + } node = node->children[idx]; ans += node->cnt; } @@ -279,42 +291,6 @@ func sumPrefixScores(words []string) []int { #### TypeScript -```ts -function sumPrefixScores(words: string[]): number[] { - const map = new Map(); - - for (const word of words) { - const n = word.length; - for (let i = 1; i <= n; i++) { - const s = word.slice(0, i); - map.set(s, (map.get(s) ?? 0) + 1); - } - } - - return words.map(word => { - const n = word.length; - let count = 0; - for (let i = 1; i <= n; i++) { - const s = word.slice(0, i); - count += map.get(s); - } - return count; - }); -} -``` - - - - - - - -### Solution 2 - - - -#### TypeScript - ```ts class Trie { children: Array; @@ -357,11 +333,7 @@ function sumPrefixScores(words: string[]): number[] { for (const w of words) { trie.insert(w); } - let ans = []; - for (const w of words) { - ans.push(trie.search(w)); - } - return ans; + return words.map(w => trie.search(w)); } ``` diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.cpp b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.cpp index 648733eaa6e99..4df8ce5e3723c 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.cpp +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.cpp @@ -1,18 +1,16 @@ class Trie { private: - vector children; - int cnt; + Trie* children[26]{}; + int cnt = 0; public: - Trie() - : children(26) - , cnt(0) {} - void insert(string& w) { Trie* node = this; for (char c : w) { int idx = c - 'a'; - if (!node->children[idx]) node->children[idx] = new Trie(); + if (!node->children[idx]) { + node->children[idx] = new Trie(); + } node = node->children[idx]; ++node->cnt; } @@ -23,7 +21,9 @@ class Trie { int ans = 0; for (char c : w) { int idx = c - 'a'; - if (!node->children[idx]) return ans; + if (!node->children[idx]) { + return ans; + } node = node->children[idx]; ans += node->cnt; } @@ -44,4 +44,4 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.py b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.py index 73579eb370314..bef821c310c9f 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.py +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.py @@ -1,4 +1,6 @@ class Trie: + __slots__ = "children", "cnt" + def __init__(self): self.children = [None] * 26 self.cnt = 0 @@ -6,7 +8,7 @@ def __init__(self): def insert(self, w): node = self for c in w: - idx = ord(c) - ord('a') + idx = ord(c) - ord("a") if node.children[idx] is None: node.children[idx] = Trie() node = node.children[idx] @@ -16,7 +18,7 @@ def search(self, w): node = self ans = 0 for c in w: - idx = ord(c) - ord('a') + idx = ord(c) - ord("a") if node.children[idx] is None: return ans node = node.children[idx] diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.ts b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.ts index 8dbbaf5a2ff83..a898ba5ece897 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.ts +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.ts @@ -1,21 +1,43 @@ -function sumPrefixScores(words: string[]): number[] { - const map = new Map(); +class Trie { + children: Array; + cnt: number; + + constructor() { + this.children = Array(26); + this.cnt = 0; + } - for (const word of words) { - const n = word.length; - for (let i = 1; i <= n; i++) { - const s = word.slice(0, i); - map.set(s, (map.get(s) ?? 0) + 1); + insert(w: string): void { + let node = this; + for (const c of w) { + const idx = c.charCodeAt(0) - 'a'.charCodeAt(0); + if (!node.children[idx]) { + node.children[idx] = new Trie(); + } + node = node.children[idx]; + node.cnt++; } } - return words.map(word => { - const n = word.length; - let count = 0; - for (let i = 1; i <= n; i++) { - const s = word.slice(0, i); - count += map.get(s); + search(w: string): number { + let node = this; + let ans = 0; + for (const c of w) { + const idx = c.charCodeAt(0) - 'a'.charCodeAt(0); + if (!node.children[idx]) { + return ans; + } + node = node.children[idx]; + ans += node.cnt; } - return count; - }); + return ans; + } +} + +function sumPrefixScores(words: string[]): number[] { + const trie = new Trie(); + for (const w of words) { + trie.insert(w); + } + return words.map(w => trie.search(w)); } diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution2.ts b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution2.ts deleted file mode 100644 index fa8217675a44e..0000000000000 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution2.ts +++ /dev/null @@ -1,47 +0,0 @@ -class Trie { - children: Array; - cnt: number; - - constructor() { - this.children = Array(26); - this.cnt = 0; - } - - insert(w: string): void { - let node = this; - for (const c of w) { - const idx = c.charCodeAt(0) - 'a'.charCodeAt(0); - if (!node.children[idx]) { - node.children[idx] = new Trie(); - } - node = node.children[idx]; - node.cnt++; - } - } - - search(w: string): number { - let node = this; - let ans = 0; - for (const c of w) { - const idx = c.charCodeAt(0) - 'a'.charCodeAt(0); - if (!node.children[idx]) { - return ans; - } - node = node.children[idx]; - ans += node.cnt; - } - return ans; - } -} - -function sumPrefixScores(words: string[]): number[] { - const trie = new Trie(); - for (const w of words) { - trie.insert(w); - } - let ans = []; - for (const w of words) { - ans.push(trie.search(w)); - } - return ans; -} diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md index cf541f2d459f9..c2b006949e5d9 100644 --- a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md @@ -176,41 +176,34 @@ class Solution { #### C++ ```cpp -using ll = long long; - class Solution { public: int numberOfStableArrays(int zero, int one, int limit) { - this->limit = limit; - f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); - return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; - } - -private: - const int mod = 1e9 + 7; - int limit; - vector>> f; - - ll dfs(int i, int j, int k) { - if (i < 0 || j < 0) { - return 0; - } - if (i == 0) { - return k == 1 && j <= limit; - } - if (j == 0) { - return k == 0 && i <= limit; - } - ll& res = f[i][j][k]; - if (res != -1) { + const int mod = 1e9 + 7; + using ll = long long; + vector>> f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); + auto dfs = [&](auto&& dfs, int i, int j, int k) -> ll { + if (i < 0 || j < 0) { + return 0; + } + if (i == 0) { + return k == 1 && j <= limit; + } + if (j == 0) { + return k == 0 && i <= limit; + } + ll& res = f[i][j][k]; + if (res != -1) { + return res; + } + if (k == 0) { + res = (dfs(dfs, i - 1, j, 0) + dfs(dfs, i - 1, j, 1) - dfs(dfs, i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(dfs, i, j - 1, 0) + dfs(dfs, i, j - 1, 1) - dfs(dfs, i, j - limit - 1, 0) + mod) % mod; + } return res; - } - if (k == 0) { - res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; - } else { - res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; - } - return res; + }; + return (dfs(dfs, zero, one, 0) + dfs(dfs, zero, one, 1)) % mod; } }; ``` @@ -259,6 +252,41 @@ func numberOfStableArrays(zero int, one int, limit int) int { } ``` +#### TypeScript + +```ts +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [-1, -1]), + ); + + const dfs = (i: number, j: number, k: number): number => { + if (i < 0 || j < 0) { + return 0; + } + if (i === 0) { + return k === 1 && j <= limit ? 1 : 0; + } + if (j === 0) { + return k === 0 && i <= limit ? 1 : 0; + } + let res = f[i][j][k]; + if (res !== -1) { + return res; + } + if (k === 0) { + res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; + } + return (f[i][j][k] = res); + }; + + return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; +} +``` + @@ -295,16 +323,10 @@ class Solution: f[0][j][1] = 1 for i in range(1, zero + 1): for j in range(1, one + 1): - f[i][j][0] = ( - f[i - 1][j][0] - + f[i - 1][j][1] - - (0 if i - limit - 1 < 0 else f[i - limit - 1][j][1]) - ) % mod - f[i][j][1] = ( - f[i][j - 1][0] - + f[i][j - 1][1] - - (0 if j - limit - 1 < 0 else f[i][j - limit - 1][0]) - ) % mod + x = 0 if i - limit - 1 < 0 else f[i - limit - 1][j][1] + y = 0 if j - limit - 1 < 0 else f[i][j - limit - 1][0] + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x) % mod + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y) % mod return sum(f[zero][one]) % mod ``` @@ -323,12 +345,10 @@ class Solution { } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + long x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + long y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (int) ((f[zero][one][0] + f[zero][one][1]) % mod); @@ -354,12 +374,10 @@ public: } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + ll x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + ll y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (f[zero][one][0] + f[zero][one][1]) % mod; @@ -398,6 +416,35 @@ func numberOfStableArrays(zero int, one int, limit int) int { } ``` +#### TypeScript + +```ts +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [0, 0]), + ); + + for (let i = 1; i <= Math.min(limit, zero); i++) { + f[i][0][0] = 1; + } + for (let j = 1; j <= Math.min(limit, one); j++) { + f[0][j][1] = 1; + } + + for (let i = 1; i <= zero; i++) { + for (let j = 1; j <= one; j++) { + const x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + const y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; + } + } + + return (f[zero][one][0] + f[zero][one][1]) % mod; +} +``` + diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md index 68d2a07b0947e..4888d0a98822b 100644 --- a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md @@ -167,41 +167,34 @@ class Solution { #### C++ ```cpp -using ll = long long; - class Solution { public: int numberOfStableArrays(int zero, int one, int limit) { - this->limit = limit; - f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); - return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; - } - -private: - const int mod = 1e9 + 7; - int limit; - vector>> f; - - ll dfs(int i, int j, int k) { - if (i < 0 || j < 0) { - return 0; - } - if (i == 0) { - return k == 1 && j <= limit; - } - if (j == 0) { - return k == 0 && i <= limit; - } - ll& res = f[i][j][k]; - if (res != -1) { + const int mod = 1e9 + 7; + using ll = long long; + vector>> f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); + auto dfs = [&](auto&& dfs, int i, int j, int k) -> ll { + if (i < 0 || j < 0) { + return 0; + } + if (i == 0) { + return k == 1 && j <= limit; + } + if (j == 0) { + return k == 0 && i <= limit; + } + ll& res = f[i][j][k]; + if (res != -1) { + return res; + } + if (k == 0) { + res = (dfs(dfs, i - 1, j, 0) + dfs(dfs, i - 1, j, 1) - dfs(dfs, i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(dfs, i, j - 1, 0) + dfs(dfs, i, j - 1, 1) - dfs(dfs, i, j - limit - 1, 0) + mod) % mod; + } return res; - } - if (k == 0) { - res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; - } else { - res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; - } - return res; + }; + return (dfs(dfs, zero, one, 0) + dfs(dfs, zero, one, 1)) % mod; } }; ``` @@ -250,6 +243,41 @@ func numberOfStableArrays(zero int, one int, limit int) int { } ``` +#### TypeScript + +```ts +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [-1, -1]), + ); + + const dfs = (i: number, j: number, k: number): number => { + if (i < 0 || j < 0) { + return 0; + } + if (i === 0) { + return k === 1 && j <= limit ? 1 : 0; + } + if (j === 0) { + return k === 0 && i <= limit ? 1 : 0; + } + let res = f[i][j][k]; + if (res !== -1) { + return res; + } + if (k === 0) { + res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; + } + return (f[i][j][k] = res); + }; + + return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; +} +``` + @@ -286,16 +314,10 @@ class Solution: f[0][j][1] = 1 for i in range(1, zero + 1): for j in range(1, one + 1): - f[i][j][0] = ( - f[i - 1][j][0] - + f[i - 1][j][1] - - (0 if i - limit - 1 < 0 else f[i - limit - 1][j][1]) - ) % mod - f[i][j][1] = ( - f[i][j - 1][0] - + f[i][j - 1][1] - - (0 if j - limit - 1 < 0 else f[i][j - limit - 1][0]) - ) % mod + x = 0 if i - limit - 1 < 0 else f[i - limit - 1][j][1] + y = 0 if j - limit - 1 < 0 else f[i][j - limit - 1][0] + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x) % mod + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y) % mod return sum(f[zero][one]) % mod ``` @@ -303,37 +325,24 @@ class Solution: ```java class Solution { - private final int mod = (int) 1e9 + 7; - private Long[][][] f; - private int limit; - public int numberOfStableArrays(int zero, int one, int limit) { - f = new Long[zero + 1][one + 1][2]; - this.limit = limit; - return (int) ((dfs(zero, one, 0) + dfs(zero, one, 1)) % mod); - } - - private long dfs(int i, int j, int k) { - if (i < 0 || j < 0) { - return 0; - } - if (i == 0) { - return k == 1 && j <= limit ? 1 : 0; - } - if (j == 0) { - return k == 0 && i <= limit ? 1 : 0; + final int mod = (int) 1e9 + 7; + long[][][] f = new long[zero + 1][one + 1][2]; + for (int i = 1; i <= Math.min(zero, limit); ++i) { + f[i][0][0] = 1; } - if (f[i][j][k] != null) { - return f[i][j][k]; + for (int j = 1; j <= Math.min(one, limit); ++j) { + f[0][j][1] = 1; } - if (k == 0) { - f[i][j][k] - = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; - } else { - f[i][j][k] - = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; + for (int i = 1; i <= zero; ++i) { + for (int j = 1; j <= one; ++j) { + long x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + long y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; + } } - return f[i][j][k]; + return (int) ((f[zero][one][0] + f[zero][one][1]) % mod); } } ``` @@ -356,12 +365,10 @@ public: } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + ll x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + ll y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (f[zero][one][0] + f[zero][one][1]) % mod; @@ -400,6 +407,35 @@ func numberOfStableArrays(zero int, one int, limit int) int { } ``` +#### TypeScript + +```ts +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [0, 0]), + ); + + for (let i = 1; i <= Math.min(limit, zero); i++) { + f[i][0][0] = 1; + } + for (let j = 1; j <= Math.min(limit, one); j++) { + f[0][j][1] = 1; + } + + for (let i = 1; i <= zero; i++) { + for (let j = 1; j <= one; j++) { + const x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + const y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; + } + } + + return (f[zero][one][0] + f[zero][one][1]) % mod; +} +``` + diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution.cpp b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution.cpp index 8f5ef0950d2a2..dfee6678a0b5e 100644 --- a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution.cpp +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution.cpp @@ -1,37 +1,30 @@ -using ll = long long; - class Solution { public: int numberOfStableArrays(int zero, int one, int limit) { - this->limit = limit; - f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); - return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; - } - -private: - const int mod = 1e9 + 7; - int limit; - vector>> f; - - ll dfs(int i, int j, int k) { - if (i < 0 || j < 0) { - return 0; - } - if (i == 0) { - return k == 1 && j <= limit; - } - if (j == 0) { - return k == 0 && i <= limit; - } - ll& res = f[i][j][k]; - if (res != -1) { + const int mod = 1e9 + 7; + using ll = long long; + vector>> f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); + auto dfs = [&](auto&& dfs, int i, int j, int k) -> ll { + if (i < 0 || j < 0) { + return 0; + } + if (i == 0) { + return k == 1 && j <= limit; + } + if (j == 0) { + return k == 0 && i <= limit; + } + ll& res = f[i][j][k]; + if (res != -1) { + return res; + } + if (k == 0) { + res = (dfs(dfs, i - 1, j, 0) + dfs(dfs, i - 1, j, 1) - dfs(dfs, i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(dfs, i, j - 1, 0) + dfs(dfs, i, j - 1, 1) - dfs(dfs, i, j - limit - 1, 0) + mod) % mod; + } return res; - } - if (k == 0) { - res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; - } else { - res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; - } - return res; + }; + return (dfs(dfs, zero, one, 0) + dfs(dfs, zero, one, 1)) % mod; } -}; \ No newline at end of file +}; diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution.ts b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution.ts new file mode 100644 index 0000000000000..64a31af4327d8 --- /dev/null +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution.ts @@ -0,0 +1,30 @@ +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [-1, -1]), + ); + + const dfs = (i: number, j: number, k: number): number => { + if (i < 0 || j < 0) { + return 0; + } + if (i === 0) { + return k === 1 && j <= limit ? 1 : 0; + } + if (j === 0) { + return k === 0 && i <= limit ? 1 : 0; + } + let res = f[i][j][k]; + if (res !== -1) { + return res; + } + if (k === 0) { + res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; + } + return (f[i][j][k] = res); + }; + + return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; +} diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.cpp b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.cpp index 0d36a39c814d3..be0cc582cb90e 100644 --- a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.cpp +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.cpp @@ -13,14 +13,12 @@ class Solution { } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + ll x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + ll y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (f[zero][one][0] + f[zero][one][1]) % mod; } -}; \ No newline at end of file +}; diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.java b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.java index ee73b631325c8..3aa871fd3244b 100644 --- a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.java +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.java @@ -10,14 +10,12 @@ public int numberOfStableArrays(int zero, int one, int limit) { } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + long x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + long y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (int) ((f[zero][one][0] + f[zero][one][1]) % mod); } -} \ No newline at end of file +} diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.py b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.py index 643f1e05590f5..861592a92cc11 100644 --- a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.py +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.py @@ -8,14 +8,8 @@ def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int: f[0][j][1] = 1 for i in range(1, zero + 1): for j in range(1, one + 1): - f[i][j][0] = ( - f[i - 1][j][0] - + f[i - 1][j][1] - - (0 if i - limit - 1 < 0 else f[i - limit - 1][j][1]) - ) % mod - f[i][j][1] = ( - f[i][j - 1][0] - + f[i][j - 1][1] - - (0 if j - limit - 1 < 0 else f[i][j - limit - 1][0]) - ) % mod + x = 0 if i - limit - 1 < 0 else f[i - limit - 1][j][1] + y = 0 if j - limit - 1 < 0 else f[i][j - limit - 1][0] + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x) % mod + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y) % mod return sum(f[zero][one]) % mod diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.ts b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.ts new file mode 100644 index 0000000000000..de481edc1913a --- /dev/null +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/Solution2.ts @@ -0,0 +1,24 @@ +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [0, 0]), + ); + + for (let i = 1; i <= Math.min(limit, zero); i++) { + f[i][0][0] = 1; + } + for (let j = 1; j <= Math.min(limit, one); j++) { + f[0][j][1] = 1; + } + + for (let i = 1; i <= zero; i++) { + for (let j = 1; j <= one; j++) { + const x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + const y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; + } + } + + return (f[zero][one][0] + f[zero][one][1]) % mod; +} diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README.md b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README.md index ed884ea3380c1..7ec6dcc5a6758 100644 --- a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README.md +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README.md @@ -176,41 +176,34 @@ class Solution { #### C++ ```cpp -using ll = long long; - class Solution { public: int numberOfStableArrays(int zero, int one, int limit) { - this->limit = limit; - f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); - return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; - } - -private: - const int mod = 1e9 + 7; - int limit; - vector>> f; - - ll dfs(int i, int j, int k) { - if (i < 0 || j < 0) { - return 0; - } - if (i == 0) { - return k == 1 && j <= limit; - } - if (j == 0) { - return k == 0 && i <= limit; - } - ll& res = f[i][j][k]; - if (res != -1) { + const int mod = 1e9 + 7; + using ll = long long; + vector>> f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); + auto dfs = [&](auto&& dfs, int i, int j, int k) -> ll { + if (i < 0 || j < 0) { + return 0; + } + if (i == 0) { + return k == 1 && j <= limit; + } + if (j == 0) { + return k == 0 && i <= limit; + } + ll& res = f[i][j][k]; + if (res != -1) { + return res; + } + if (k == 0) { + res = (dfs(dfs, i - 1, j, 0) + dfs(dfs, i - 1, j, 1) - dfs(dfs, i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(dfs, i, j - 1, 0) + dfs(dfs, i, j - 1, 1) - dfs(dfs, i, j - limit - 1, 0) + mod) % mod; + } return res; - } - if (k == 0) { - res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; - } else { - res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; - } - return res; + }; + return (dfs(dfs, zero, one, 0) + dfs(dfs, zero, one, 1)) % mod; } }; ``` @@ -295,16 +288,10 @@ class Solution: f[0][j][1] = 1 for i in range(1, zero + 1): for j in range(1, one + 1): - f[i][j][0] = ( - f[i - 1][j][0] - + f[i - 1][j][1] - - (0 if i - limit - 1 < 0 else f[i - limit - 1][j][1]) - ) % mod - f[i][j][1] = ( - f[i][j - 1][0] - + f[i][j - 1][1] - - (0 if j - limit - 1 < 0 else f[i][j - limit - 1][0]) - ) % mod + x = 0 if i - limit - 1 < 0 else f[i - limit - 1][j][1] + y = 0 if j - limit - 1 < 0 else f[i][j - limit - 1][0] + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x) % mod + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y) % mod return sum(f[zero][one]) % mod ``` @@ -323,12 +310,10 @@ class Solution { } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + long x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + long y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (int) ((f[zero][one][0] + f[zero][one][1]) % mod); @@ -354,12 +339,10 @@ public: } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + ll x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + ll y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (f[zero][one][0] + f[zero][one][1]) % mod; @@ -398,6 +381,35 @@ func numberOfStableArrays(zero int, one int, limit int) int { } ``` +#### TypeScript + +```ts +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [0, 0]), + ); + + for (let i = 1; i <= Math.min(limit, zero); i++) { + f[i][0][0] = 1; + } + for (let j = 1; j <= Math.min(limit, one); j++) { + f[0][j][1] = 1; + } + + for (let i = 1; i <= zero; i++) { + for (let j = 1; j <= one; j++) { + const x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + const y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; + } + } + + return (f[zero][one][0] + f[zero][one][1]) % mod; +} +``` + diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README_EN.md b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README_EN.md index 7ee1772ce1bfd..db7a668d7cdcc 100644 --- a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README_EN.md +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README_EN.md @@ -167,41 +167,34 @@ class Solution { #### C++ ```cpp -using ll = long long; - class Solution { public: int numberOfStableArrays(int zero, int one, int limit) { - this->limit = limit; - f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); - return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; - } - -private: - const int mod = 1e9 + 7; - int limit; - vector>> f; - - ll dfs(int i, int j, int k) { - if (i < 0 || j < 0) { - return 0; - } - if (i == 0) { - return k == 1 && j <= limit; - } - if (j == 0) { - return k == 0 && i <= limit; - } - ll& res = f[i][j][k]; - if (res != -1) { + const int mod = 1e9 + 7; + using ll = long long; + vector>> f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); + auto dfs = [&](auto&& dfs, int i, int j, int k) -> ll { + if (i < 0 || j < 0) { + return 0; + } + if (i == 0) { + return k == 1 && j <= limit; + } + if (j == 0) { + return k == 0 && i <= limit; + } + ll& res = f[i][j][k]; + if (res != -1) { + return res; + } + if (k == 0) { + res = (dfs(dfs, i - 1, j, 0) + dfs(dfs, i - 1, j, 1) - dfs(dfs, i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(dfs, i, j - 1, 0) + dfs(dfs, i, j - 1, 1) - dfs(dfs, i, j - limit - 1, 0) + mod) % mod; + } return res; - } - if (k == 0) { - res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; - } else { - res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; - } - return res; + }; + return (dfs(dfs, zero, one, 0) + dfs(dfs, zero, one, 1)) % mod; } }; ``` @@ -286,16 +279,10 @@ class Solution: f[0][j][1] = 1 for i in range(1, zero + 1): for j in range(1, one + 1): - f[i][j][0] = ( - f[i - 1][j][0] - + f[i - 1][j][1] - - (0 if i - limit - 1 < 0 else f[i - limit - 1][j][1]) - ) % mod - f[i][j][1] = ( - f[i][j - 1][0] - + f[i][j - 1][1] - - (0 if j - limit - 1 < 0 else f[i][j - limit - 1][0]) - ) % mod + x = 0 if i - limit - 1 < 0 else f[i - limit - 1][j][1] + y = 0 if j - limit - 1 < 0 else f[i][j - limit - 1][0] + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x) % mod + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y) % mod return sum(f[zero][one]) % mod ``` @@ -314,12 +301,10 @@ class Solution { } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + long x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + long y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (int) ((f[zero][one][0] + f[zero][one][1]) % mod); @@ -345,12 +330,10 @@ public: } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + ll x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + ll y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (f[zero][one][0] + f[zero][one][1]) % mod; @@ -389,6 +372,35 @@ func numberOfStableArrays(zero int, one int, limit int) int { } ``` +#### TypeScript + +```ts +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [0, 0]), + ); + + for (let i = 1; i <= Math.min(limit, zero); i++) { + f[i][0][0] = 1; + } + for (let j = 1; j <= Math.min(limit, one); j++) { + f[0][j][1] = 1; + } + + for (let i = 1; i <= zero; i++) { + for (let j = 1; j <= one; j++) { + const x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + const y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; + } + } + + return (f[zero][one][0] + f[zero][one][1]) % mod; +} +``` + diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution.cpp b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution.cpp index 8f5ef0950d2a2..dfee6678a0b5e 100644 --- a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution.cpp +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution.cpp @@ -1,37 +1,30 @@ -using ll = long long; - class Solution { public: int numberOfStableArrays(int zero, int one, int limit) { - this->limit = limit; - f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); - return (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod; - } - -private: - const int mod = 1e9 + 7; - int limit; - vector>> f; - - ll dfs(int i, int j, int k) { - if (i < 0 || j < 0) { - return 0; - } - if (i == 0) { - return k == 1 && j <= limit; - } - if (j == 0) { - return k == 0 && i <= limit; - } - ll& res = f[i][j][k]; - if (res != -1) { + const int mod = 1e9 + 7; + using ll = long long; + vector>> f = vector>>(zero + 1, vector>(one + 1, {-1, -1})); + auto dfs = [&](auto&& dfs, int i, int j, int k) -> ll { + if (i < 0 || j < 0) { + return 0; + } + if (i == 0) { + return k == 1 && j <= limit; + } + if (j == 0) { + return k == 0 && i <= limit; + } + ll& res = f[i][j][k]; + if (res != -1) { + return res; + } + if (k == 0) { + res = (dfs(dfs, i - 1, j, 0) + dfs(dfs, i - 1, j, 1) - dfs(dfs, i - limit - 1, j, 1) + mod) % mod; + } else { + res = (dfs(dfs, i, j - 1, 0) + dfs(dfs, i, j - 1, 1) - dfs(dfs, i, j - limit - 1, 0) + mod) % mod; + } return res; - } - if (k == 0) { - res = (dfs(i - 1, j, 0) + dfs(i - 1, j, 1) - dfs(i - limit - 1, j, 1) + mod) % mod; - } else { - res = (dfs(i, j - 1, 0) + dfs(i, j - 1, 1) - dfs(i, j - limit - 1, 0) + mod) % mod; - } - return res; + }; + return (dfs(dfs, zero, one, 0) + dfs(dfs, zero, one, 1)) % mod; } -}; \ No newline at end of file +}; diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.cpp b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.cpp index 0d36a39c814d3..be0cc582cb90e 100644 --- a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.cpp +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.cpp @@ -13,14 +13,12 @@ class Solution { } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + ll x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + ll y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (f[zero][one][0] + f[zero][one][1]) % mod; } -}; \ No newline at end of file +}; diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.java b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.java index ee73b631325c8..3aa871fd3244b 100644 --- a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.java +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.java @@ -10,14 +10,12 @@ public int numberOfStableArrays(int zero, int one, int limit) { } for (int i = 1; i <= zero; ++i) { for (int j = 1; j <= one; ++j) { - f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - - (i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]) + mod) - % mod; - f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - - (j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]) + mod) - % mod; + long x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + long y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; } } return (int) ((f[zero][one][0] + f[zero][one][1]) % mod); } -} \ No newline at end of file +} diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.py b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.py index 643f1e05590f5..861592a92cc11 100644 --- a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.py +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.py @@ -8,14 +8,8 @@ def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int: f[0][j][1] = 1 for i in range(1, zero + 1): for j in range(1, one + 1): - f[i][j][0] = ( - f[i - 1][j][0] - + f[i - 1][j][1] - - (0 if i - limit - 1 < 0 else f[i - limit - 1][j][1]) - ) % mod - f[i][j][1] = ( - f[i][j - 1][0] - + f[i][j - 1][1] - - (0 if j - limit - 1 < 0 else f[i][j - limit - 1][0]) - ) % mod + x = 0 if i - limit - 1 < 0 else f[i - limit - 1][j][1] + y = 0 if j - limit - 1 < 0 else f[i][j - limit - 1][0] + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x) % mod + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y) % mod return sum(f[zero][one]) % mod diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.ts b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.ts new file mode 100644 index 0000000000000..de481edc1913a --- /dev/null +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/Solution2.ts @@ -0,0 +1,24 @@ +function numberOfStableArrays(zero: number, one: number, limit: number): number { + const mod = 1e9 + 7; + const f: number[][][] = Array.from({ length: zero + 1 }, () => + Array.from({ length: one + 1 }, () => [0, 0]), + ); + + for (let i = 1; i <= Math.min(limit, zero); i++) { + f[i][0][0] = 1; + } + for (let j = 1; j <= Math.min(limit, one); j++) { + f[0][j][1] = 1; + } + + for (let i = 1; i <= zero; i++) { + for (let j = 1; j <= one; j++) { + const x = i - limit - 1 < 0 ? 0 : f[i - limit - 1][j][1]; + const y = j - limit - 1 < 0 ? 0 : f[i][j - limit - 1][0]; + f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] - x + mod) % mod; + f[i][j][1] = (f[i][j - 1][0] + f[i][j - 1][1] - y + mod) % mod; + } + } + + return (f[zero][one][0] + f[zero][one][1]) % mod; +} diff --git a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md index dd8c718f55868..051e12d53965f 100644 --- a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md +++ b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md @@ -105,14 +105,56 @@ manager_id 是 employee_id 对应员工的经理。首席执行官的 manager_id -### 方法一 +### 方法一:递归 CTE + 连接 + +首先,我们使用递归 CTE 计算出每个员工的层级,其中 CEO 的层级为 0,将 `employee_id`、`employee_name`、`hierarchy_level`、`manager_id` 和 `salary` 保存到临时表 `T` 中。 + +然后,我们查询出 CEO 的薪资,将其保存到临时表 `P` 中。 + +最后,我们连接 `T` 和 `P` 表,计算出每个下属的薪资差异,并按照 `hierarchy_level` 和 `subordinate_id` 进行排序。 #### MySQL ```sql - +# Write your MySQL query statement below +WITH RECURSIVE + T AS ( + SELECT + employee_id, + employee_name, + 0 AS hierarchy_level, + manager_id, + salary + FROM Employees + WHERE manager_id IS NULL + UNION ALL + SELECT + e.employee_id, + e.employee_name, + hierarchy_level + 1 AS hierarchy_level, + e.manager_id, + e.salary + FROM + T t + JOIN Employees e ON t.employee_id = e.manager_id + ), + P AS ( + SELECT salary + FROM Employees + WHERE manager_id IS NULL + ) +SELECT + employee_id subordinate_id, + employee_name subordinate_name, + hierarchy_level, + t.salary - p.salary salary_difference +FROM + T t + JOIN P p +WHERE hierarchy_level != 0 +ORDER BY 3, 1; ``` diff --git a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md index df33c7678a355..cef9919f85541 100644 --- a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md +++ b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md @@ -106,14 +106,56 @@ manager_id is the employee_id of the employee's manager. The CEO has a NULL -### Solution 1 +### Solution 1: Recursive CTE + Join + +First, we use a recursive CTE to calculate the hierarchy level of each employee, where the CEO's level is $0$. We save `employee_id`, `employee_name`, `hierarchy_level`, `manager_id`, and `salary` into a temporary table `T`. + +Then, we query the CEO's salary and save it into a temporary table `P`. + +Finally, we join tables `T` and `P` to calculate the salary difference for each subordinate, and sort by `hierarchy_level` and `subordinate_id`. #### MySQL ```sql - +# Write your MySQL query statement below +WITH RECURSIVE + T AS ( + SELECT + employee_id, + employee_name, + 0 AS hierarchy_level, + manager_id, + salary + FROM Employees + WHERE manager_id IS NULL + UNION ALL + SELECT + e.employee_id, + e.employee_name, + hierarchy_level + 1 AS hierarchy_level, + e.manager_id, + e.salary + FROM + T t + JOIN Employees e ON t.employee_id = e.manager_id + ), + P AS ( + SELECT salary + FROM Employees + WHERE manager_id IS NULL + ) +SELECT + employee_id subordinate_id, + employee_name subordinate_name, + hierarchy_level, + t.salary - p.salary salary_difference +FROM + T t + JOIN P p +WHERE hierarchy_level != 0 +ORDER BY 3, 1; ``` diff --git a/solution/3200-3299/3236.CEO Subordinate Hierarchy/Solution.sql b/solution/3200-3299/3236.CEO Subordinate Hierarchy/Solution.sql new file mode 100644 index 0000000000000..7c91b6608841c --- /dev/null +++ b/solution/3200-3299/3236.CEO Subordinate Hierarchy/Solution.sql @@ -0,0 +1,37 @@ +# Write your MySQL query statement below +WITH RECURSIVE + T AS ( + SELECT + employee_id, + employee_name, + 0 AS hierarchy_level, + manager_id, + salary + FROM Employees + WHERE manager_id IS NULL + UNION ALL + SELECT + e.employee_id, + e.employee_name, + hierarchy_level + 1 AS hierarchy_level, + e.manager_id, + e.salary + FROM + T t + JOIN Employees e ON t.employee_id = e.manager_id + ), + P AS ( + SELECT salary + FROM Employees + WHERE manager_id IS NULL + ) +SELECT + employee_id subordinate_id, + employee_name subordinate_name, + hierarchy_level, + t.salary - p.salary salary_difference +FROM + T t + JOIN P p +WHERE hierarchy_level != 0 +ORDER BY 3, 1;