From a66b591f230e784aae2aaaf8af583610b08fd2d8 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 1 Nov 2023 19:32:18 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problems: No.1787,1788 * No.1787.Make the XOR of All Segments Equal to Zero * No.1788.Maximize the Beauty of the Garden --- .../README.md | 14 ++-- .../README_EN.md | 14 ++-- .../Solution.cpp | 48 ++++++------- .../Solution.java | 72 +++++++++---------- .../README.md | 59 ++++++++++++++- .../README_EN.md | 59 ++++++++++++++- .../Solution.java | 33 ++++----- .../Solution.py | 24 +++---- .../Solution.rs | 20 ++++++ .../Solution.ts | 16 +++++ 10 files changed, 253 insertions(+), 106 deletions(-) create mode 100644 solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.rs create mode 100644 solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.ts diff --git a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README.md b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README.md index 30659ce3385fc..1754cd332ecd3 100644 --- a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README.md +++ b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README.md @@ -111,16 +111,16 @@ class Solution { public int minChanges(int[] nums, int k) { int n = 1 << 10; Map[] cnt = new Map[k]; + Arrays.setAll(cnt, i -> new HashMap()); int[] size = new int[k]; - for (int i = 0; i < k; ++i) { - cnt[i] = new HashMap(); - } for (int i = 0; i < nums.length; ++i) { - cnt[i % k].put(nums[i], cnt[i % k].getOrDefault(nums[i], 0) + 1); + int j = i % k; + cnt[j].merge(nums[i], 1, Integer::sum); size[i % k]++; } int[] f = new int[n]; - Arrays.fill(f, 0x3f3f3f3f); + final int inf = 1 << 30; + Arrays.fill(f, inf); f[0] = 0; for (int i = 0; i < k; ++i) { int[] g = new int[n]; @@ -153,13 +153,13 @@ class Solution { public: int minChanges(vector& nums, int k) { int n = 1 << 10; - vector> cnt(k); + unordered_map cnt[k]; vector size(k); for (int i = 0; i < nums.size(); ++i) { cnt[i % k][nums[i]]++; size[i % k]++; } - vector f(n, 0x3f3f3f3f); + vector f(n, 1 << 30); f[0] = 0; for (int i = 0; i < k; ++i) { int mi = *min_element(f.begin(), f.end()); diff --git a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md index 793fc28b0feb4..92fe08665f171 100644 --- a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md +++ b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md @@ -73,16 +73,16 @@ class Solution { public int minChanges(int[] nums, int k) { int n = 1 << 10; Map[] cnt = new Map[k]; + Arrays.setAll(cnt, i -> new HashMap()); int[] size = new int[k]; - for (int i = 0; i < k; ++i) { - cnt[i] = new HashMap(); - } for (int i = 0; i < nums.length; ++i) { - cnt[i % k].put(nums[i], cnt[i % k].getOrDefault(nums[i], 0) + 1); + int j = i % k; + cnt[j].merge(nums[i], 1, Integer::sum); size[i % k]++; } int[] f = new int[n]; - Arrays.fill(f, 0x3f3f3f3f); + final int inf = 1 << 30; + Arrays.fill(f, inf); f[0] = 0; for (int i = 0; i < k; ++i) { int[] g = new int[n]; @@ -115,13 +115,13 @@ class Solution { public: int minChanges(vector& nums, int k) { int n = 1 << 10; - vector> cnt(k); + unordered_map cnt[k]; vector size(k); for (int i = 0; i < nums.size(); ++i) { cnt[i % k][nums[i]]++; size[i % k]++; } - vector f(n, 0x3f3f3f3f); + vector f(n, 1 << 30); f[0] = 0; for (int i = 0; i < k; ++i) { int mi = *min_element(f.begin(), f.end()); diff --git a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.cpp b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.cpp index 726bae600d766..7c64695afe09f 100644 --- a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.cpp +++ b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.cpp @@ -1,25 +1,25 @@ -class Solution { -public: - int minChanges(vector& nums, int k) { - int n = 1 << 10; - vector> cnt(k); - vector size(k); - for (int i = 0; i < nums.size(); ++i) { - cnt[i % k][nums[i]]++; - size[i % k]++; - } - vector f(n, 0x3f3f3f3f); - f[0] = 0; - for (int i = 0; i < k; ++i) { - int mi = *min_element(f.begin(), f.end()); - vector g(n, mi + size[i]); - for (int j = 0; j < n; ++j) { - for (auto& [v, c] : cnt[i]) { - g[j] = min(g[j], f[j ^ v] + size[i] - c); - } - } - f = move(g); - } - return f[0]; - } +class Solution { +public: + int minChanges(vector& nums, int k) { + int n = 1 << 10; + unordered_map cnt[k]; + vector size(k); + for (int i = 0; i < nums.size(); ++i) { + cnt[i % k][nums[i]]++; + size[i % k]++; + } + vector f(n, 1 << 30); + f[0] = 0; + for (int i = 0; i < k; ++i) { + int mi = *min_element(f.begin(), f.end()); + vector g(n, mi + size[i]); + for (int j = 0; j < n; ++j) { + for (auto& [v, c] : cnt[i]) { + g[j] = min(g[j], f[j ^ v] + size[i] - c); + } + } + f = move(g); + } + return f[0]; + } }; \ No newline at end of file diff --git a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.java b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.java index 020ec36162444..ea794ff6c7f8c 100644 --- a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.java +++ b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.java @@ -1,37 +1,37 @@ -class Solution { - public int minChanges(int[] nums, int k) { - int n = 1 << 10; - Map[] cnt = new Map[k]; - int[] size = new int[k]; - for (int i = 0; i < k; ++i) { - cnt[i] = new HashMap(); - } - for (int i = 0; i < nums.length; ++i) { - cnt[i % k].put(nums[i], cnt[i % k].getOrDefault(nums[i], 0) + 1); - size[i % k]++; - } - int[] f = new int[n]; - Arrays.fill(f, 0x3f3f3f3f); - f[0] = 0; - for (int i = 0; i < k; ++i) { - int[] g = new int[n]; - Arrays.fill(g, min(f) + size[i]); - for (int j = 0; j < n; ++j) { - for (var e : cnt[i].entrySet()) { - int v = e.getKey(), c = e.getValue(); - g[j] = Math.min(g[j], f[j ^ v] + size[i] - c); - } - } - f = g; - } - return f[0]; - } - - private int min(int[] arr) { - int mi = arr[0]; - for (int v : arr) { - mi = Math.min(mi, v); - } - return mi; - } +class Solution { + public int minChanges(int[] nums, int k) { + int n = 1 << 10; + Map[] cnt = new Map[k]; + Arrays.setAll(cnt, i -> new HashMap()); + int[] size = new int[k]; + for (int i = 0; i < nums.length; ++i) { + int j = i % k; + cnt[j].merge(nums[i], 1, Integer::sum); + size[i % k]++; + } + int[] f = new int[n]; + final int inf = 1 << 30; + Arrays.fill(f, inf); + f[0] = 0; + for (int i = 0; i < k; ++i) { + int[] g = new int[n]; + Arrays.fill(g, min(f) + size[i]); + for (int j = 0; j < n; ++j) { + for (var e : cnt[i].entrySet()) { + int v = e.getKey(), c = e.getValue(); + g[j] = Math.min(g[j], f[j ^ v] + size[i] - c); + } + } + f = g; + } + return f[0]; + } + + private int min(int[] arr) { + int mi = arr[0]; + for (int v : arr) { + mi = Math.min(mi, v); + } + return mi; + } } \ No newline at end of file diff --git a/solution/1700-1799/1788.Maximize the Beauty of the Garden/README.md b/solution/1700-1799/1788.Maximize the Beauty of the Garden/README.md index 058f6f507eb0a..a3b2b8db17f1d 100644 --- a/solution/1700-1799/1788.Maximize the Beauty of the Garden/README.md +++ b/solution/1700-1799/1788.Maximize the Beauty of the Garden/README.md @@ -55,6 +55,14 @@ +**方法一:哈希表 + 前缀和** + +我们用哈希表 $d$ 记录每个美观度第一次出现的位置,用前缀和数组 $s$ 记录当前位置之前的美观度之和。如果一个美观度 $v$ 在位置 $i$ 和 $j$ 出现过(其中 $i \lt j$),那么我们可以得到一个有效的花园 $[i+1,j],ドル其美观度为 $s[i] - s[j + 1] + v \times 2,ドル我们用这个值更新答案。否则,我们将当前美观度所在的位置 $i$ 记录到哈希表 $d$ 中。接下来,我们更新前缀和,如果美观度 $v$ 为负数,我们将其视为 0ドル$。 + +遍历完所有的美观度之后,我们就可以得到答案。 + +时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为花朵的数量。 + ### **Python3** @@ -83,10 +91,11 @@ class Solution: ```java class Solution { public int maximumBeauty(int[] flowers) { - int[] s = new int[flowers.length + 1]; + int n = flowers.length; + int[] s = new int[n + 1]; Map d = new HashMap(); int ans = Integer.MIN_VALUE; - for (int i = 0; i < flowers.length; ++i) { + for (int i = 0; i < n; ++i) { int v = flowers[i]; if (d.containsKey(v)) { ans = Math.max(ans, s[i] - s[d.get(v) + 1] + v * 2); @@ -144,6 +153,52 @@ func maximumBeauty(flowers []int) int { } ``` +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn maximum_beauty(flowers: Vec) -> i32 { + let mut s = vec![0; flowers.len() + 1]; + let mut d = HashMap::new(); + let mut ans = i32::MIN; + + for (i, &v) in flowers.iter().enumerate() { + if let Some(&j) = d.get(&v) { + ans = ans.max(s[i] - s[j + 1] + v * 2); + } else { + d.insert(v, i); + } + s[i + 1] = s[i] + v.max(0); + } + + ans + } +} +``` + +### **TypeScript** + +```ts +function maximumBeauty(flowers: number[]): number { + const n = flowers.length; + const s: number[] = Array(n + 1).fill(0); + const d: Map = new Map(); + let ans = -Infinity; + for (let i = 0; i < n; ++i) { + const v = flowers[i]; + if (d.has(v)) { + ans = Math.max(ans, s[i] - s[d.get(v)! + 1] + v * 2); + } else { + d.set(v, i); + } + s[i + 1] = s[i] + Math.max(v, 0); + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/1700-1799/1788.Maximize the Beauty of the Garden/README_EN.md b/solution/1700-1799/1788.Maximize the Beauty of the Garden/README_EN.md index 2e9c71ac217eb..54607e447715e 100644 --- a/solution/1700-1799/1788.Maximize the Beauty of the Garden/README_EN.md +++ b/solution/1700-1799/1788.Maximize the Beauty of the Garden/README_EN.md @@ -52,6 +52,14 @@ ## Solutions +**Solution 1: Hash Table + Prefix Sum** + +We use a hash table $d$ to record the first occurrence of each aesthetic value, and a prefix sum array $s$ to record the sum of the aesthetic values before the current position. If an aesthetic value $v$ appears at positions $i$ and $j$ (where $i \lt j$), then we can get a valid garden $[i+1,j],ドル whose aesthetic value is $s[i] - s[j + 1] + v \times 2$. We use this value to update the answer. Otherwise, we record the current position $i$ of the aesthetic value in the hash table $d$. Next, we update the prefix sum. If the aesthetic value $v$ is negative, we treat it as 0ドル$. + +After traversing all the aesthetic values, we can get the answer. + +The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the number of flowers. + ### **Python3** @@ -76,10 +84,11 @@ class Solution: ```java class Solution { public int maximumBeauty(int[] flowers) { - int[] s = new int[flowers.length + 1]; + int n = flowers.length; + int[] s = new int[n + 1]; Map d = new HashMap(); int ans = Integer.MIN_VALUE; - for (int i = 0; i < flowers.length; ++i) { + for (int i = 0; i < n; ++i) { int v = flowers[i]; if (d.containsKey(v)) { ans = Math.max(ans, s[i] - s[d.get(v) + 1] + v * 2); @@ -137,6 +146,52 @@ func maximumBeauty(flowers []int) int { } ``` +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn maximum_beauty(flowers: Vec) -> i32 { + let mut s = vec![0; flowers.len() + 1]; + let mut d = HashMap::new(); + let mut ans = i32::MIN; + + for (i, &v) in flowers.iter().enumerate() { + if let Some(&j) = d.get(&v) { + ans = ans.max(s[i] - s[j + 1] + v * 2); + } else { + d.insert(v, i); + } + s[i + 1] = s[i] + v.max(0); + } + + ans + } +} +``` + +### **TypeScript** + +```ts +function maximumBeauty(flowers: number[]): number { + const n = flowers.length; + const s: number[] = Array(n + 1).fill(0); + const d: Map = new Map(); + let ans = -Infinity; + for (let i = 0; i < n; ++i) { + const v = flowers[i]; + if (d.has(v)) { + ans = Math.max(ans, s[i] - s[d.get(v)! + 1] + v * 2); + } else { + d.set(v, i); + } + s[i + 1] = s[i] + Math.max(v, 0); + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.java b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.java index f19689abe4aa9..3b46759c59a66 100644 --- a/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.java +++ b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.java @@ -1,17 +1,18 @@ -class Solution { - public int maximumBeauty(int[] flowers) { - int[] s = new int[flowers.length + 1]; - Map d = new HashMap(); - int ans = Integer.MIN_VALUE; - for (int i = 0; i < flowers.length; ++i) { - int v = flowers[i]; - if (d.containsKey(v)) { - ans = Math.max(ans, s[i] - s[d.get(v) + 1] + v * 2); - } else { - d.put(v, i); - } - s[i + 1] = s[i] + Math.max(v, 0); - } - return ans; - } +class Solution { + public int maximumBeauty(int[] flowers) { + int n = flowers.length; + int[] s = new int[n + 1]; + Map d = new HashMap(); + int ans = Integer.MIN_VALUE; + for (int i = 0; i < n; ++i) { + int v = flowers[i]; + if (d.containsKey(v)) { + ans = Math.max(ans, s[i] - s[d.get(v) + 1] + v * 2); + } else { + d.put(v, i); + } + s[i + 1] = s[i] + Math.max(v, 0); + } + return ans; + } } \ No newline at end of file diff --git a/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.py b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.py index 7ffcfc339da2a..37a006ae5b305 100644 --- a/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.py +++ b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.py @@ -1,12 +1,12 @@ -class Solution: - def maximumBeauty(self, flowers: List[int]) -> int: - s = [0] * (len(flowers) + 1) - d = {} - ans = -inf - for i, v in enumerate(flowers): - if v in d: - ans = max(ans, s[i] - s[d[v] + 1] + v * 2) - else: - d[v] = i - s[i + 1] = s[i] + max(v, 0) - return ans +class Solution: + def maximumBeauty(self, flowers: List[int]) -> int: + s = [0] * (len(flowers) + 1) + d = {} + ans = -inf + for i, v in enumerate(flowers): + if v in d: + ans = max(ans, s[i] - s[d[v] + 1] + v * 2) + else: + d[v] = i + s[i + 1] = s[i] + max(v, 0) + return ans diff --git a/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.rs b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.rs new file mode 100644 index 0000000000000..ec7093a114f0d --- /dev/null +++ b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.rs @@ -0,0 +1,20 @@ +use std::collections::HashMap; + +impl Solution { + pub fn maximum_beauty(flowers: Vec) -> i32 { + let mut s = vec![0; flowers.len() + 1]; + let mut d = HashMap::new(); + let mut ans = i32::MIN; + + for (i, &v) in flowers.iter().enumerate() { + if let Some(&j) = d.get(&v) { + ans = ans.max(s[i] - s[j + 1] + v * 2); + } else { + d.insert(v, i); + } + s[i + 1] = s[i] + v.max(0); + } + + ans + } +} diff --git a/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.ts b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.ts new file mode 100644 index 0000000000000..5cbb230b66180 --- /dev/null +++ b/solution/1700-1799/1788.Maximize the Beauty of the Garden/Solution.ts @@ -0,0 +1,16 @@ +function maximumBeauty(flowers: number[]): number { + const n = flowers.length; + const s: number[] = Array(n + 1).fill(0); + const d: Map = new Map(); + let ans = -Infinity; + for (let i = 0; i < n; ++i) { + const v = flowers[i]; + if (d.has(v)) { + ans = Math.max(ans, s[i] - s[d.get(v)! + 1] + v * 2); + } else { + d.set(v, i); + } + s[i + 1] = s[i] + Math.max(v, 0); + } + return ans; +} From 5a18bd82fbcf12a3c3cfba3f235c8915b041228e Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 1 Nov 2023 19:44:44 +0800 Subject: [PATCH 2/2] fix: java code --- .../1787.Make the XOR of All Segments Equal to Zero/README.md | 2 +- .../README_EN.md | 2 +- .../Solution.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README.md b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README.md index 1754cd332ecd3..d722bfec42718 100644 --- a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README.md +++ b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README.md @@ -116,7 +116,7 @@ class Solution { for (int i = 0; i < nums.length; ++i) { int j = i % k; cnt[j].merge(nums[i], 1, Integer::sum); - size[i % k]++; + size[j]++; } int[] f = new int[n]; final int inf = 1 << 30; diff --git a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md index 92fe08665f171..eda58f953bc6b 100644 --- a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md +++ b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/README_EN.md @@ -78,7 +78,7 @@ class Solution { for (int i = 0; i < nums.length; ++i) { int j = i % k; cnt[j].merge(nums[i], 1, Integer::sum); - size[i % k]++; + size[j]++; } int[] f = new int[n]; final int inf = 1 << 30; diff --git a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.java b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.java index ea794ff6c7f8c..b8b899da38d63 100644 --- a/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.java +++ b/solution/1700-1799/1787.Make the XOR of All Segments Equal to Zero/Solution.java @@ -7,7 +7,7 @@ public int minChanges(int[] nums, int k) { for (int i = 0; i < nums.length; ++i) { int j = i % k; cnt[j].merge(nums[i], 1, Integer::sum); - size[i % k]++; + size[j]++; } int[] f = new int[n]; final int inf = 1 << 30;

AltStyle によって変換されたページ (->オリジナル) /