diff --git "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/README.md" "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/README.md" index f500b965aeff8..4e21105c919d4 100644 --- "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/README.md" +++ "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/README.md" @@ -42,13 +42,20 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2040.%20%E5%BF%83% -### 方法一:排序 + 贪心 +### 方法一:贪心 + 排序 -排序先取最大的 $cnt$ 个数,如果和为偶数则直接返回答案。 +我们注意到,题目选取的是子序列,因此我们可以考虑先对数组进行排序。 -否则,找一个已取的最小奇数换成剩余未取的最大偶数,或者找一个已取的最小偶数换成剩下未取的最大奇数,取两者中较大的。 +接下来,我们先贪心地选取最大的 $\textit{cnt}$ 个数,如果这些数的和为偶数,则直接返回这个和 $ans$。 -时间复杂度 $O(nlogn)$。 +否则,我们有两种贪心策略: + +1. 在最大的 $\textit{cnt}$ 个数中,找到一个最小的偶数 $mi1$,然后在剩下的 $n - \textit{cnt}$ 个数中,找到一个最大的奇数 $mx1$,将 $mi1$ 替换为 $mx1$,如果存在这样的替换,那么替换后的和 $ans - mi1 + mx1$ 一定是偶数; +1. 在最大的 $\textit{cnt}$ 个数中,找到一个最小的奇数 $mi2$,然后在剩下的 $n - \textit{cnt}$ 个数中,找到一个最大的偶数 $mx2$,将 $mi2$ 替换为 $mx2$,如果存在这样的替换,那么替换后的和 $ans - mi2 + mx2$ 一定是偶数。 + +我们取最大的偶数和作为答案。如果不存在偶数和,则返回 $0$。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。 @@ -57,16 +64,25 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2040.%20%E5%BF%83% ```python class Solution: def maxmiumScore(self, cards: List[int], cnt: int) -> int: - cards.sort(reverse=True) - t = cards[:cnt] - ans = sum(t) + cards.sort() + ans = sum(cards[-cnt:]) if ans % 2 == 0: return ans - a = min([v for v in t if v & 1], default=inf) - b = min([v for v in t if v % 2 == 0], default=inf) - c = max([v for v in cards[cnt:] if v % 2 == 0], default=-inf) - d = max([v for v in cards[cnt:] if v & 1], default=-inf) - return max(ans - a + c, ans - b + d, 0) + n = len(cards) + mx1 = mx2 = -inf + for x in cards[: n - cnt]: + if x & 1: + mx1 = x + else: + mx2 = x + mi1 = mi2 = inf + for x in cards[-cnt:][::-1]: + if x & 1: + mi2 = x + else: + mi1 = x + ans = max(ans - mi1 + mx1, ans - mi2 + mx2, -1) + return 0 if ans < 0 else ans ``` #### Java @@ -83,26 +99,25 @@ class Solution { if (ans % 2 == 0) { return ans; } - int inf = 0x3f3f3f3f; - int a = inf, b = inf; - for (int i = 0; i < cnt; ++i) { - int v = cards[n - i - 1]; - if (v % 2 == 1) { - a = Math.min(a, v); + final int inf = 1 << 29; + int mx1 = -inf, mx2 = -inf; + for (int i = 0; i < n - cnt; ++i) { + if (cards[i] % 2 == 1) { + mx1 = cards[i]; } else { - b = Math.min(b, v); + mx2 = cards[i]; } } - int c = -inf, d = -inf; - for (int i = cnt; i < n; ++i) { - int v = cards[n - i - 1]; - if (v % 2 == 0) { - c = Math.max(c, v); + int mi1 = inf, mi2 = inf; + for (int i = n - 1; i >= n - cnt; --i) { + if (cards[i] % 2 == 1) { + mi2 = cards[i]; } else { - d = Math.max(d, v); + mi1 = cards[i]; } } - return Math.max(0, Math.max(ans - a + c, ans - b + d)); + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? 0 : ans; } } ``` @@ -114,27 +129,33 @@ class Solution { public: int maxmiumScore(vector& cards, int cnt) { sort(cards.begin(), cards.end()); - reverse(cards.begin(), cards.end()); - int ans = 0, n = cards.size(); - for (int i = 0; i < cnt; ++i) ans += cards[i]; - if (ans % 2 == 0) return ans; - int inf = 0x3f3f3f3f; - int a = inf, b = inf, c = -inf, d = -inf; + int ans = 0; + int n = cards.size(); for (int i = 0; i < cnt; ++i) { - int v = cards[i]; - if (v % 2 == 1) - a = min(a, v); - else - b = min(b, v); + ans += cards[n - i - 1]; } - for (int i = cnt; i < n; ++i) { - int v = cards[i]; - if (v % 2 == 0) - c = max(c, v); - else - d = max(d, v); + if (ans % 2 == 0) { + return ans; + } + const int inf = 1 << 29; + int mx1 = -inf, mx2 = -inf; + for (int i = 0; i < n - cnt; ++i) { + if (cards[i] % 2) { + mx1 = cards[i]; + } else { + mx2 = cards[i]; + } } - return max(0, max(ans - a + c, ans - b + d)); + int mi1 = inf, mi2 = inf; + for (int i = n - 1; i >= n - cnt; --i) { + if (cards[i] % 2) { + mi2 = cards[i]; + } else { + mi1 = cards[i]; + } + } + ans = max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? 0 : ans; } }; ``` @@ -143,31 +164,74 @@ public: ```go func maxmiumScore(cards []int, cnt int) int { + sort.Ints(cards) ans := 0 - sort.Slice(cards, func(i, j int) bool { return cards[i] > cards[j] }) - for _, v := range cards[:cnt] { - ans += v + n := len(cards) + for i := 0; i < cnt; i++ { + ans += cards[n-1-i] } if ans%2 == 0 { return ans } - inf := 0x3f3f3f3f - a, b, c, d := inf, inf, -inf, -inf - for _, v := range cards[:cnt] { - if v%2 == 1 { - a = min(a, v) + const inf = 1 << 29 + mx1, mx2 := -inf, -inf + for _, x := range cards[:n-cnt] { + if x%2 == 1 { + mx1 = x } else { - b = min(b, v) + mx2 = x } } - for _, v := range cards[cnt:] { - if v%2 == 0 { - c = max(c, v) + mi1, mi2 := inf, inf + for i := n - 1; i >= n-cnt; i-- { + if cards[i]%2 == 1 { + mi2 = cards[i] } else { - d = max(d, v) + mi1 = cards[i] } } - return max(0, max(ans-a+c, ans-b+d)) + ans = max(-1, max(ans-mi1+mx1, ans-mi2+mx2)) + if ans < 0 { + return 0 + } + return ans +} +``` + +#### TypeScript + +```ts +function maxmiumScore(cards: number[], cnt: number): number { + cards.sort((a, b) => a - b); + let ans = 0; + const n = cards.length; + for (let i = 0; i < cnt; ++i) { + ans += cards[n - i - 1]; + } + if (ans % 2 === 0) { + return ans; + } + const inf = 1 << 29; + let mx1 = -inf, + mx2 = -inf; + for (let i = 0; i < n - cnt; ++i) { + if (cards[i] % 2 === 1) { + mx1 = cards[i]; + } else { + mx2 = cards[i]; + } + } + let mi1 = inf, + mi2 = inf; + for (let i = n - 1; i >= n - cnt; --i) { + if (cards[i] % 2 === 1) { + mi2 = cards[i]; + } else { + mi1 = cards[i]; + } + } + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? 0 : ans; } ``` diff --git "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.cpp" "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.cpp" index 385484dd7f083..c0c34b2c228b7 100644 --- "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.cpp" +++ "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.cpp" @@ -2,26 +2,32 @@ class Solution { public: int maxmiumScore(vector& cards, int cnt) { sort(cards.begin(), cards.end()); - reverse(cards.begin(), cards.end()); - int ans = 0, n = cards.size(); - for (int i = 0; i < cnt; ++i) ans += cards[i]; - if (ans % 2 == 0) return ans; - int inf = 0x3f3f3f3f; - int a = inf, b = inf, c = -inf, d = -inf; + int ans = 0; + int n = cards.size(); for (int i = 0; i < cnt; ++i) { - int v = cards[i]; - if (v % 2 == 1) - a = min(a, v); - else - b = min(b, v); + ans += cards[n - i - 1]; } - for (int i = cnt; i < n; ++i) { - int v = cards[i]; - if (v % 2 == 0) - c = max(c, v); - else - d = max(d, v); + if (ans % 2 == 0) { + return ans; } - return max(0, max(ans - a + c, ans - b + d)); + const int inf = 1 << 29; + int mx1 = -inf, mx2 = -inf; + for (int i = 0; i < n - cnt; ++i) { + if (cards[i] % 2) { + mx1 = cards[i]; + } else { + mx2 = cards[i]; + } + } + int mi1 = inf, mi2 = inf; + for (int i = n - 1; i >= n - cnt; --i) { + if (cards[i] % 2) { + mi2 = cards[i]; + } else { + mi1 = cards[i]; + } + } + ans = max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? 0 : ans; } -}; \ No newline at end of file +}; diff --git "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.go" "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.go" index 3379eeaca2a9d..9af7695a4ebe4 100644 --- "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.go" +++ "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.go" @@ -1,27 +1,33 @@ func maxmiumScore(cards []int, cnt int) int { + sort.Ints(cards) ans := 0 - sort.Slice(cards, func(i, j int) bool { return cards[i] > cards[j] }) - for _, v := range cards[:cnt] { - ans += v + n := len(cards) + for i := 0; i < cnt; i++ { + ans += cards[n-1-i] } if ans%2 == 0 { return ans } - inf := 0x3f3f3f3f - a, b, c, d := inf, inf, -inf, -inf - for _, v := range cards[:cnt] { - if v%2 == 1 { - a = min(a, v) + const inf = 1 << 29 + mx1, mx2 := -inf, -inf + for _, x := range cards[:n-cnt] { + if x%2 == 1 { + mx1 = x } else { - b = min(b, v) + mx2 = x } } - for _, v := range cards[cnt:] { - if v%2 == 0 { - c = max(c, v) + mi1, mi2 := inf, inf + for i := n - 1; i >= n-cnt; i-- { + if cards[i]%2 == 1 { + mi2 = cards[i] } else { - d = max(d, v) + mi1 = cards[i] } } - return max(0, max(ans-a+c, ans-b+d)) -} \ No newline at end of file + ans = max(-1, max(ans-mi1+mx1, ans-mi2+mx2)) + if ans < 0 { + return 0 + } + return ans +} diff --git "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.java" "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.java" index 8372eaaa75bfe..1bf357429b133 100644 --- "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.java" +++ "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.java" @@ -9,25 +9,24 @@ public int maxmiumScore(int[] cards, int cnt) { if (ans % 2 == 0) { return ans; } - int inf = 0x3f3f3f3f; - int a = inf, b = inf; - for (int i = 0; i < cnt; ++i) { - int v = cards[n - i - 1]; - if (v % 2 == 1) { - a = Math.min(a, v); + final int inf = 1 << 29; + int mx1 = -inf, mx2 = -inf; + for (int i = 0; i < n - cnt; ++i) { + if (cards[i] % 2 == 1) { + mx1 = cards[i]; } else { - b = Math.min(b, v); + mx2 = cards[i]; } } - int c = -inf, d = -inf; - for (int i = cnt; i < n; ++i) { - int v = cards[n - i - 1]; - if (v % 2 == 0) { - c = Math.max(c, v); + int mi1 = inf, mi2 = inf; + for (int i = n - 1; i >= n - cnt; --i) { + if (cards[i] % 2 == 1) { + mi2 = cards[i]; } else { - d = Math.max(d, v); + mi1 = cards[i]; } } - return Math.max(0, Math.max(ans - a + c, ans - b + d)); + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? 0 : ans; } -} \ No newline at end of file +} diff --git "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.py" "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.py" index 508387b12f151..ea164ef9bfd91 100644 --- "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.py" +++ "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.py" @@ -1,12 +1,21 @@ class Solution: def maxmiumScore(self, cards: List[int], cnt: int) -> int: - cards.sort(reverse=True) - t = cards[:cnt] - ans = sum(t) + cards.sort() + ans = sum(cards[-cnt:]) if ans % 2 == 0: return ans - a = min([v for v in t if v & 1], default=inf) - b = min([v for v in t if v % 2 == 0], default=inf) - c = max([v for v in cards[cnt:] if v % 2 == 0], default=-inf) - d = max([v for v in cards[cnt:] if v & 1], default=-inf) - return max(ans - a + c, ans - b + d, 0) + n = len(cards) + mx1 = mx2 = -inf + for x in cards[: n - cnt]: + if x & 1: + mx1 = x + else: + mx2 = x + mi1 = mi2 = inf + for x in cards[-cnt:][::-1]: + if x & 1: + mi2 = x + else: + mi1 = x + ans = max(ans - mi1 + mx1, ans - mi2 + mx2, -1) + return 0 if ans < 0 else ans diff --git "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.ts" "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.ts" new file mode 100644 index 0000000000000..4e6114429978a --- /dev/null +++ "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.ts" @@ -0,0 +1,32 @@ +function maxmiumScore(cards: number[], cnt: number): number { + cards.sort((a, b) => a - b); + let ans = 0; + const n = cards.length; + for (let i = 0; i < cnt; ++i) { + ans += cards[n - i - 1]; + } + if (ans % 2 === 0) { + return ans; + } + const inf = 1 << 29; + let mx1 = -inf, + mx2 = -inf; + for (let i = 0; i < n - cnt; ++i) { + if (cards[i] % 2 === 1) { + mx1 = cards[i]; + } else { + mx2 = cards[i]; + } + } + let mi1 = inf, + mi2 = inf; + for (let i = n - 1; i >= n - cnt; --i) { + if (cards[i] % 2 === 1) { + mi2 = cards[i]; + } else { + mi1 = cards[i]; + } + } + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? 0 : ans; +} diff --git a/solution/3200-3299/3206.Alternating Groups I/README.md b/solution/3200-3299/3206.Alternating Groups I/README.md index 13e5f8ae452d0..4222dac247b6c 100644 --- a/solution/3200-3299/3206.Alternating Groups I/README.md +++ b/solution/3200-3299/3206.Alternating Groups I/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3206.Alternating%20Groups%20I/README.md +rating: 1223 +source: 第 134 场双周赛 Q1 tags: - 数组 - 滑动窗口 diff --git a/solution/3200-3299/3206.Alternating Groups I/README_EN.md b/solution/3200-3299/3206.Alternating Groups I/README_EN.md index f955404325211..92c5e1fecdfdf 100644 --- a/solution/3200-3299/3206.Alternating Groups I/README_EN.md +++ b/solution/3200-3299/3206.Alternating Groups I/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3206.Alternating%20Groups%20I/README_EN.md +rating: 1223 +source: Biweekly Contest 134 Q1 tags: - Array - Sliding Window diff --git a/solution/3200-3299/3207.Maximum Points After Enemy Battles/README.md b/solution/3200-3299/3207.Maximum Points After Enemy Battles/README.md index 7b186e1d37b5a..122f272003f8e 100644 --- a/solution/3200-3299/3207.Maximum Points After Enemy Battles/README.md +++ b/solution/3200-3299/3207.Maximum Points After Enemy Battles/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3207.Maximum%20Points%20After%20Enemy%20Battles/README.md +rating: 1591 +source: 第 134 场双周赛 Q2 tags: - 贪心 - 数组 diff --git a/solution/3200-3299/3207.Maximum Points After Enemy Battles/README_EN.md b/solution/3200-3299/3207.Maximum Points After Enemy Battles/README_EN.md index ca3f5e671014d..28741c8708b18 100644 --- a/solution/3200-3299/3207.Maximum Points After Enemy Battles/README_EN.md +++ b/solution/3200-3299/3207.Maximum Points After Enemy Battles/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3207.Maximum%20Points%20After%20Enemy%20Battles/README_EN.md +rating: 1591 +source: Biweekly Contest 134 Q2 tags: - Greedy - Array diff --git a/solution/3200-3299/3208.Alternating Groups II/README.md b/solution/3200-3299/3208.Alternating Groups II/README.md index a2a42683fd46f..8b2dc2eba5eeb 100644 --- a/solution/3200-3299/3208.Alternating Groups II/README.md +++ b/solution/3200-3299/3208.Alternating Groups II/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3208.Alternating%20Groups%20II/README.md +rating: 1721 +source: 第 134 场双周赛 Q3 tags: - 数组 - 滑动窗口 diff --git a/solution/3200-3299/3208.Alternating Groups II/README_EN.md b/solution/3200-3299/3208.Alternating Groups II/README_EN.md index 70a06159d6e20..a8811e709e9cd 100644 --- a/solution/3200-3299/3208.Alternating Groups II/README_EN.md +++ b/solution/3200-3299/3208.Alternating Groups II/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3208.Alternating%20Groups%20II/README_EN.md +rating: 1721 +source: Biweekly Contest 134 Q3 tags: - Array - Sliding Window diff --git a/solution/3200-3299/3209.Number of Subarrays With AND Value of K/README.md b/solution/3200-3299/3209.Number of Subarrays With AND Value of K/README.md index b4e01a11e8e7e..96be0735405d7 100644 --- a/solution/3200-3299/3209.Number of Subarrays With AND Value of K/README.md +++ b/solution/3200-3299/3209.Number of Subarrays With AND Value of K/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3209.Number%20of%20Subarrays%20With%20AND%20Value%20of%20K/README.md +rating: 2050 +source: 第 134 场双周赛 Q4 tags: - 位运算 - 线段树 diff --git a/solution/3200-3299/3209.Number of Subarrays With AND Value of K/README_EN.md b/solution/3200-3299/3209.Number of Subarrays With AND Value of K/README_EN.md index e3fcea5a71fe0..9cf3a00991055 100644 --- a/solution/3200-3299/3209.Number of Subarrays With AND Value of K/README_EN.md +++ b/solution/3200-3299/3209.Number of Subarrays With AND Value of K/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3209.Number%20of%20Subarrays%20With%20AND%20Value%20of%20K/README_EN.md +rating: 2050 +source: Biweekly Contest 134 Q4 tags: - Bit Manipulation - Segment Tree diff --git a/solution/3200-3299/3210.Find the Encrypted String/README.md b/solution/3200-3299/3210.Find the Encrypted String/README.md index 7d494a649f799..5eda6583473a2 100644 --- a/solution/3200-3299/3210.Find the Encrypted String/README.md +++ b/solution/3200-3299/3210.Find the Encrypted String/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3210.Find%20the%20Encrypted%20String/README.md +rating: 1179 +source: 第 405 场周赛 Q1 tags: - 字符串 --- diff --git a/solution/3200-3299/3210.Find the Encrypted String/README_EN.md b/solution/3200-3299/3210.Find the Encrypted String/README_EN.md index 19a55052eace1..55d01cf9d8cc1 100644 --- a/solution/3200-3299/3210.Find the Encrypted String/README_EN.md +++ b/solution/3200-3299/3210.Find the Encrypted String/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3210.Find%20the%20Encrypted%20String/README_EN.md +rating: 1179 +source: Weekly Contest 405 Q1 tags: - String --- diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md index d1169b1cf7f3b..9adda151ba2e4 100644 --- a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3211.Generate%20Binary%20Strings%20Without%20Adjacent%20Zeros/README.md +rating: 1352 +source: 第 405 场周赛 Q2 tags: - 位运算 - 递归 diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md index 14dc2ab98e423..6b1ac72f6b51a 100644 --- a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3211.Generate%20Binary%20Strings%20Without%20Adjacent%20Zeros/README_EN.md +rating: 1352 +source: Weekly Contest 405 Q2 tags: - Bit Manipulation - Recursion diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md index 8a55c146733cd..e3c42831e9a7d 100644 --- a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README.md +rating: 1672 +source: 第 405 场周赛 Q3 tags: - 数组 - 矩阵 diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md index 466cb50630e6c..d8280dcef2bd0 100644 --- a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README_EN.md +rating: 1672 +source: Weekly Contest 405 Q3 tags: - Array - Matrix diff --git a/solution/3200-3299/3213.Construct String with Minimum Cost/README.md b/solution/3200-3299/3213.Construct String with Minimum Cost/README.md index c2b9f03bdfdfc..c32e43fe2fe81 100644 --- a/solution/3200-3299/3213.Construct String with Minimum Cost/README.md +++ b/solution/3200-3299/3213.Construct String with Minimum Cost/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README.md +rating: 2170 +source: 第 405 场周赛 Q4 tags: - 数组 - 字符串 diff --git a/solution/3200-3299/3213.Construct String with Minimum Cost/README_EN.md b/solution/3200-3299/3213.Construct String with Minimum Cost/README_EN.md index c9c9d354943bc..b9403c42dccfe 100644 --- a/solution/3200-3299/3213.Construct String with Minimum Cost/README_EN.md +++ b/solution/3200-3299/3213.Construct String with Minimum Cost/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README_EN.md +rating: 2170 +source: Weekly Contest 405 Q4 tags: - Array - String diff --git a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md index 619a08f34dfc5..b7bd3ad19f6dc 100644 --- a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md +++ b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3216.Lexicographically%20Smallest%20String%20After%20a%20Swap/README.md +rating: 1242 +source: 第 406 场周赛 Q1 tags: - 贪心 - 字符串 diff --git a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md index 722bd35a5fce3..de9488609865b 100644 --- a/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md +++ b/solution/3200-3299/3216.Lexicographically Smallest String After a Swap/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3216.Lexicographically%20Smallest%20String%20After%20a%20Swap/README_EN.md +rating: 1242 +source: Weekly Contest 406 Q1 tags: - Greedy - String diff --git a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md index f9197ce5cab68..d999985a5ca76 100644 --- a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md +++ b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3217.Delete%20Nodes%20From%20Linked%20List%20Present%20in%20Array/README.md +rating: 1341 +source: 第 406 场周赛 Q2 tags: - 数组 - 哈希表 diff --git a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md index 1cb8b73ac116c..25635df4cc7e0 100644 --- a/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md +++ b/solution/3200-3299/3217.Delete Nodes From Linked List Present in Array/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3217.Delete%20Nodes%20From%20Linked%20List%20Present%20in%20Array/README_EN.md +rating: 1341 +source: Weekly Contest 406 Q2 tags: - Array - Hash Table diff --git a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md index 1e93bb69e8b4f..0be2d066422a0 100644 --- a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md +++ b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3218.Minimum%20Cost%20for%20Cutting%20Cake%20I/README.md +rating: 1654 +source: 第 406 场周赛 Q3 tags: - 贪心 - 数组 diff --git a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md index 1b75f685db101..9d77bdd991469 100644 --- a/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md +++ b/solution/3200-3299/3218.Minimum Cost for Cutting Cake I/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3218.Minimum%20Cost%20for%20Cutting%20Cake%20I/README_EN.md +rating: 1654 +source: Weekly Contest 406 Q3 tags: - Greedy - Array diff --git a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md index ec9e4977f4a8c..14d3bfb3b2c6e 100644 --- a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md +++ b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3219.Minimum%20Cost%20for%20Cutting%20Cake%20II/README.md +rating: 1789 +source: 第 406 场周赛 Q4 tags: - 贪心 - 数组 diff --git a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README_EN.md b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README_EN.md index 7a9606a19f53c..502af18615bb1 100644 --- a/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README_EN.md +++ b/solution/3200-3299/3219.Minimum Cost for Cutting Cake II/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3219.Minimum%20Cost%20for%20Cutting%20Cake%20II/README_EN.md +rating: 1789 +source: Weekly Contest 406 Q4 tags: - Greedy - Array diff --git a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md new file mode 100644 index 0000000000000..dd8c718f55868 --- /dev/null +++ b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README.md @@ -0,0 +1,122 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README.md +tags: + - 数据库 +--- + + + +# [3236. 首席执行官下属层级 🔒](https://leetcode.cn/problems/ceo-subordinate-hierarchy) + +[English Version](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README_EN.md) + +## 题目描述 + + + +

