first two problems solved

main
Luca Lombardo 1 year ago
parent 4d59a022b3
commit b8a53afc00

@ -0,0 +1,8 @@
[package]
name = "learders_in_an_array"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,30 @@
# Comments on the solution
We know for sure that there will be at least one leader in the array (the last one). We can start from the end and keep track of the maximum value we have seen so far. If the current value is greater than the maximum value, then we have found a new leader. We can store it in a list and update the maximum value. In this way, we will have the leaders in the reverse order.
So after we have iterated through all the array, we can reverse the list and return it.
### Complexity
- **Time Complexity:** `O(n)` since we iterate through the array once and reverse the list once.
- **Space Complexity:** `O(n)` since we store the leaders in a list.
---
### Rust solution
```rust
fn leaders_in_an_array(arr: &[i32]) -> Vec<i32> {
let mut leaders = Vec::new();
let mut max = arr[arr.len() - 1];
leaders.push(max);
for i in (0..arr.len() - 1).rev() {
if arr[i] > max {
max = arr[i];
leaders.push(max);
}
}
leaders.reverse();
leaders
}
```

@ -0,0 +1,27 @@
// 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<i32>
fn leaders_in_an_array(arr: &[i32]) -> Vec<i32> {
// Empty vector to store the leaders
let mut leaders = Vec::new();
// Set the max value to the last element of the array
let mut max = arr[arr.len() - 1];
// Add the max value to the leaders vector
leaders.push(max);
// Iterate through the array in reverse order
for i in (0..arr.len() - 1).rev() {
if arr[i] > max {
// Set the max value to the current element
max = arr[i];
leaders.push(max);
}
}
leaders.reverse();
leaders
}
fn main() {
let arr = [16, 17, 4, 3, 5, 2];
let leaders = leaders_in_an_array(&arr);
println!("{:?}", leaders);
}

@ -0,0 +1,8 @@
[package]
name = "maximum_subarray"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,26 @@
# Comments on the solution
We want to use Kadane's Algorithm: a dynamic programming algorithm used to find the maximum subarray sum in an array of integers. It works by iterating through the array and keeping track of the maximum sum seen so far and the maximum sum ending at the current index.
At each index, the algorithm compares the current element with the maximum sum ending at the previous index plus the current element. If the current element is greater, then the maximum sum ending at the current index is just the current element. Otherwise, the maximum sum ending at the current index is the sum of the maximum sum ending at the previous index and the current element.
The algorithm also keeps track of the maximum sum seen so far, which is the maximum of all the maximum sums ending at each index. The final result is the maximum sum seen so far.
### Coding
```rust
fn max_sub_array(nums: &[i32]) -> i32 {
let mut max_ending_here = nums[0];
let mut max_so_far = nums[0];
for &num in nums.iter().skip(1) {
max_ending_here = i32::max(num, max_ending_here + num);
max_so_far = i32::max(max_so_far, max_ending_here);
}
max_so_far
}
```
### Leetcode result
![](https://i.imgur.com/enTIFEs.png)

@ -0,0 +1,22 @@
// Given an integer array nums, find the subarray with the largest sum, and return its sum.
// https://leetcode.com/problems/maximum-subarray/description/
fn max_sub_array(nums: &[i32]) -> i32 {
let mut max_ending_here = nums[0];
let mut max_so_far = nums[0];
for &num in nums.iter().skip(1) {
// What is the maximum sum of the subarray ending at this point?
max_ending_here = i32::max(num, max_ending_here + num);
// What is the maximum sum of any subarray we've seen so far?
max_so_far = i32::max(max_so_far, max_ending_here);
}
max_so_far
}
fn main() {
let nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
let res = maxSubArray(&nums);
println!("{}", res);
}

@ -1 +0,0 @@
# competitive-programming
Loading…
Cancel
Save