Skip to content

feat: add solutions to lc problem: No.2107 #1915

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
merged 1 commit into from
Nov 2, 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
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class Solution:
cnt = Counter(candies[k:])
ans = len(cnt)
for i in range(k, len(candies)):
cnt[candies[i]] -= 1
cnt[candies[i - k]] += 1
cnt[candies[i]] -= 1
if cnt[candies[i]] == 0:
cnt.pop(candies[i])
ans = max(ans, len(cnt))
Expand All @@ -104,11 +104,11 @@ class Solution {
cnt.merge(candies[i], 1, Integer::sum);
}
int ans = cnt.size();
for (int i = k; i < candies.length; ++i) {
for (int i = k; i < n; ++i) {
cnt.merge(candies[i - k], 1, Integer::sum);
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
cnt.remove(candies[i]);
}
cnt.merge(candies[i - k], 1, Integer::sum);
ans = Math.max(ans, cnt.size());
}
return ans;
Expand All @@ -128,11 +128,11 @@ public:
++cnt[candies[i]];
}
int ans = cnt.size();
for (int i = k; i < candies.size(); ++i) {
for (int i = k; i < n; ++i) {
++cnt[candies[i - k]];
if (--cnt[candies[i]] == 0) {
cnt.erase(candies[i]);
}
++cnt[candies[i - k]];
ans = max(ans, (int) cnt.size());
}
return ans;
Expand All @@ -150,21 +150,69 @@ func shareCandies(candies []int, k int) (ans int) {
}
ans = len(cnt)
for i := k; i < len(candies); i++ {
cnt[candies[i-k]]++
cnt[candies[i]]--
if cnt[candies[i]] == 0 {
delete(cnt, candies[i])
}
cnt[candies[i-k]]++
ans = max(ans, len(cnt))
}
return
}
```

### **Rust**

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

impl Solution {
pub fn share_candies(candies: Vec<i32>, k: i32) -> i32 {
let mut cnt = HashMap::new();
let n = candies.len();

for i in k as usize..n {
*cnt.entry(candies[i]).or_insert(0) += 1;
}

let mut ans = cnt.len() as i32;

for i in k as usize..n {
*cnt.entry(candies[i - k as usize]).or_insert(0) += 1;
if let Some(x) = cnt.get_mut(&candies[i]) {
*x -= 1;
if *x == 0 {
cnt.remove(&candies[i]);
}
}

ans = ans.max(cnt.len() as i32);
}

ans
}
}
```

### **TypeScript**

```ts

function shareCandies(candies: number[], k: number): number {
const cnt: Map<number, number> = new Map();
for (const x of candies.slice(k)) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
let ans = cnt.size;
for (let i = k; i < candies.length; ++i) {
cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1);
cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1);
if (cnt.get(candies[i]) === 0) {
cnt.delete(candies[i]);
}
ans = Math.max(ans, cnt.size);
}
return ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ There are 3 unique flavors, so return 3.

## Solutions

**Solution 1: Sliding Window + Hash Table**

We can maintain a sliding window of size $k$, where the candies outside the window are for ourselves, and the $k$ candies inside the window are shared with our sister and mother. We can use a hash table $cnt$ to record the flavors of the candies outside the window and their corresponding quantities.

Initially, the hash table $cnt$ stores the flavors of the candies from $candies[k]$ to $candies[n-1]$ and their corresponding quantities. At this time, the number of candy flavors is the size of the hash table $cnt$, that is, $ans = cnt.size()$.

Next, we traverse the candies in the range $[k,..n-1]$, add the current candy $candies[i]$ to the window, and move the candy $candies[i-k]$ on the left side of the window out of the window. Then we update the hash table $cnt$, and update the number of candy flavors $ans$ to $max(ans, cnt.size())$.

After traversing all the candies, we can get the maximum number of unique flavors of candies that can be retained.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of candies.

<!-- tabs:start -->

### **Python3**
Expand All @@ -64,8 +76,8 @@ class Solution:
cnt = Counter(candies[k:])
ans = len(cnt)
for i in range(k, len(candies)):
cnt[candies[i]] -= 1
cnt[candies[i - k]] += 1
cnt[candies[i]] -= 1
if cnt[candies[i]] == 0:
cnt.pop(candies[i])
ans = max(ans, len(cnt))
Expand All @@ -83,11 +95,11 @@ class Solution {
cnt.merge(candies[i], 1, Integer::sum);
}
int ans = cnt.size();
for (int i = k; i < candies.length; ++i) {
for (int i = k; i < n; ++i) {
cnt.merge(candies[i - k], 1, Integer::sum);
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
cnt.remove(candies[i]);
}
cnt.merge(candies[i - k], 1, Integer::sum);
ans = Math.max(ans, cnt.size());
}
return ans;
Expand All @@ -107,11 +119,11 @@ public:
++cnt[candies[i]];
}
int ans = cnt.size();
for (int i = k; i < candies.size(); ++i) {
for (int i = k; i < n; ++i) {
++cnt[candies[i - k]];
if (--cnt[candies[i]] == 0) {
cnt.erase(candies[i]);
}
++cnt[candies[i - k]];
ans = max(ans, (int) cnt.size());
}
return ans;
Expand All @@ -129,21 +141,69 @@ func shareCandies(candies []int, k int) (ans int) {
}
ans = len(cnt)
for i := k; i < len(candies); i++ {
cnt[candies[i-k]]++
cnt[candies[i]]--
if cnt[candies[i]] == 0 {
delete(cnt, candies[i])
}
cnt[candies[i-k]]++
ans = max(ans, len(cnt))
}
return
}
```