表:Employees

+ +
++---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| employee_id   | int     |
+| employee_name | varchar |
+| manager_id    | int     |
+| salary        | int     |
++---------------+---------+
+employee_id 是这张表的唯一标识符。
+manager_id 是 employee_id 对应员工的经理。首席执行官的 manager_id 为 NULL。
+
+ +

编写一个解决方案来找到首席执行官的下属(直接 和 非直接),以及他们在 等级制度中的级别 以及与首席执行官的 薪资差异。结果应该包含下面的列:

+ +

查询结果格式如下所示。

+ + + +

返回结果表以 hierarchy_level 升序排序,然后按 subordinate_id 升序排序

+ +

查询格式如下所示。

+ +

 

+ +

示例:

+ +
+

输入:

+ +

Employees 表:

+ +
++-------------+----------------+------------+---------+
+| employee_id | employee_name  | manager_id | salary  |
++-------------+----------------+------------+---------+
+| 1           | Alice          | NULL       | 150000  |
+| 2           | Bob            | 1          | 120000  |
+| 3           | Charlie        | 1          | 110000  |
+| 4           | David          | 2          | 105000  |
+| 5           | Eve            | 2          | 100000  |
+| 6           | Frank          | 3          | 95000   |
+| 7           | Grace          | 3          | 98000   |
+| 8           | Helen          | 5          | 90000   |
++-------------+----------------+------------+---------+
+
+ +

