Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problems: No.1787,1788 #1912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
idoocs merged 2 commits into main from dev
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ class Solution {
public int minChanges(int[] nums, int k) {
int n = 1 << 10;
Map<Integer, Integer>[] 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);
size[i % k]++;
int j = i % k;
cnt[j].merge(nums[i], 1, Integer::sum);
size[j]++;
}
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];
Expand Down Expand Up @@ -153,13 +153,13 @@ class Solution {
public:
int minChanges(vector<int>& nums, int k) {
int n = 1 << 10;
vector<unordered_map<int, int>> cnt(k);
unordered_map<int, int> cnt[k];
vector<int> size(k);
for (int i = 0; i < nums.size(); ++i) {
cnt[i % k][nums[i]]++;
size[i % k]++;
}
vector<int> f(n, 0x3f3f3f3f);
vector<int> f(n, 1 << 30);
f[0] = 0;
for (int i = 0; i < k; ++i) {
int mi = *min_element(f.begin(), f.end());
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ class Solution {
public int minChanges(int[] nums, int k) {
int n = 1 << 10;
Map<Integer, Integer>[] 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);
size[i % k]++;
int j = i % k;
cnt[j].merge(nums[i], 1, Integer::sum);
size[j]++;
}
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];
Expand Down Expand Up @@ -115,13 +115,13 @@ class Solution {
public:
int minChanges(vector<int>& nums, int k) {
int n = 1 << 10;
vector<unordered_map<int, int>> cnt(k);
unordered_map<int, int> cnt[k];
vector<int> size(k);
for (int i = 0; i < nums.size(); ++i) {
cnt[i % k][nums[i]]++;
size[i % k]++;
}
vector<int> f(n, 0x3f3f3f3f);
vector<int> f(n, 1 << 30);
f[0] = 0;
for (int i = 0; i < k; ++i) {
int mi = *min_element(f.begin(), f.end());
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
class Solution {
public:
int minChanges(vector<int>& nums, int k) {
int n = 1 << 10;
vector<unordered_map<int, int>> cnt(k);
vector<int> size(k);
for (int i = 0; i < nums.size(); ++i) {
cnt[i % k][nums[i]]++;
size[i % k]++;
}
vector<int> f(n, 0x3f3f3f3f);
f[0] = 0;
for (int i = 0; i < k; ++i) {
int mi = *min_element(f.begin(), f.end());
vector<int> 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<int>& nums, int k) {
int n = 1 << 10;
unordered_map<int, int> cnt[k];
vector<int> size(k);
for (int i = 0; i < nums.size(); ++i) {
cnt[i % k][nums[i]]++;
size[i % k]++;
}
vector<int> f(n, 1 << 30);
f[0] = 0;
for (int i = 0; i < k; ++i) {
int mi = *min_element(f.begin(), f.end());
vector<int> 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];
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
class Solution {
public int minChanges(int[] nums, int k) {
int n = 1 << 10;
Map<Integer, Integer>[] 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<Integer, Integer>[] 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[j]++;
}
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;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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$ 为花朵的数量。

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -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<Integer, Integer> 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);
Expand Down Expand Up @@ -144,6 +153,52 @@ func maximumBeauty(flowers []int) int {
}
```

### **Rust**

```rust
use std::collections::HashMap;

impl Solution {
pub fn maximum_beauty(flowers: Vec<i32>) -> 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<number, number> = 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;
}
```

### **...**

```
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

### **Python3**
Expand All @@ -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<Integer, Integer> 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);
Expand Down Expand Up @@ -137,6 +146,52 @@ func maximumBeauty(flowers []int) int {
}
```

### **Rust**

```rust
use std::collections::HashMap;

impl Solution {
pub fn maximum_beauty(flowers: Vec<i32>) -> 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<number, number> = 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;
}
```

### **...**

```
Expand Down
Loading

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