Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions data_structures/arrays/maximum_subarray_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def maximum_subarray_sum(arr):
"""
Finds the maximum sum of a contiguous subarray using Kadane's Algorithm

Args:
arr (list of int): The input array

Returns:
int: The maximum subarray sum
"""

if not arr:
return 0

current_max_sum = max_sum = arr[0]

for i in arr[1:]:
current_max_sum = max(i, current_max_sum + i)
max_sum = max(max_sum, current_max_sum)

return max_sum


if __name__ == "__main__":
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
print("maximum subarray sum is: ", maximum_subarray_sum(arr))
8 changes: 5 additions & 3 deletions greedy_methods/fractional_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
return (
0
if k == 0
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
else (
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
)
)


Expand Down
8 changes: 5 additions & 3 deletions matrix/matrix_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ def cofactors(self) -> Matrix:
return Matrix(
[
[
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
(
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
)
for column in range(self.minors().num_columns)
]
for row in range(self.minors().num_rows)
Expand Down