输出:

+ +
++----------------+------------------+------------------+-------------------+
+| subordinate_id | subordinate_name | hierarchy_level  | salary_difference |
++----------------+------------------+------------------+-------------------+
+| 2              | Bob              | 1                | -30000            |
+| 3              | Charlie          | 1                | -40000            |
+| 4              | David            | 2                | -45000            |
+| 5              | Eve              | 2                | -50000            |
+| 6              | Frank            | 2                | -55000            |
+| 7              | Grace            | 2                | -52000            |
+| 8              | Helen            | 3                | -60000            |
++----------------+------------------+------------------+-------------------+
+
+ +

解释:

+ +
    +
  • Bob 和 Charlie 是 Alice 的直接下属(首席执行官)因此,hierarchy_level 为 1。
  • +
  • David 和 Eve 下属于 Bob,而 Frank 和 Grace 下属于 Charlie,因此他们是二级下属(hierarchy_level 为 2)。
  • +
  • Helen 下属于 Eve,因此 Helen 为三级下属(hierarchy_level 为 3)。
  • +
  • 薪资差异是相对于 Alice 的薪资 150000 计算的。
  • +
  • 结果先以 hierarchy_level 升序排序,然后以 subordinate_id 升序排序。
  • +
+ +

注意:输出表先以 hierarchy_level 升序排序,然后以 subordinate_id 升序排序。

