-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdpMaximumSubarray.ps1
More file actions
56 lines (47 loc) · 1.87 KB
/
dpMaximumSubarray.ps1
File metadata and controls
56 lines (47 loc) · 1.87 KB
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
function dpMaximumSubarray($inputArray) {
# Check if all elements of inputArray are negative ones and return the highest
# one in this case.
$allNegative = $true
$highestElementValue = $null
for ($i = 0; $i -lt $inputArray.Count; $i += 1) {
if ($inputArray[$i] -ge 0) {
$allNegative = $false
}
if ($highestElementValue -eq $null -or $highestElementValue -lt $inputArray[$i]) {
$highestElementValue = $inputArray[$i]
}
}
if ($allNegative -and $highestElementValue -ne $null) {
return @($highestElementValue)
}
# Let's assume that there is at list one positive integer exists in array.
# And thus the maximum sum will for sure be grater then 0. Thus we're able
# to always reset max sum to zero.
$maxSum = 0
# This array will keep a combination that gave the highest sum.
$maxSubArray = @()
# Current sum and subarray that will memoize all previous computations.
$currentSum = 0
$currentSubArray = @()
for ($i = 0; $i -lt $inputArray.Count; $i += 1) {
# Let's add current element value to the current sum.
$currentSum += $inputArray[$i]
if ($currentSum -lt 0) {
# If the sum went below zero then reset it and don't add current element to max subarray.
$currentSum = 0
# Reset current subarray.
$currentSubArray = @()
}
else {
# If current sum stays positive then add current element to current sub array.
$currentSubArray += $inputArray[$i]
if ($currentSum -gt $maxSum) {
# If current sum became greater then max registered sum then update
# max sum and max subarray.
$maxSum = $currentSum
$maxSubArray = $currentSubArray.Clone()
}
}
}
$maxSubArray
}