diff --git a/2023_09_18/.vscode/launch.json b/2023_09_18/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/2023_09_18/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/2023_09_18/learders_in_an_array/src/main.rs b/2023_09_18/learders_in_an_array/src/main.rs index 1769b99..51dac0c 100644 --- a/2023_09_18/learders_in_an_array/src/main.rs +++ b/2023_09_18/learders_in_an_array/src/main.rs @@ -1,6 +1,5 @@ // Given an array A of positive integers. Your task is to find the leaders in the array. An element of array is leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader. -// Declare a function that takes a slice of i32 as input and returns a Vec fn leaders_in_an_array(arr: &[i32]) -> Vec { // Empty vector to store the leaders let mut leaders = Vec::new(); @@ -13,6 +12,7 @@ fn leaders_in_an_array(arr: &[i32]) -> Vec { if arr[i] > max { // Set the max value to the current element max = arr[i]; + // Add the max value to the leaders vector leaders.push(max); } } diff --git a/2023_09_18/missing_number/Cargo.toml b/2023_09_18/missing_number/Cargo.toml new file mode 100644 index 0000000..76e2132 --- /dev/null +++ b/2023_09_18/missing_number/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "missing_number" +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/2023_09_18/missing_number/README.md b/2023_09_18/missing_number/README.md new file mode 100644 index 0000000..b89150e --- /dev/null +++ b/2023_09_18/missing_number/README.md @@ -0,0 +1,24 @@ +# Comments on the solution + +We are using a simple mathematical approach that it's pretty much self-explanatory. We can express the sum of all numbers from `0` to `n` as `n * (n + 1) / 2`. We can then subtract the sum of the array from the sum of all numbers from `0` to `n` to get the missing number. + +### Complexity + +- **Time Complexity:** `O(n)` - We are iterating over the array once. +- **Space Complexity:** `O(1)` - We are not using any extra space. + +--- + +### Solution in Rust + +```rust +fn missing_number(nums: Vec) -> i32 { + (1..=nums.len() as i32).sum::() - nums.iter().sum::() +} +``` + +where we use the `..=` range operator to create a range from `1` to the length of the input vector, and then sum the values in that range using the `sum` method + +### Leetcode result + +![](https://i.imgur.com/qW2tNVm.png) diff --git a/2023_09_18/missing_number/src/main.rs b/2023_09_18/missing_number/src/main.rs new file mode 100644 index 0000000..2a8e052 --- /dev/null +++ b/2023_09_18/missing_number/src/main.rs @@ -0,0 +1,10 @@ +// Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. + +fn missing_number(nums: Vec) -> i32 { + (1..=nums.len() as i32).sum::() - nums.iter().sum::() +} + +fn main() { + let nums = vec![3, 0, 1]; + println!("{}", missing_number(nums)); +}