Skip to content

Commit a0560c4

Browse files
committed
add kadane algorithm
1 parent 1da1cb4 commit a0560c4

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

‎patterns/c#/KadaneAlgorithm.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
public class KadaneAlgorithm {
4+
public int MaxSubArray(int[] nums) {
5+
int currentSum = nums[0];
6+
int maxSum = nums[0];
7+
8+
for (int i = 1; i < nums.Length; i++) {
9+
currentSum = Math.Max(nums[i], currentSum + nums[i]);
10+
maxSum = Math.Max(maxSum, currentSum);
11+
}
12+
return maxSum;
13+
}
14+
}

‎patterns/c++/KadaneAlgorithm.cpp

Whitespace-only changes.

‎patterns/go/kadane_algorithm.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "math"
4+
5+
func maxSubArray(nums []int) int {
6+
currentSum := nums[0]
7+
maxSum := nums[0]
8+
9+
for i := 1; i < len(nums); i++ {
10+
currentSum = int(math.Max(float64(nums[i]), float64(currentSum+nums[i])))
11+
maxSum = int(math.Max(float64(maxSum), float64(currentSum)))
12+
}
13+
14+
return maxSum
15+
}

‎patterns/java/KadaneAlgorithm.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package patterns.java;
2+
3+
public class KadaneAlgorithm {
4+
public int maxSubArray(int[] nums) {
5+
int currentSum = nums[0]; // Start with the first element
6+
int maxSum = nums[0]; // Initialize maxSum with the first element
7+
8+
// Traverse the array from the second element
9+
for (int i = 1; i < nums.length; i++) {
10+
// If currentSum is negative, reset to current element
11+
currentSum = Math.max(nums[i], currentSum + nums[i]);
12+
// Update maxSum if currentSum is greater
13+
maxSum = Math.max(maxSum, currentSum);
14+
}
15+
return maxSum;
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class KadaneAlgorithm {
2+
maxSubArray(nums) {
3+
let currentSum = nums[0];
4+
let maxSum = nums[0];
5+
6+
for (let i = 1; i < nums.length; i++) {
7+
currentSum = Math.max(nums[i], currentSum + nums[i]);
8+
maxSum = Math.max(maxSum, currentSum);
9+
}
10+
return maxSum;
11+
}
12+
}

‎patterns/python/kadane_algorithm.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class KadaneAlgorithm:
2+
def max_sub_array(self, nums):
3+
current_sum = nums[0]
4+
max_sum = nums[0]
5+
6+
for i in range(1, len(nums)):
7+
current_sum = max(nums[i], current_sum + nums[i])
8+
max_sum = max(max_sum, current_sum)
9+
10+
return max_sum
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class KadaneAlgorithm {
2+
maxSubArray(nums: number[]): number {
3+
let currentSum: number = nums[0];
4+
let maxSum: number = nums[0];
5+
6+
for (let i = 1; i < nums.length; i++) {
7+
currentSum = Math.max(nums[i], currentSum + nums[i]);
8+
maxSum = Math.max(maxSum, currentSum);
9+
}
10+
return maxSum;
11+
}
12+
}

0 commit comments

Comments
 (0)