-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1590 (#3996)
No.1590.Make Sum Divisible by P
- Loading branch information
Showing
4 changed files
with
227 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
solution/1500-1599/1590.Make Sum Divisible by P/Solution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} p | ||
* @return {number} | ||
*/ | ||
var minSubarray = function (nums, p) { | ||
let k = 0; | ||
for (const x of nums) { | ||
k = (k + x) % p; | ||
} | ||
if (k === 0) { | ||
return 0; | ||
} | ||
const last = new Map(); | ||
last.set(0, -1); | ||
const n = nums.length; | ||
let ans = n; | ||
let cur = 0; | ||
for (let i = 0; i < n; ++i) { | ||
cur = (cur + nums[i]) % p; | ||
const target = (cur - k + p) % p; | ||
if (last.has(target)) { | ||
const j = last.get(target); | ||
ans = Math.min(ans, i - j); | ||
} | ||
last.set(cur, i); | ||
} | ||
return ans === n ? -1 : ans; | ||
}; |
34 changes: 34 additions & 0 deletions
34
solution/1500-1599/1590.Make Sum Divisible by P/Solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::collections::HashMap; | ||
|
||
impl Solution { | ||
pub fn min_subarray(nums: Vec<i32>, p: i32) -> i32 { | ||
let mut k = 0; | ||
for &x in &nums { | ||
k = (k + x) % p; | ||
} | ||
if k == 0 { | ||
return 0; | ||
} | ||
|
||
let mut last = HashMap::new(); | ||
last.insert(0, -1); | ||
let n = nums.len(); | ||
let mut ans = n as i32; | ||
let mut cur = 0; | ||
|
||
for i in 0..n { | ||
cur = (cur + nums[i]) % p; | ||
let target = (cur - k + p) % p; | ||
if let Some(&prev_idx) = last.get(&target) { | ||
ans = ans.min(i as i32 - prev_idx); | ||
} | ||
last.insert(cur, i as i32); | ||
} | ||
|
||
if ans == n as i32 { | ||
-1 | ||
} else { | ||
ans | ||
} | ||
} | ||
} |