Skip to content

Commit

Permalink
1793. Maximum Score of a Good Subarray: RETRY
Browse files Browse the repository at this point in the history
  • Loading branch information
tan-wei committed Dec 7, 2024
1 parent 523e709 commit 609a2f4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/solution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,3 +1351,4 @@ mod s1787_make_the_xor_of_all_segments_equal_to_zero;
mod s1790_check_if_one_string_swap_can_make_strings_equal;
mod s1791_find_center_of_star_graph;
mod s1792_maximum_average_pass_ratio;
mod s1793_maximum_score_of_a_good_subarray;
68 changes: 68 additions & 0 deletions src/solution/s1793_maximum_score_of_a_good_subarray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* [1793] Maximum Score of a Good Subarray
*
* You are given an array of integers nums (0-indexed) and an integer k.
* The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). A good subarray is a subarray where i <= k <= j.
* Return the maximum possible score of a good subarray.
*
* Example 1:
*
* Input: nums = [1,4,3,7,4,5], k = 3
* Output: 15
* Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
*
* Example 2:
*
* Input: nums = [5,5,4,5,4,1,1,1], k = 0
* Output: 20
* Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 1 <= nums[i] <= 2 * 10^4
* 0 <= k < nums.length
*
*/
pub struct Solution {}

// problem: https://leetcode.com/problems/maximum-score-of-a-good-subarray/
// discuss: https://leetcode.com/problems/maximum-score-of-a-good-subarray/discuss/?currentPage=1&orderBy=most_votes&query=

// submission codes start here

impl Solution {
pub fn maximum_score(nums: Vec<i32>, k: i32) -> i32 {
0
}
}

// submission codes end

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[ignore]
fn test_1793_example_1() {
let nums = vec![1, 4, 3, 7, 4, 5];
let k = 3;

let result = 15;

assert_eq!(Solution::maximum_score(nums, k), result);
}

#[test]
#[ignore]
fn test_1793_example_2() {
let nums = vec![5, 5, 4, 5, 4, 1, 1, 1];
let k = 0;

let result = 20;

assert_eq!(Solution::maximum_score(nums, k), result);
}
}

0 comments on commit 609a2f4

Please sign in to comment.