aboutsummaryrefslogtreecommitdiff
path: root/other/min_avg_diff.rs
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/min_avg_diff.rs
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/min_avg_diff.rs')
-rw-r--r--other/min_avg_diff.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/other/min_avg_diff.rs b/other/min_avg_diff.rs
new file mode 100644
index 0000000..71c18aa
--- /dev/null
+++ b/other/min_avg_diff.rs
@@ -0,0 +1,23 @@
+struct Solution;
+
+impl Solution {
+ pub fn minimum_average_difference(nums: Vec<i32>) -> i32 {
+ let (mut left, mut ans, mut min) = (0, 0, i64::MAX);
+ let mut right = nums.iter().map(|&x| x as i64).sum::<i64>();
+ for i in 0..nums.len() {
+ left += nums[i] as i64;
+ right -= nums[i] as i64;
+ let diff = (left / (i + 1) as i64 - right / 1i64.max((nums.len() - i - 1) as i64)).abs();
+ if diff < min {
+ ans = i as i32;
+ min = diff;
+ }
+ }
+ ans
+ }
+}
+
+fn main() {
+ let nums = vec![2,5,3,9,5,3];
+ print!("{}", Solution::minimum_average_difference(nums));
+}