diff options
author | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
commit | 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch) | |
tree | 0072cca328fe5adb2ed61004010228ff85e2164d /group-the-people-given-the-group-size-they-belong-to |
Diffstat (limited to 'group-the-people-given-the-group-size-they-belong-to')
-rw-r--r-- | group-the-people-given-the-group-size-they-belong-to/Cargo.toml | 8 | ||||
-rw-r--r-- | group-the-people-given-the-group-size-they-belong-to/src/main.rs | 26 |
2 files changed, 34 insertions, 0 deletions
diff --git a/group-the-people-given-the-group-size-they-belong-to/Cargo.toml b/group-the-people-given-the-group-size-they-belong-to/Cargo.toml new file mode 100644 index 0000000..5a9601f --- /dev/null +++ b/group-the-people-given-the-group-size-they-belong-to/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "group-the-people-given-the-group-size-they-belong-to" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/group-the-people-given-the-group-size-they-belong-to/src/main.rs b/group-the-people-given-the-group-size-they-belong-to/src/main.rs new file mode 100644 index 0000000..f380f39 --- /dev/null +++ b/group-the-people-given-the-group-size-they-belong-to/src/main.rs @@ -0,0 +1,26 @@ +fn main() { + println!("Hello, world!"); +} + +struct Solution {} +impl Solution { + pub fn group_the_people(group_sizes: Vec<i32>) -> Vec<Vec<i32>> { + let mut result: Vec<Vec<i32>> = Vec::new(); + + + for (i, size) in group_sizes.iter().enumerate() { + let group = result.iter_mut() + .find(|x| x.capacity() == *size as usize && x.len() != x.capacity()); + + match group { + Some(entry) => entry.push(i as i32), + None => { + let mut new_group = Vec::with_capacity(*size as usize); + new_group.push(i as i32); + result.push(new_group); + } + }; + } + result + } +} |