diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp index c4f7595369057..d7d480c5562e6 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp @@ -15,7 +15,7 @@ class FindElements { root->val = 0; dfs(root); } - + bool find(int target) { return s.contains(target); } diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.java b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.java index 9861770263419..f36f14261252a 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.java +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.java @@ -20,7 +20,7 @@ public FindElements(TreeNode root) { root.val = 0; dfs(root); } - + public boolean find(int target) { return s.contains(target); } diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md index 30ec8ec9086f4..68c74e000925a 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README.md @@ -55,7 +55,7 @@ 由于题目中的 $n$ 范围较小,我们也可以直接创建一个大小为 $n+1$ 的数组来存放数据,运行效率较高。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $groupSizes$ 的长度。 @@ -95,7 +95,9 @@ public: vector> groupThePeople(vector& groupSizes) { int n = groupSizes.size(); vector> g(n + 1); - for (int i = 0; i < n; ++i) g[groupSizes[i]].push_back(i); + for (int i = 0; i < n; ++i) { + g[groupSizes[i]].push_back(i); + } vector> ans; for (int i = 0; i < g.size(); ++i) { for (int j = 0; j < g[i].size(); j += i) { @@ -127,82 +129,49 @@ func groupThePeople(groupSizes []int) [][]int { ```ts function groupThePeople(groupSizes: number[]): number[][] { - const res = []; - const map = new Map(); - const n = groupSizes.length; - for (let i = 0; i < n; i++) { - const size = groupSizes[i]; - map.set(size, [...(map.get(size) ?? []), i]); - const arr = map.get(size); - if (arr.length === size) { - res.push(arr); - map.set(size, []); + const n: number = groupSizes.length; + const g: number[][] = Array.from({ length: n + 1 }, () => []); + + for (let i = 0; i < groupSizes.length; i++) { + const size: number = groupSizes[i]; + g[size].push(i); + } + const ans: number[][] = []; + for (let i = 1; i <= n; i++) { + const group: number[] = []; + for (let j = 0; j < g[i].length; j += i) { + group.push(...g[i].slice(j, j + i)); + ans.push([...group]); + group.length = 0; } } - return res; + return ans; } ``` ```rust -use std::collections::HashMap; impl Solution { pub fn group_the_people(group_sizes: Vec) -> Vec> { - let mut res = vec![]; - let mut map = HashMap::new(); - for i in 0..group_sizes.len() { - let size = group_sizes[i] as usize; - let arr = map.entry(size).or_insert(vec![]); - arr.push(i as i32); - if arr.len() == size { - res.push(arr.clone()); - arr.clear(); - } - } - res - } -} -``` - - - -### 方法二 + let n: usize = group_sizes.len(); + let mut g: Vec> = vec![Vec::new(); n + 1]; - - -```python -class Solution: - def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: - g = defaultdict(list) - for i, x in enumerate(groupSizes): - g[x].append(i) - ans = [] - for x, idx in g.items(): - t = [] - for i in idx: - t.append(i) - if len(t) == x: - ans.append(t) - t = [] - return ans -``` + for (i, &size) in group_sizes.iter().enumerate() { + g[size as usize].push(i); + } -```rust -impl Solution { - #[allow(dead_code)] - pub fn group_the_people(group_sizes: Vec) -> Vec> { - let n = group_sizes.len(); - let mut g = vec![vec![]; n + 1]; - let mut ret = vec![]; - - for i in 0..n { - g[group_sizes[i] as usize].push(i as i32); - if g[group_sizes[i] as usize].len() == (group_sizes[i] as usize) { - ret.push(g[group_sizes[i] as usize].clone()); - g[group_sizes[i] as usize].clear(); + let mut ans: Vec> = Vec::new(); + for (i, v) in g.into_iter().enumerate() { + for j in (0..v.len()).step_by(i.max(1)) { + ans.push( + v[j..(j + i).min(v.len())] + .iter() + .map(|&x| x as i32) + .collect() + ); } } - ret + ans } } ``` diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md index 5951e4fdbef47..0127ff29d6425 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/README_EN.md @@ -45,7 +45,13 @@ Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]]. ## Solutions -### Solution 1 +### Solution 1: Hash Table or Array + +We use a hash table $g$ to store which people are in each group size $groupSize$. Then we partition each group size into $k$ equal parts, with each part containing $groupSize$ people. + +Since the range of $n$ in the problem is small, we can also directly create an array of size $n+1$ to store the data, which is more efficient. + +Time complexity is $O(n)$, and space complexity is $O(n)$. Here, $n$ is the length of $groupSizes$. @@ -85,7 +91,9 @@ public: vector> groupThePeople(vector& groupSizes) { int n = groupSizes.size(); vector> g(n + 1); - for (int i = 0; i < n; ++i) g[groupSizes[i]].push_back(i); + for (int i = 0; i < n; ++i) { + g[groupSizes[i]].push_back(i); + } vector> ans; for (int i = 0; i < g.size(); ++i) { for (int j = 0; j < g[i].size(); j += i) { @@ -117,82 +125,49 @@ func groupThePeople(groupSizes []int) [][]int { ```ts function groupThePeople(groupSizes: number[]): number[][] { - const res = []; - const map = new Map(); - const n = groupSizes.length; - for (let i = 0; i < n; i++) { - const size = groupSizes[i]; - map.set(size, [...(map.get(size) ?? []), i]); - const arr = map.get(size); - if (arr.length === size) { - res.push(arr); - map.set(size, []); + const n: number = groupSizes.length; + const g: number[][] = Array.from({ length: n + 1 }, () => []); + + for (let i = 0; i < groupSizes.length; i++) { + const size: number = groupSizes[i]; + g[size].push(i); + } + const ans: number[][] = []; + for (let i = 1; i <= n; i++) { + const group: number[] = []; + for (let j = 0; j < g[i].length; j += i) { + group.push(...g[i].slice(j, j + i)); + ans.push([...group]); + group.length = 0; } } - return res; + return ans; } ``` ```rust -use std::collections::HashMap; impl Solution { pub fn group_the_people(group_sizes: Vec) -> Vec> { - let mut res = vec![]; - let mut map = HashMap::new(); - for i in 0..group_sizes.len() { - let size = group_sizes[i] as usize; - let arr = map.entry(size).or_insert(vec![]); - arr.push(i as i32); - if arr.len() == size { - res.push(arr.clone()); - arr.clear(); - } - } - res - } -} -``` - - - -### Solution 2 - - + let n: usize = group_sizes.len(); + let mut g: Vec> = vec![Vec::new(); n + 1]; -```python -class Solution: - def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: - g = defaultdict(list) - for i, x in enumerate(groupSizes): - g[x].append(i) - ans = [] - for x, idx in g.items(): - t = [] - for i in idx: - t.append(i) - if len(t) == x: - ans.append(t) - t = [] - return ans -``` + for (i, &size) in group_sizes.iter().enumerate() { + g[size as usize].push(i); + } -```rust -impl Solution { - #[allow(dead_code)] - pub fn group_the_people(group_sizes: Vec) -> Vec> { - let n = group_sizes.len(); - let mut g = vec![vec![]; n + 1]; - let mut ret = vec![]; - - for i in 0..n { - g[group_sizes[i] as usize].push(i as i32); - if g[group_sizes[i] as usize].len() == (group_sizes[i] as usize) { - ret.push(g[group_sizes[i] as usize].clone()); - g[group_sizes[i] as usize].clear(); + let mut ans: Vec> = Vec::new(); + for (i, v) in g.into_iter().enumerate() { + for j in (0..v.len()).step_by(i.max(1)) { + ans.push( + v[j..(j + i).min(v.len())] + .iter() + .map(|&x| x as i32) + .collect() + ); } } - ret + ans } } ``` diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.cpp b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.cpp index f60f666a11edb..4f22bbd9f1544 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.cpp +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.cpp @@ -3,7 +3,9 @@ class Solution { vector> groupThePeople(vector& groupSizes) { int n = groupSizes.size(); vector> g(n + 1); - for (int i = 0; i < n; ++i) g[groupSizes[i]].push_back(i); + for (int i = 0; i < n; ++i) { + g[groupSizes[i]].push_back(i); + } vector> ans; for (int i = 0; i < g.size(); ++i) { for (int j = 0; j < g[i].size(); j += i) { diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.rs b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.rs index 5eab9172a63db..10621ee0149ac 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.rs +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.rs @@ -1,17 +1,24 @@ -use std::collections::HashMap; impl Solution { pub fn group_the_people(group_sizes: Vec) -> Vec> { - let mut res = vec![]; - let mut map = HashMap::new(); - for i in 0..group_sizes.len() { - let size = group_sizes[i] as usize; - let arr = map.entry(size).or_insert(vec![]); - arr.push(i as i32); - if arr.len() == size { - res.push(arr.clone()); - arr.clear(); + let n: usize = group_sizes.len(); + let mut g: Vec> = vec![Vec::new(); n + 1]; + + for (i, &size) in group_sizes.iter().enumerate() { + g[size as usize].push(i); + } + + let mut ans: Vec> = Vec::new(); + for (i, v) in g.into_iter().enumerate() { + for j in (0..v.len()).step_by(i.max(1)) { + ans.push( + v[j..(j + i).min(v.len())] + .iter() + .map(|&x| x as i32) + .collect() + ); } } - res + + ans } } diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.ts b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.ts index 377aecf32e3a5..4b0c219422236 100644 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.ts +++ b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.ts @@ -1,15 +1,19 @@ function groupThePeople(groupSizes: number[]): number[][] { - const res = []; - const map = new Map(); - const n = groupSizes.length; - for (let i = 0; i < n; i++) { - const size = groupSizes[i]; - map.set(size, [...(map.get(size) ?? []), i]); - const arr = map.get(size); - if (arr.length === size) { - res.push(arr); - map.set(size, []); + const n: number = groupSizes.length; + const g: number[][] = Array.from({ length: n + 1 }, () => []); + + for (let i = 0; i < groupSizes.length; i++) { + const size: number = groupSizes[i]; + g[size].push(i); + } + const ans: number[][] = []; + for (let i = 1; i <= n; i++) { + const group: number[] = []; + for (let j = 0; j < g[i].length; j += i) { + group.push(...g[i].slice(j, j + i)); + ans.push([...group]); + group.length = 0; } } - return res; + return ans; } diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution2.py b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution2.py deleted file mode 100644 index fc7f8fb0241a5..0000000000000 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution2.py +++ /dev/null @@ -1,14 +0,0 @@ -class Solution: - def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: - g = defaultdict(list) - for i, x in enumerate(groupSizes): - g[x].append(i) - ans = [] - for x, idx in g.items(): - t = [] - for i in idx: - t.append(i) - if len(t) == x: - ans.append(t) - t = [] - return ans diff --git a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution2.rs b/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution2.rs deleted file mode 100644 index 5f7f5ca3b2269..0000000000000 --- a/solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution2.rs +++ /dev/null @@ -1,18 +0,0 @@ -impl Solution { - #[allow(dead_code)] - pub fn group_the_people(group_sizes: Vec) -> Vec> { - let n = group_sizes.len(); - let mut g = vec![vec![]; n + 1]; - let mut ret = vec![]; - - for i in 0..n { - g[group_sizes[i] as usize].push(i as i32); - if g[group_sizes[i] as usize].len() == (group_sizes[i] as usize) { - ret.push(g[group_sizes[i] as usize].clone()); - g[group_sizes[i] as usize].clear(); - } - } - - ret - } -} diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md index 271a98eb8279b..827a8a24f340e 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md @@ -78,6 +78,16 @@ class Solution: return l ``` +```python +class Solution: + def smallestDivisor(self, nums: List[int], threshold: int) -> int: + def f(v: int) -> bool: + v += 1 + return sum((x + v - 1) // v for x in nums) <= threshold + + return bisect_left(range(max(nums)), True, key=f) + 1 +``` + ```java class Solution { public int smallestDivisor(int[] nums, int threshold) { @@ -204,20 +214,4 @@ public class Solution { -### 方法二 - - - -```python -class Solution: - def smallestDivisor(self, nums: List[int], threshold: int) -> int: - def f(v: int) -> bool: - v += 1 - return sum((x + v - 1) // v for x in nums) <= threshold - - return bisect_left(range(max(nums)), True, key=f) + 1 -``` - - - diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md index 0a0d114671c1c..4195732ce8459 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md @@ -65,6 +65,16 @@ class Solution: return l ``` +```python +class Solution: + def smallestDivisor(self, nums: List[int], threshold: int) -> int: + def f(v: int) -> bool: + v += 1 + return sum((x + v - 1) // v for x in nums) <= threshold + + return bisect_left(range(max(nums)), True, key=f) + 1 +``` + ```java class Solution { public int smallestDivisor(int[] nums, int threshold) { @@ -191,20 +201,4 @@ public class Solution { -### Solution 2 - - - -```python -class Solution: - def smallestDivisor(self, nums: List[int], threshold: int) -> int: - def f(v: int) -> bool: - v += 1 - return sum((x + v - 1) // v for x in nums) <= threshold - - return bisect_left(range(max(nums)), True, key=f) + 1 -``` - - -