### **Rust**

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

impl Solution {
pub fn share_candies(candies: Vec<i32>, k: i32) -> i32 {
let mut cnt = HashMap::new();
let n = candies.len();

for i in k as usize..n {
*cnt.entry(candies[i]).or_insert(0) += 1;
}

let mut ans = cnt.len() as i32;

for i in k as usize..n {
*cnt.entry(candies[i - k as usize]).or_insert(0) += 1;
if let Some(x) = cnt.get_mut(&candies[i]) {
*x -= 1;
if *x == 0 {
cnt.remove(&candies[i]);
}
}

ans = ans.max(cnt.len() as i32);
}

ans
}
}
```

### **TypeScript**

```ts

function shareCandies(candies: number[], k: number): number {
const cnt: Map<number, number> = new Map();
for (const x of candies.slice(k)) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
let ans = cnt.size;
for (let i = k; i < candies.length; ++i) {
cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1);
cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1);
if (cnt.get(candies[i]) === 0) {
cnt.delete(candies[i]);
}
ans = Math.max(ans, cnt.size);
}
return ans;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class Solution {
public:
int shareCandies(vector<int>& candies, int k) {
unordered_map<int, int> cnt;
int n = candies.size();
for (int i = k; i < n; ++i) {
++cnt[candies[i]];
}
int ans = cnt.size();
for (int i = k; i < candies.size(); ++i) {
if (--cnt[candies[i]] == 0) {
cnt.erase(candies[i]);
}
++cnt[candies[i - k]];
ans = max(ans, (int) cnt.size());
}
return ans;
}
class Solution {
public:
int shareCandies(vector<int>& candies, int k) {
unordered_map<int, int> cnt;
int n = candies.size();
for (int i = k; i < n; ++i) {
++cnt[candies[i]];
}
int ans = cnt.size();
for (int i = k; i < n; ++i) {
++cnt[candies[i - k]];
if (--cnt[candies[i]] == 0) {
cnt.erase(candies[i]);
}
ans = max(ans, (int) cnt.size());
}
return ans;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ func shareCandies(candies []int, k int) (ans int) {
}
ans = len(cnt)
for i := k; i < len(candies); i++ {
cnt[candies[i-k]]++
cnt[candies[i]]--
if cnt[candies[i]] == 0 {
delete(cnt, candies[i])
}
cnt[candies[i-k]]++
ans = max(ans, len(cnt))
}
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class Solution {
public int shareCandies(int[] candies, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
int n = candies.length;
for (int i = k; i < n; ++i) {
cnt.merge(candies[i], 1, Integer::sum);
}
int ans = cnt.size();
for (int i = k; i < candies.length; ++i) {
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
cnt.remove(candies[i]);
}
cnt.merge(candies[i - k], 1, Integer::sum);
ans = Math.max(ans, cnt.size());
}
return ans;
}
class Solution {
public int shareCandies(int[] candies, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
int n = candies.length;
for (int i = k; i < n; ++i) {
cnt.merge(candies[i], 1, Integer::sum);
}
int ans = cnt.size();
for (int i = k; i < n; ++i) {
cnt.merge(candies[i - k], 1, Integer::sum);
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
cnt.remove(candies[i]);
}
ans = Math.max(ans, cnt.size());
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution:
def shareCandies(self, candies: List[int], k: int) -> int:
cnt = Counter(candies[k:])
ans = len(cnt)
for i in range(k, len(candies)):
cnt[candies[i]] -= 1
cnt[candies[i - k]] += 1
if cnt[candies[i]] == 0:
cnt.pop(candies[i])
ans = max(ans, len(cnt))
return ans
class Solution:
def shareCandies(self, candies: List[int], k: int) -> int:
cnt = Counter(candies[k:])
ans = len(cnt)
for i in range(k, len(candies)):
cnt[candies[i - k]] += 1
cnt[candies[i]] -= 1
if cnt[candies[i]] == 0:
cnt.pop(candies[i])
ans = max(ans, len(cnt))
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::collections::HashMap;

impl Solution {
pub fn share_candies(candies: Vec<i32>, k: i32) -> i32 {
let mut cnt = HashMap::new();
let n = candies.len();

for i in k as usize..n {
*cnt.entry(candies[i]).or_insert(0) += 1;
}

let mut ans = cnt.len() as i32;

for i in k as usize..n {
*cnt.entry(candies[i - k as usize]).or_insert(0) += 1;
if let Some(x) = cnt.get_mut(&candies[i]) {
*x -= 1;
if *x == 0 {
cnt.remove(&candies[i]);
}
}

ans = ans.max(cnt.len() as i32);
}

ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function shareCandies(candies: number[], k: number): number {
const cnt: Map<number, number> = new Map();
for (const x of candies.slice(k)) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
let ans = cnt.size;
for (let i = k; i < candies.length; ++i) {
cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1);
cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1);
if (cnt.get(candies[i]) === 0) {
cnt.delete(candies[i]);
}
ans = Math.max(ans, cnt.size);
}
return ans;
}