+
+ + + +## 解法 + + + +### 方法一 + + + +#### MySQL + +```sql + +``` + + + + + + diff --git a/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md new file mode 100644 index 0000000000000..df33c7678a355 --- /dev/null +++ b/solution/3200-3299/3236.CEO Subordinate Hierarchy/README_EN.md @@ -0,0 +1,123 @@ +--- +comments: true +difficulty: Hard +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README_EN.md +tags: + - Database +--- + + + +# [3236. CEO Subordinate Hierarchy 🔒](https://leetcode.com/problems/ceo-subordinate-hierarchy) + +[中文文档](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README.md) + +## Description + + + +

Table: Employees

+ +
++---------------+---------+
+| Column Name   | Type    |
++---------------+---------+
+| employee_id   | int     |
+| employee_name | varchar |
+| manager_id    | int     |
+| salary        | int     |
++---------------+---------+
+employee_id is the unique identifier for this table.
+manager_id is the employee_id of the employee's manager. The CEO has a NULL manager_id.
+
+ +

Write a solution to find subordinates of the CEO (both direct and indirect), along with their level in the hierarchy and their salary difference from the CEO.

+ +

The result should have the following columns:

+ +

The query result format is in the following example.

+ + + +

Return the result table ordered by hierarchy_level ascending, and then by subordinate_id ascending.

