forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2085 from AkifhanIlgaz/0918
Create: 0918-maximum-sum-circular-subarray
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
func main() { | ||
|
||
} | ||
|
||
func maxSubarraySumCircular(nums []int) int { | ||
globalMax, globalMin := nums[0], nums[0] | ||
currentMax, currentMin := 0, 0 | ||
total := 0 | ||
|
||
for _, num := range nums { | ||
currentMax = max(num, currentMax+num) | ||
currentMin = min(num, currentMin+num) | ||
total += num | ||
globalMax = max(globalMax, currentMax) | ||
globalMin = min(globalMin, currentMin) | ||
} | ||
|
||
if globalMax > 0 { | ||
return max(globalMax, total-globalMin) | ||
} else { | ||
return globalMax | ||
} | ||
|
||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func min(a, b int) int { | ||
if a < b { | ||
return a | ||
} | ||
return b | ||
} |
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,19 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubarraySumCircular = function (nums) { | ||
let [globalMax, globalMin] = [nums[0], nums[0]]; | ||
let [currentMax, currentMin] = [0, 0]; | ||
let total = 0; | ||
|
||
for (num of nums) { | ||
currentMax = Math.max(num, currentMax + num); | ||
currentMin = Math.min(num, currentMin + num); | ||
total += num; | ||
globalMax = Math.max(globalMax, currentMax); | ||
globalMin = Math.min(globalMin, currentMin); | ||
} | ||
|
||
return globalMax > 0 ? Math.max(globalMax, total - globalMin) : globalMax; | ||
}; |
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,21 @@ | ||
impl Solution { | ||
pub fn max_subarray_sum_circular(nums: Vec<i32>) -> i32 { | ||
let (mut global_max, mut global_min) = (nums[0], nums[0]); | ||
let (mut current_max, mut current_min) = (0, 0); | ||
let mut total = 0; | ||
|
||
for num in nums { | ||
current_max = i32::max(num, current_max + num); | ||
current_min = i32::min(num, current_min + num); | ||
total += num; | ||
global_max = i32::max(global_max, current_max); | ||
global_min = i32::min(global_min, current_min); | ||
} | ||
|
||
if global_max > 0 { | ||
return i32::max(global_max, total - global_min); | ||
} else { | ||
return global_max; | ||
} | ||
} | ||
} |
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,15 @@ | ||
function maxSubarraySumCircular(nums: number[]): number { | ||
let [globalMax, globalMin] = [nums[0], nums[0]]; | ||
let [currentMax, currentMin] = [0, 0]; | ||
let total = 0; | ||
|
||
for (let num of nums) { | ||
currentMax = Math.max(num, currentMax + num); | ||
currentMin = Math.min(num, currentMin + num); | ||
total += num; | ||
globalMax = Math.max(globalMax, currentMax); | ||
globalMin = Math.min(globalMin, currentMin); | ||
} | ||
|
||
return globalMax > 0 ? Math.max(globalMax, total - globalMin) : globalMax; | ||
} |