Skip to content

Commit 0040192

Browse files
Cpp solution (codedecks-in#42)
* docs:Update contribution detail * Add maximum-subarray solution
1 parent 5e6c51a commit 0040192

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

C++/maximum-subarray.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* The Optimized Solution
2+
Time Complexity: O(N)
3+
Space Complexity: O(1)
4+
5+
*/
6+
7+
class Solution {
8+
public:
9+
int maxSubArray(vector<int>& nums) {
10+
11+
int cs=0;
12+
int ms=INT_MIN;
13+
// if size of array is 1 return that elemnt .
14+
if(nums.size()==1){
15+
return nums[0];
16+
}
17+
//Linearly traverse the array and add to the current sum variable and take maximum .
18+
for (int i = 0; i < nums.size(); i++) {
19+
cs=cs+nums[i];
20+
21+
22+
ms=max(cs,ms);
23+
if(cs<0){
24+
cs=0;
25+
}
26+
}
27+
28+
return ms;
29+
}
30+
31+
32+
33+
34+
};

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
9898
| 1534 | [Count Good Triplets](https://leetcode.com/problems/count-good-triplets/) | [Python](./Python/count-good-triplets.py) | _O(N^3)_ | _O(1)_ | Easy | | |
9999
| 1572 | [Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/) | [Java](./Java/matrix-diagonal-sum.java) | _O(N)_ | _O(1)_ | Easy | | |
100100
| 811 | [Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/) | [Javascript](./JavaScript/Subdomain-Visit-Count.js) | _O(N*M)_ | _O(N*M + N)_ | Easy | | |
101-
101+
| 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [C++](./C++/maximum-subarray.cpp) | _O(N)_ | _O(1)_ |
102+
Easy | Array | | |
102103

103104
<br/>
104105
<div align="right">
@@ -348,7 +349,8 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
348349
| [Sachin Singh Negi](https://github.com/sachinnegi) <br> <img src="https://github.com/sachinnegi.png" width="100" height="100"> | India | Python | [Twitter](https://twitter.com/SachinSinghNe17)<br>[Leetcode](https://leetcode.com/negisachin688/)<br>[Hackerrrak](https://www.hackerrank.com/negisachin688) |
349350
| [Girish Thatte](https://github.com/girishgr8/) <br> <img src="https://github.com/girishgr8.png" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/girish13/) <br> [Hackerrank](https://www.hackerrank.com/procoder_13) <br> [Codechef](https://www.codechef.com/procoder_13) |
350351
| [Kevin Chittilapilly](https://github.com/KevinChittilapilly) <br> <img src="https://github.com/KevinChittilapilly.png" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/being_kevin/) <br> [Hackerrank](https://www.hackerrank.com/ckevinvarghese11) <br> [Kaggle](https://www.kaggle.com/kevinchittilapilly) |
351-
| [Nour Grati](https://github.com/Nour-Grati) <br> <img src="https://github.com/Nour-Grati.png" width="100" height="100"> | Tunisia | Python | [Leetcode](https://leetcode.com/nourgrati/) <br> [Hackerrank](https://www.hackerrank.com/noor_grati) <br> [Twitter](https://twitter.com/GratiNour1) |
352+
| [Nour Grati](https://github.com/Nour-Grati) <br> <img src="https://github.com/Nour-Grati.png" width="100" height="100"> | Tunisia | Python | [Leetcode](https://leetcode.com/nourgrati/) <br> [Hackerrank](https://www.hackerrank.com/noor_grati) <br> [Twitter](https://twitter.com/GratiNour1)
353+
|[Avinash Trivedi](https://github.com/trivediavinash) <br> <img src="https://github.com/trivediavinash.png" width="100" height="100"> |India | C++ | [Leetcode](https://leetcode.com/avi_002/) |
352354

353355
<br/>
354356
<div align="right">

0 commit comments

Comments
 (0)