+ +

The query result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

Employees table:

+ +
++-------------+----------------+------------+---------+
+| employee_id | employee_name  | manager_id | salary  |
++-------------+----------------+------------+---------+
+| 1           | Alice          | NULL       | 150000  |
+| 2           | Bob            | 1          | 120000  |
+| 3           | Charlie        | 1          | 110000  |
+| 4           | David          | 2          | 105000  |
+| 5           | Eve            | 2          | 100000  |
+| 6           | Frank          | 3          | 95000   |
+| 7           | Grace          | 3          | 98000   |
+| 8           | Helen          | 5          | 90000   |
++-------------+----------------+------------+---------+
+
+ +

Output:

+ +
++----------------+------------------+------------------+-------------------+
+| subordinate_id | subordinate_name | hierarchy_level  | salary_difference |
++----------------+------------------+------------------+-------------------+
+| 2              | Bob              | 1                | -30000            |
+| 3              | Charlie          | 1                | -40000            |
+| 4              | David            | 2                | -45000            |
+| 5              | Eve              | 2                | -50000            |
+| 6              | Frank            | 2                | -55000            |
+| 7              | Grace            | 2                | -52000            |
+| 8              | Helen            | 3                | -60000            |
++----------------+------------------+------------------+-------------------+
+
+ +

