From 49e1725de619b7da2f2101062fd52766aaaed079 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 4 Feb 2024 17:30:09 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3024~3027 --- .../3024.Type of Triangle II/README.md | 18 ++ .../3024.Type of Triangle II/README_EN.md | 18 ++ .../3024.Type of Triangle II/Solution.cs | 15 ++ .../README.md | 23 +++ .../README_EN.md | 23 +++ .../Solution.cs | 20 +++ .../3026.Maximum Good Subarray Sum/README.md | 168 ++++++++++-------- .../README_EN.md | 168 ++++++++++-------- .../Solution.cpp | 25 +-- .../Solution.cs | 22 +++ .../Solution.go | 49 +++-- .../Solution.java | 17 +- .../Solution.py | 26 ++- .../Solution.ts | 20 +-- .../README.md | 23 +++ .../README_EN.md | 23 +++ .../Solution.cs | 20 +++ 17 files changed, 461 insertions(+), 217 deletions(-) create mode 100644 solution/3000-3099/3024.Type of Triangle II/Solution.cs create mode 100644 solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.cs create mode 100644 solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.cs create mode 100644 solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.cs diff --git a/solution/3000-3099/3024.Type of Triangle II/README.md b/solution/3000-3099/3024.Type of Triangle II/README.md index 679d1ed11af9e..6433074816b59 100644 --- a/solution/3000-3099/3024.Type of Triangle II/README.md +++ b/solution/3000-3099/3024.Type of Triangle II/README.md @@ -145,6 +145,24 @@ function triangleType(nums: number[]): string { } ``` +```cs +public class Solution { + public string TriangleType(int[] nums) { + Array.Sort(nums); + if (nums[0] + nums[1] <= nums[2]) { + return "none"; + } + if (nums[0] == nums[2]) { + return "equilateral"; + } + if (nums[0] == nums[1] || nums[1] == nums[2]) { + return "isosceles"; + } + return "scalene"; + } +} +``` + diff --git a/solution/3000-3099/3024.Type of Triangle II/README_EN.md b/solution/3000-3099/3024.Type of Triangle II/README_EN.md index faa77dce472a2..2420844797cf3 100644 --- a/solution/3000-3099/3024.Type of Triangle II/README_EN.md +++ b/solution/3000-3099/3024.Type of Triangle II/README_EN.md @@ -141,6 +141,24 @@ function triangleType(nums: number[]): string { } ``` +```cs +public class Solution { + public string TriangleType(int[] nums) { + Array.Sort(nums); + if (nums[0] + nums[1] <= nums[2]) { + return "none"; + } + if (nums[0] == nums[2]) { + return "equilateral"; + } + if (nums[0] == nums[1] || nums[1] == nums[2]) { + return "isosceles"; + } + return "scalene"; + } +} +``` + diff --git a/solution/3000-3099/3024.Type of Triangle II/Solution.cs b/solution/3000-3099/3024.Type of Triangle II/Solution.cs new file mode 100644 index 0000000000000..ada3aaeab829e --- /dev/null +++ b/solution/3000-3099/3024.Type of Triangle II/Solution.cs @@ -0,0 +1,15 @@ +public class Solution { + public string TriangleType(int[] nums) { + Array.Sort(nums); + if (nums[0] + nums[1] <= nums[2]) { + return "none"; + } + if (nums[0] == nums[2]) { + return "equilateral"; + } + if (nums[0] == nums[1] || nums[1] == nums[2]) { + return "isosceles"; + } + return "scalene"; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md b/solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md index 8b7eca4c2c980..609cd2af005f8 100644 --- a/solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/README.md @@ -189,6 +189,29 @@ function numberOfPairs(points: number[][]): number { } ``` +```cs +public class Solution { + public int NumberOfPairs(int[][] points) { + Array.Sort(points, (a, b) => a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.Length; + int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} +``` + diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md b/solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md index 2fa0b479103e6..1bc70ccd5e0f8 100644 --- a/solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/README_EN.md @@ -178,6 +178,29 @@ function numberOfPairs(points: number[][]): number { } ``` +```cs +public class Solution { + public int NumberOfPairs(int[][] points) { + Array.Sort(points, (a, b) => a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.Length; + int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} +``` + diff --git a/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.cs b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.cs new file mode 100644 index 0000000000000..41257789e8d60 --- /dev/null +++ b/solution/3000-3099/3025.Find the Number of Ways to Place People I/Solution.cs @@ -0,0 +1,20 @@ +public class Solution { + public int NumberOfPairs(int[][] points) { + Array.Sort(points, (a, b) => a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.Length; + int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3026.Maximum Good Subarray Sum/README.md b/solution/3000-3099/3026.Maximum Good Subarray Sum/README.md index 95a73d74a5c3b..487ce003b5177 100644 --- a/solution/3000-3099/3026.Maximum Good Subarray Sum/README.md +++ b/solution/3000-3099/3026.Maximum Good Subarray Sum/README.md @@ -50,53 +50,56 @@ ## 解法 -### 方法一 +### 方法一:前缀和 + 哈希表 + +我们用一个哈希表 $p$ 记录 $nums[i]$ 的前缀数组 $nums[0..i-1]$ 的和 $s$,如果有多个相同的 $nums[i]$,我们只保留最小的 $s$。初始时,我们将 $p[nums[0]]$ 设为 $0$。另外,我们用一个变量 $s$ 记录当前的前缀和,初始时 $s = 0$。初始化答案 $ans$ 为 $-\infty$。 + +接下来,我们枚举 $nums[i]$,并且维护一个变量 $s$ 表示 $nums[0..i]$ 的和。如果 $nums[i] - k$ 在 $p$ 中,那么我们就找到了一个好子数组,将答案更新为 $ans = \max(ans, s - p[nums[i] - k])$。同理,如果 $nums[i] + k$ 在 $p$ 中,那么我们也找到了一个好子数组,将答案更新为 $ans = \max(ans, s - p[nums[i] + k])$。然后,如果 $i + 1 \lt n$ 并且 $nums[i + 1]$ 不在 $p$ 中,或者 $p[nums[i + 1]] \gt s$,我们就将 $p[nums[i + 1]]$ 设为 $s$。 + +最后,如果 $ans = -\infty$,那么我们返回 $0$,否则返回 $ans$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 ```python class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: - p = {} - r = float('-inf') - p[nums[0]] = 0 - s = 0 - n = len(nums) - for i in range(n): - s += nums[i] - if nums[i] - k in p: - r = max(r, s - p[nums[i] - k]) - if nums[i] + k in p: - r = max(r, s - p[nums[i] + k]) - if i + 1 == n: - break - if nums[i + 1] not in p or p[nums[i + 1]] > s: + ans = -inf + p = {nums[0]: 0} + s, n = 0, len(nums) + for i, x in enumerate(nums): + s += x + if x - k in p: + ans = max(ans, s - p[x - k]) + if x + k in p: + ans = max(ans, s - p[x + k]) + if i + 1 < n and (nums[i + 1] not in p or p[nums[i + 1]] > s): p[nums[i + 1]] = s - return r if r != float('-inf') else 0 + return 0 if ans == -inf else ans ``` ```java class Solution { public long maximumSubarraySum(int[] nums, int k) { - HashMap p = new HashMap<>(); - long r = Long.MIN_VALUE; + Map p = new HashMap<>(); p.put(nums[0], 0L); long s = 0; int n = nums.length; - for (int i = 0;; ++i) { + long ans = Long.MIN_VALUE; + for (int i = 0; i < n; ++i) { s += nums[i]; if (p.containsKey(nums[i] - k)) { - r = Math.max(r, s - p.get(nums[i] - k)); + ans = Math.max(ans, s - p.get(nums[i] - k)); } if (p.containsKey(nums[i] + k)) { - r = Math.max(r, s - p.get(nums[i] + k)); + ans = Math.max(ans, s - p.get(nums[i] + k)); } - if (i + 1 == n) break; - if (!p.containsKey(nums[i + 1]) || p.get(nums[i + 1]) > s) { + if (i + 1 < n && (!p.containsKey(nums[i + 1]) || p.get(nums[i + 1]) > s)) { p.put(nums[i + 1], s); } } - return r == Long.MIN_VALUE ? 0 : r; + return ans == Long.MIN_VALUE ? 0 : ans; } } ``` @@ -106,85 +109,106 @@ class Solution { public: long long maximumSubarraySum(vector& nums, int k) { unordered_map p; - long long r = LONG_LONG_MIN; p[nums[0]] = 0; long long s = 0; const int n = nums.size(); + long long ans = LONG_LONG_MIN; for (int i = 0;; ++i) { s += nums[i]; - auto t = p.find(nums[i] - k); - if (t != p.end()) { - r = max(r, s - t->second); + auto it = p.find(nums[i] - k); + if (it != p.end()) { + ans = max(ans, s - it->second); + } + it = p.find(nums[i] + k); + if (it != p.end()) { + ans = max(ans, s - it->second); } - t = p.find(nums[i] + k); - if (t != p.end()) { - r = max(r, s - t->second); + if (i + 1 == n) { + break; } - if (i + 1 == n) - break; - t = p.find(nums[i + 1]); - if (t == p.end() || t->second > s) { + it = p.find(nums[i + 1]); + if (it == p.end() || it->second > s) { p[nums[i + 1]] = s; } } - return r == LONG_LONG_MIN ? 0 : r; + return ans == LONG_LONG_MIN ? 0 : ans; } }; ``` ```go func maximumSubarraySum(nums []int, k int) int64 { - p := make(map[int]int64) - var r int64 = math.MinInt64 - p[nums[0]] = 0 - var s int64 = 0 - n := len(nums) - for i := 0; ; i++ { - s += int64(nums[i]) - if t, ok := p[nums[i]-k]; ok { - r = max(r, s-t) - } - if t, ok := p[nums[i]+k]; ok { - r = max(r, s-t) - } - if i+1 == n { - break - } - if t, ok := p[nums[i+1]]; !ok || t > s { - p[nums[i+1]] = s - } - } - if r == math.MinInt64 { - return 0 - } - return r + p := map[int]int64{nums[0]: 0} + var s int64 = 0 + n := len(nums) + var ans int64 = math.MinInt64 + for i, x := range nums { + s += int64(x) + if t, ok := p[nums[i]-k]; ok { + ans = max(ans, s-t) + } + if t, ok := p[nums[i]+k]; ok { + ans = max(ans, s-t) + } + if i+1 == n { + break + } + if t, ok := p[nums[i+1]]; !ok || s < t { + p[nums[i+1]] = s + } + } + if ans == math.MinInt64 { + return 0 + } + return ans } ``` ```ts function maximumSubarraySum(nums: number[], k: number): number { const p: Map = new Map(); - let r: number = Number.MIN_SAFE_INTEGER; p.set(nums[0], 0); + let ans: number = -Infinity; let s: number = 0; const n: number = nums.length; - for (let i = 0; ; ++i) { + for (let i = 0; i < n; ++i) { s += nums[i]; - let t: number | undefined = p.get(nums[i] - k); - if (t !== undefined) { - r = Math.max(r, s - t); + if (p.has(nums[i] - k)) { + ans = Math.max(ans, s - p.get(nums[i] - k)!); } - t = p.get(nums[i] + k); - if (t !== undefined) { - r = Math.max(r, s - t); + if (p.has(nums[i] + k)) { + ans = Math.max(ans, s - p.get(nums[i] + k)!); } - if (i + 1 === n) break; - t = p.get(nums[i + 1]); - if (t === undefined || t > s) { + if (i + 1 < n && (!p.has(nums[i + 1]) || p.get(nums[i + 1])! > s)) { p.set(nums[i + 1], s); } } - return r === Number.MIN_SAFE_INTEGER ? 0 : r; + return ans === -Infinity ? 0 : ans; +} +``` + +```cs +public class Solution { + public long MaximumSubarraySum(int[] nums, int k) { + Dictionary p = new Dictionary(); + p[nums[0]] = 0L; + long s = 0; + int n = nums.Length; + long ans = long.MinValue; + for (int i = 0; i < n; ++i) { + s += nums[i]; + if (p.ContainsKey(nums[i] - k)) { + ans = Math.Max(ans, s - p[nums[i] - k]); + } + if (p.ContainsKey(nums[i] + k)) { + ans = Math.Max(ans, s - p[nums[i] + k]); + } + if (i + 1 < n && (!p.ContainsKey(nums[i + 1]) || p[nums[i + 1]] > s)) { + p[nums[i + 1]] = s; + } + } + return ans == long.MinValue ? 0 : ans; + } } ``` diff --git a/solution/3000-3099/3026.Maximum Good Subarray Sum/README_EN.md b/solution/3000-3099/3026.Maximum Good Subarray Sum/README_EN.md index 1c5215ad610d8..3930ee56494ee 100644 --- a/solution/3000-3099/3026.Maximum Good Subarray Sum/README_EN.md +++ b/solution/3000-3099/3026.Maximum Good Subarray Sum/README_EN.md @@ -46,53 +46,56 @@ ## Solutions -### Solution 1 +### Solution 1: Prefix Sum + Hash Table + +We use a hash table $p$ to record the sum $s$ of the prefix array $nums[0..i-1]$ for $nums[i]$. If there are multiple identical $nums[i]$, we only keep the smallest $s$. Initially, we set $p[nums[0]]$ to $0$. In addition, we use a variable $s$ to record the current prefix sum, initially $s = 0$. Initialize the answer $ans$ to $-\infty$. + +Next, we enumerate $nums[i]$, and maintain a variable $s$ to represent the sum of $nums[0..i]$. If $nums[i] - k$ is in $p$, then we have found a good subarray, and update the answer to $ans = \max(ans, s - p[nums[i] - k])$. Similarly, if $nums[i] + k$ is in $p$, then we have also found a good subarray, and update the answer to $ans = \max(ans, s - p[nums[i] + k])$. Then, if $i + 1 \lt n$ and $nums[i + 1]$ is not in $p$, or $p[nums[i + 1]] \gt s$, we set $p[nums[i + 1]]$ to $s$. + +Finally, if $ans = -\infty$, then we return $0$, otherwise return $ans$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array. ```python class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: - p = {} - r = float('-inf') - p[nums[0]] = 0 - s = 0 - n = len(nums) - for i in range(n): - s += nums[i] - if nums[i] - k in p: - r = max(r, s - p[nums[i] - k]) - if nums[i] + k in p: - r = max(r, s - p[nums[i] + k]) - if i + 1 == n: - break - if nums[i + 1] not in p or p[nums[i + 1]] > s: + ans = -inf + p = {nums[0]: 0} + s, n = 0, len(nums) + for i, x in enumerate(nums): + s += x + if x - k in p: + ans = max(ans, s - p[x - k]) + if x + k in p: + ans = max(ans, s - p[x + k]) + if i + 1 < n and (nums[i + 1] not in p or p[nums[i + 1]] > s): p[nums[i + 1]] = s - return r if r != float('-inf') else 0 + return 0 if ans == -inf else ans ``` ```java class Solution { public long maximumSubarraySum(int[] nums, int k) { - HashMap p = new HashMap<>(); - long r = Long.MIN_VALUE; + Map p = new HashMap<>(); p.put(nums[0], 0L); long s = 0; int n = nums.length; - for (int i = 0;; ++i) { + long ans = Long.MIN_VALUE; + for (int i = 0; i < n; ++i) { s += nums[i]; if (p.containsKey(nums[i] - k)) { - r = Math.max(r, s - p.get(nums[i] - k)); + ans = Math.max(ans, s - p.get(nums[i] - k)); } if (p.containsKey(nums[i] + k)) { - r = Math.max(r, s - p.get(nums[i] + k)); + ans = Math.max(ans, s - p.get(nums[i] + k)); } - if (i + 1 == n) break; - if (!p.containsKey(nums[i + 1]) || p.get(nums[i + 1]) > s) { + if (i + 1 < n && (!p.containsKey(nums[i + 1]) || p.get(nums[i + 1]) > s)) { p.put(nums[i + 1], s); } } - return r == Long.MIN_VALUE ? 0 : r; + return ans == Long.MIN_VALUE ? 0 : ans; } } ``` @@ -102,85 +105,106 @@ class Solution { public: long long maximumSubarraySum(vector& nums, int k) { unordered_map p; - long long r = LONG_LONG_MIN; p[nums[0]] = 0; long long s = 0; const int n = nums.size(); + long long ans = LONG_LONG_MIN; for (int i = 0;; ++i) { s += nums[i]; - auto t = p.find(nums[i] - k); - if (t != p.end()) { - r = max(r, s - t->second); + auto it = p.find(nums[i] - k); + if (it != p.end()) { + ans = max(ans, s - it->second); + } + it = p.find(nums[i] + k); + if (it != p.end()) { + ans = max(ans, s - it->second); } - t = p.find(nums[i] + k); - if (t != p.end()) { - r = max(r, s - t->second); + if (i + 1 == n) { + break; } - if (i + 1 == n) - break; - t = p.find(nums[i + 1]); - if (t == p.end() || t->second > s) { + it = p.find(nums[i + 1]); + if (it == p.end() || it->second > s) { p[nums[i + 1]] = s; } } - return r == LONG_LONG_MIN ? 0 : r; + return ans == LONG_LONG_MIN ? 0 : ans; } }; ``` ```go func maximumSubarraySum(nums []int, k int) int64 { - p := make(map[int]int64) - var r int64 = math.MinInt64 - p[nums[0]] = 0 - var s int64 = 0 - n := len(nums) - for i := 0; ; i++ { - s += int64(nums[i]) - if t, ok := p[nums[i]-k]; ok { - r = max(r, s-t) - } - if t, ok := p[nums[i]+k]; ok { - r = max(r, s-t) - } - if i+1 == n { - break - } - if t, ok := p[nums[i+1]]; !ok || t > s { - p[nums[i+1]] = s - } - } - if r == math.MinInt64 { - return 0 - } - return r + p := map[int]int64{nums[0]: 0} + var s int64 = 0 + n := len(nums) + var ans int64 = math.MinInt64 + for i, x := range nums { + s += int64(x) + if t, ok := p[nums[i]-k]; ok { + ans = max(ans, s-t) + } + if t, ok := p[nums[i]+k]; ok { + ans = max(ans, s-t) + } + if i+1 == n { + break + } + if t, ok := p[nums[i+1]]; !ok || s < t { + p[nums[i+1]] = s + } + } + if ans == math.MinInt64 { + return 0 + } + return ans } ``` ```ts function maximumSubarraySum(nums: number[], k: number): number { const p: Map = new Map(); - let r: number = Number.MIN_SAFE_INTEGER; p.set(nums[0], 0); + let ans: number = -Infinity; let s: number = 0; const n: number = nums.length; - for (let i = 0; ; ++i) { + for (let i = 0; i < n; ++i) { s += nums[i]; - let t: number | undefined = p.get(nums[i] - k); - if (t !== undefined) { - r = Math.max(r, s - t); + if (p.has(nums[i] - k)) { + ans = Math.max(ans, s - p.get(nums[i] - k)!); } - t = p.get(nums[i] + k); - if (t !== undefined) { - r = Math.max(r, s - t); + if (p.has(nums[i] + k)) { + ans = Math.max(ans, s - p.get(nums[i] + k)!); } - if (i + 1 === n) break; - t = p.get(nums[i + 1]); - if (t === undefined || t > s) { + if (i + 1 < n && (!p.has(nums[i + 1]) || p.get(nums[i + 1])! > s)) { p.set(nums[i + 1], s); } } - return r === Number.MIN_SAFE_INTEGER ? 0 : r; + return ans === -Infinity ? 0 : ans; +} +``` + +```cs +public class Solution { + public long MaximumSubarraySum(int[] nums, int k) { + Dictionary p = new Dictionary(); + p[nums[0]] = 0L; + long s = 0; + int n = nums.Length; + long ans = long.MinValue; + for (int i = 0; i < n; ++i) { + s += nums[i]; + if (p.ContainsKey(nums[i] - k)) { + ans = Math.Max(ans, s - p[nums[i] - k]); + } + if (p.ContainsKey(nums[i] + k)) { + ans = Math.Max(ans, s - p[nums[i] + k]); + } + if (i + 1 < n && (!p.ContainsKey(nums[i + 1]) || p[nums[i + 1]] > s)) { + p[nums[i + 1]] = s; + } + } + return ans == long.MinValue ? 0 : ans; + } } ``` diff --git a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.cpp b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.cpp index c002641010a70..b7ef4116fdc26 100644 --- a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.cpp +++ b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.cpp @@ -2,27 +2,28 @@ class Solution { public: long long maximumSubarraySum(vector& nums, int k) { unordered_map p; - long long r = LONG_LONG_MIN; p[nums[0]] = 0; long long s = 0; const int n = nums.size(); + long long ans = LONG_LONG_MIN; for (int i = 0;; ++i) { s += nums[i]; - auto t = p.find(nums[i] - k); - if (t != p.end()) { - r = max(r, s - t->second); + auto it = p.find(nums[i] - k); + if (it != p.end()) { + ans = max(ans, s - it->second); } - t = p.find(nums[i] + k); - if (t != p.end()) { - r = max(r, s - t->second); + it = p.find(nums[i] + k); + if (it != p.end()) { + ans = max(ans, s - it->second); } - if (i + 1 == n) + if (i + 1 == n) { break; - t = p.find(nums[i + 1]); - if (t == p.end() || t->second > s) { + } + it = p.find(nums[i + 1]); + if (it == p.end() || it->second > s) { p[nums[i + 1]] = s; } } - return r == LONG_LONG_MIN ? 0 : r; + return ans == LONG_LONG_MIN ? 0 : ans; } -}; +}; \ No newline at end of file diff --git a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.cs b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.cs new file mode 100644 index 0000000000000..cc1e6624e4cf4 --- /dev/null +++ b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.cs @@ -0,0 +1,22 @@ +public class Solution { + public long MaximumSubarraySum(int[] nums, int k) { + Dictionary p = new Dictionary(); + p[nums[0]] = 0L; + long s = 0; + int n = nums.Length; + long ans = long.MinValue; + for (int i = 0; i < n; ++i) { + s += nums[i]; + if (p.ContainsKey(nums[i] - k)) { + ans = Math.Max(ans, s - p[nums[i] - k]); + } + if (p.ContainsKey(nums[i] + k)) { + ans = Math.Max(ans, s - p[nums[i] + k]); + } + if (i + 1 < n && (!p.ContainsKey(nums[i + 1]) || p[nums[i + 1]] > s)) { + p[nums[i + 1]] = s; + } + } + return ans == long.MinValue ? 0 : ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.go b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.go index b14d02d611e87..2b35845131bc8 100644 --- a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.go +++ b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.go @@ -1,26 +1,25 @@ func maximumSubarraySum(nums []int, k int) int64 { - p := make(map[int]int64) - var r int64 = math.MinInt64 - p[nums[0]] = 0 - var s int64 = 0 - n := len(nums) - for i := 0; ; i++ { - s += int64(nums[i]) - if t, ok := p[nums[i]-k]; ok { - r = max(r, s-t) - } - if t, ok := p[nums[i]+k]; ok { - r = max(r, s-t) - } - if i+1 == n { - break - } - if t, ok := p[nums[i+1]]; !ok || t > s { - p[nums[i+1]] = s - } - } - if r == math.MinInt64 { - return 0 - } - return r -} + p := map[int]int64{nums[0]: 0} + var s int64 = 0 + n := len(nums) + var ans int64 = math.MinInt64 + for i, x := range nums { + s += int64(x) + if t, ok := p[nums[i]-k]; ok { + ans = max(ans, s-t) + } + if t, ok := p[nums[i]+k]; ok { + ans = max(ans, s-t) + } + if i+1 == n { + break + } + if t, ok := p[nums[i+1]]; !ok || s < t { + p[nums[i+1]] = s + } + } + if ans == math.MinInt64 { + return 0 + } + return ans +} \ No newline at end of file diff --git a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.java b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.java index 4eeb6c8e0d2a9..9a75429b010ef 100644 --- a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.java +++ b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.java @@ -1,23 +1,22 @@ class Solution { public long maximumSubarraySum(int[] nums, int k) { - HashMap p = new HashMap<>(); - long r = Long.MIN_VALUE; + Map p = new HashMap<>(); p.put(nums[0], 0L); long s = 0; int n = nums.length; - for (int i = 0;; ++i) { + long ans = Long.MIN_VALUE; + for (int i = 0; i < n; ++i) { s += nums[i]; if (p.containsKey(nums[i] - k)) { - r = Math.max(r, s - p.get(nums[i] - k)); + ans = Math.max(ans, s - p.get(nums[i] - k)); } if (p.containsKey(nums[i] + k)) { - r = Math.max(r, s - p.get(nums[i] + k)); + ans = Math.max(ans, s - p.get(nums[i] + k)); } - if (i + 1 == n) break; - if (!p.containsKey(nums[i + 1]) || p.get(nums[i + 1]) > s) { + if (i + 1 < n && (!p.containsKey(nums[i + 1]) || p.get(nums[i + 1]) > s)) { p.put(nums[i + 1], s); } } - return r == Long.MIN_VALUE ? 0 : r; + return ans == Long.MIN_VALUE ? 0 : ans; } -} +} \ No newline at end of file diff --git a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.py b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.py index 10a5950f11e26..47e577b2288df 100644 --- a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.py +++ b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.py @@ -1,18 +1,14 @@ class Solution: def maximumSubarraySum(self, nums: List[int], k: int) -> int: - p = {} - r = float('-inf') - p[nums[0]] = 0 - s = 0 - n = len(nums) - for i in range(n): - s += nums[i] - if nums[i] - k in p: - r = max(r, s - p[nums[i] - k]) - if nums[i] + k in p: - r = max(r, s - p[nums[i] + k]) - if i + 1 == n: - break - if nums[i + 1] not in p or p[nums[i + 1]] > s: + ans = -inf + p = {nums[0]: 0} + s, n = 0, len(nums) + for i, x in enumerate(nums): + s += x + if x - k in p: + ans = max(ans, s - p[x - k]) + if x + k in p: + ans = max(ans, s - p[x + k]) + if i + 1 < n and (nums[i + 1] not in p or p[nums[i + 1]] > s): p[nums[i + 1]] = s - return r if r != float('-inf') else 0 + return 0 if ans == -inf else ans diff --git a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.ts b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.ts index a7b50bf7dcec9..4794d21c4f358 100644 --- a/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.ts +++ b/solution/3000-3099/3026.Maximum Good Subarray Sum/Solution.ts @@ -1,24 +1,20 @@ function maximumSubarraySum(nums: number[], k: number): number { const p: Map = new Map(); - let r: number = Number.MIN_SAFE_INTEGER; p.set(nums[0], 0); + let ans: number = -Infinity; let s: number = 0; const n: number = nums.length; - for (let i = 0; ; ++i) { + for (let i = 0; i < n; ++i) { s += nums[i]; - let t: number | undefined = p.get(nums[i] - k); - if (t !== undefined) { - r = Math.max(r, s - t); + if (p.has(nums[i] - k)) { + ans = Math.max(ans, s - p.get(nums[i] - k)!); } - t = p.get(nums[i] + k); - if (t !== undefined) { - r = Math.max(r, s - t); + if (p.has(nums[i] + k)) { + ans = Math.max(ans, s - p.get(nums[i] + k)!); } - if (i + 1 === n) break; - t = p.get(nums[i + 1]); - if (t === undefined || t > s) { + if (i + 1 < n && (!p.has(nums[i + 1]) || p.get(nums[i + 1])! > s)) { p.set(nums[i + 1], s); } } - return r === Number.MIN_SAFE_INTEGER ? 0 : r; + return ans === -Infinity ? 0 : ans; } diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md b/solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md index 476144a64e9fc..ba07e886829d7 100644 --- a/solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/README.md @@ -189,6 +189,29 @@ function numberOfPairs(points: number[][]): number { } ``` +```cs +public class Solution { + public int NumberOfPairs(int[][] points) { + Array.Sort(points, (a, b) => a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.Length; + int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} +``` + diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/README_EN.md b/solution/3000-3099/3027.Find the Number of Ways to Place People II/README_EN.md index 26a93bebcd7c8..709ca4561824b 100644 --- a/solution/3000-3099/3027.Find the Number of Ways to Place People II/README_EN.md +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/README_EN.md @@ -178,6 +178,29 @@ function numberOfPairs(points: number[][]): number { } ``` +```cs +public class Solution { + public int NumberOfPairs(int[][] points) { + Array.Sort(points, (a, b) => a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.Length; + int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} +``` + diff --git a/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.cs b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.cs new file mode 100644 index 0000000000000..41257789e8d60 --- /dev/null +++ b/solution/3000-3099/3027.Find the Number of Ways to Place People II/Solution.cs @@ -0,0 +1,20 @@ +public class Solution { + public int NumberOfPairs(int[][] points) { + Array.Sort(points, (a, b) => a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); + int ans = 0; + int n = points.Length; + int inf = 1 << 30; + for (int i = 0; i < n; ++i) { + int y1 = points[i][1]; + int maxY = -inf; + for (int j = i + 1; j < n; ++j) { + int y2 = points[j][1]; + if (maxY < y2 && y2 <= y1) { + maxY = y2; + ++ans; + } + } + } + return ans; + } +} \ No newline at end of file