Skip to content

Commit 3c1fa53

Browse files
committed
Add maximum subarray and range sum query 2d immutable
1 parent 8098a28 commit 3c1fa53

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

Easy/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Easy
2323
- [maximum-ascending-subarray-sum.md](Easy/maximum-ascending-subarray-sum.md)
2424
- [maximum-depth-of-binary-tree.md](Easy/maximum-depth-of-binary-tree.md)
2525
- [maximum-depth-of-n-ary-tree.md](Easy/maximum-depth-of-n-ary-tree.md)
26+
- [maximum-subarray.md](Easy/maximum-subarray.md)
2627
- [merge-sorted-array.md](Easy/merge-sorted-array.md)
2728
- [merge-two-sorted-lists.md](Easy/merge-two-sorted-lists.md)
2829
- [minimum-depth-of-binary-tree.md](Easy/minimum-depth-of-binary-tree.md)

Easy/maximum-subarray.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Code
2+
====
3+
4+
```go
5+
func maxSubArray(nums []int) int {
6+
max := nums[0]
7+
for i := 1; i < len(nums); i++ {
8+
if nums[i]+nums[i-1] > nums[i] {
9+
nums[i] = nums[i] + nums[i-1]
10+
}
11+
if nums[i] > max {
12+
max = nums[i]
13+
}
14+
}
15+
16+
return max
17+
}
18+
```
19+
20+
Solution in mind
21+
================
22+
23+
- We iterate through the array and replace the number at a given position by the maximum sum that can be created till that position.
24+
25+
- The number at a given position is thus set to the `max(nums[i], nums[i]+nums[i-1])`.
26+
27+
- Thus if a sum is lesser than the number itself, we're better off by using just the number.

Medium/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Medium
5151
- [populating-next-right-pointers-in-each-node-ii.md](Medium/populating-next-right-pointers-in-each-node-ii.md)
5252
- [populating-next-right-pointers-in-each-node.md](Medium/populating-next-right-pointers-in-each-node.md)
5353
- [pseudo-palindromic-paths-in-a-binary-tree.md](Medium/pseudo-palindromic-paths-in-a-binary-tree.md)
54+
- [range-sum-query-2d-immutable.md](Medium/range-sum-query-2d-immutable.md)
5455
- [redundant-connection.md](Medium/redundant-connection.md)
5556
- [remove-comments.md](Medium/remove-comments.md)
5657
- [remove-duplicates-from-sorted-array-ii.md](Medium/remove-duplicates-from-sorted-array-ii.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Code
2+
====
3+
4+
```go
5+
type NumMatrix struct {
6+
Matrix [][]int
7+
}
8+
9+
func Constructor(matrix [][]int) NumMatrix {
10+
n := NumMatrix{Matrix: matrix}
11+
return n
12+
}
13+
14+
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
15+
sum := 0
16+
for i := row1; i <= row2; i++ {
17+
for j := col1; j <= col2; j++ {
18+
sum += this.Matrix[i][j]
19+
}
20+
}
21+
22+
return sum
23+
}
24+
25+
/**
26+
* Your NumMatrix object will be instantiated and called as such:
27+
* obj := Constructor(matrix);
28+
* param_1 := obj.SumRegion(row1,col1,row2,col2);
29+
*/
30+
```

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Link to profile, [bhargavsnv100](https://leetcode.com/bhargavsnv100/)
77

88
| [Easy](Easy/README.md) | [Medium](Medium/README.md) | [Hard](Hard/README.md) |
99
|------------------------|----------------------------|------------------------|
10-
| 47 | 76 | 14 |
10+
| 48 | 77 | 14 |
1111

1212
Problems finished
1313
=================
@@ -37,6 +37,7 @@ Easy
3737
- [maximum-ascending-subarray-sum.md](Easy/maximum-ascending-subarray-sum.md)
3838
- [maximum-depth-of-binary-tree.md](Easy/maximum-depth-of-binary-tree.md)
3939
- [maximum-depth-of-n-ary-tree.md](Easy/maximum-depth-of-n-ary-tree.md)
40+
- [maximum-subarray.md](Easy/maximum-subarray.md)
4041
- [merge-sorted-array.md](Easy/merge-sorted-array.md)
4142
- [merge-two-sorted-lists.md](Easy/merge-two-sorted-lists.md)
4243
- [minimum-depth-of-binary-tree.md](Easy/minimum-depth-of-binary-tree.md)
@@ -116,6 +117,7 @@ Medium
116117
- [populating-next-right-pointers-in-each-node-ii.md](Medium/populating-next-right-pointers-in-each-node-ii.md)
117118
- [populating-next-right-pointers-in-each-node.md](Medium/populating-next-right-pointers-in-each-node.md)
118119
- [pseudo-palindromic-paths-in-a-binary-tree.md](Medium/pseudo-palindromic-paths-in-a-binary-tree.md)
120+
- [range-sum-query-2d-immutable.md](Medium/range-sum-query-2d-immutable.md)
119121
- [redundant-connection.md](Medium/redundant-connection.md)
120122
- [remove-comments.md](Medium/remove-comments.md)
121123
- [remove-duplicates-from-sorted-array-ii.md](Medium/remove-duplicates-from-sorted-array-ii.md)

0 commit comments

Comments
 (0)