Explanation:

+ +
    +
  • Bob and Charlie are direct subordinates of Alice (CEO) and thus have a hierarchy_level of 1.
  • +
  • David and Eve report to Bob, while Frank and Grace report to Charlie, making them second-level subordinates (hierarchy_level 2).
  • +
  • Helen reports to Eve, making Helen a third-level subordinate (hierarchy_level 3).
  • +
  • Salary differences are calculated relative to Alice's salary of 150000.
  • +
  • The result is ordered by hierarchy_level ascending, and then by subordinate_id ascending.
  • +
+ +

Note: The output is ordered first by hierarchy_level in ascending order, then by subordinate_id in ascending order.

+
+ + + +## Solutions + + + +### Solution 1 + + + +#### MySQL + +```sql + +``` + + + + + + diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index f867c16100502..44c6bbb9252b3 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -289,6 +289,7 @@ | 3214 | [同比增长率](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 | | 3220 | [奇数和偶数交易](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | `数据库` | 中等 | | | 3230 | [客户购买行为分析](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | `数据库` | 中等 | 🔒 | +| 3236 | [首席执行官下属层级](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README.md) | `数据库` | 困难 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index eab94499e3828..1fd0eba2b117a 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -287,6 +287,7 @@ Press Control + F(or Command + F on | 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md) | `Database` | Hard | 🔒 | | 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | `Database` | Medium | | | 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | +| 3236 | [CEO Subordinate Hierarchy](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README_EN.md) | `Database` | Hard | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index 958837c421182..c226352fd5805 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3246,6 +3246,7 @@ | 3233 | [统计不是特殊数字的数字数量](/solution/3200-3299/3233.Find%20the%20Count%20of%20Numbers%20Which%20Are%20Not%20Special/README.md) | | 中等 | 第 408 场周赛 | | 3234 | [统计 1 显著的字符串的数量](/solution/3200-3299/3234.Count%20the%20Number%20of%20Substrings%20With%20Dominant%20Ones/README.md) | | 中等 | 第 408 场周赛 | | 3235 | [判断矩形的两个角落是否可达](/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/README.md) | | 困难 | 第 408 场周赛 | +| 3236 | [首席执行官下属层级](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README.md) | `数据库` | 困难 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 1bd5ec7d6d077..ec969b4c196a3 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3244,6 +3244,7 @@ Press Control + F(or Command + F on | 3233 | [Find the Count of Numbers Which Are Not Special](/solution/3200-3299/3233.Find%20the%20Count%20of%20Numbers%20Which%20Are%20Not%20Special/README_EN.md) | | Medium | Weekly Contest 408 | | 3234 | [Count the Number of Substrings With Dominant Ones](/solution/3200-3299/3234.Count%20the%20Number%20of%20Substrings%20With%20Dominant%20Ones/README_EN.md) | | Medium | Weekly Contest 408 | | 3235 | [Check if the Rectangle Corner Is Reachable](/solution/3200-3299/3235.Check%20if%20the%20Rectangle%20Corner%20Is%20Reachable/README_EN.md) | | Hard | Weekly Contest 408 | +| 3236 | [CEO Subordinate Hierarchy](/solution/3200-3299/3236.CEO%20Subordinate%20Hierarchy/README_EN.md) | `Database` | Hard | 🔒 | ## Copyright