-
Notifications
You must be signed in to change notification settings - Fork 0
/
1590_sum_divisible_by_p.swift
65 lines (58 loc) · 2.13 KB
/
1590_sum_divisible_by_p.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
// Solution 3: Prefix Sum - O(n)/O(n)
func minSubarray(_ nums: [Int], _ p: Int) -> Int {
var k = nums.reduce(0,+) % p
if k == 0 { return 0 }
var reminders : [Int:Int] = [0:-1]
var res = nums.count
var sum = 0
for (index, num) in nums.enumerated() {
sum += num
let r = sum % p
var rp = (r - k + p) % p
if let rem = reminders[rp] { // it has a pair
res = min(res, index - rem) // return the subarray lenth
}
reminders[r] = index
}
return res == nums.count ? -1 : res
}
// Solution 2: Brute Force - O(n^2), taking too long
// func minSubarray(_ nums: [Int], _ p: Int) -> Int {
// if nums.reduce(0,+) % p == 0 { return 0 }
// var arr = nums
// var n = 1
// while n < arr.count {
// // select a range and check if the sum of subArr is divisible
// for i in 0...arr.count - n {
// // var subtractedArr = arr[i...i+n-1]
// var subArr1 = i-1>=0 ? arr[0...i-1] : []
// var subArr2 = i+n < arr.count ? arr[i+n...arr.count-1] : []
// var subArr = subArr1 + subArr2
// if subArr.reduce(0,+) % p == 0 { return n }
// }
// n += 1
// }
// return -1
// }
// Solution 1: DFS (Failed, because it needs to be subarray)
// func minSubarray(_ nums: [Int], _ p: Int) -> Int {
// let res : Int = dfs(nums, 0, p)
// return res == Int.max ? -1 : res
// }
// func dfs(_ subArr: [Int],_ n: Int, _ p: Int) -> Int {
// // base case
// if subArr.count == 0 { return Int.max }
// else if subArr.reduce(0,+) % p == 0 { return n }
// // enter branches
// var minVal = Int.max
// for i in 0..<subArr.count {
// if subArr[i] % p != 0 {
// var newArr = subArr
// newArr.remove(at: i)
// minVal = min(minVal, dfs(newArr, n+1, p))
// }
// }
// return minVal
// }
}