mirror of
https://github.com/lukefleed/competitive-programming.git
synced 2026-10-06 22:24:53 +00:00
first two problems solved
This commit is contained in:
@@ -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
|
||||
|
||||

|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user