Skip to content

Commit b87babd

Browse files
Added day14 and day15 solution
1 parent 4a1163a commit b87babd

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int maxSubarraySumCircular(vector<int>& A) {
4+
if(A.size() == 0) return 0;
5+
6+
int nocircle = helper(A), sum = 0;
7+
if(nocircle < 0) return nocircle;
8+
9+
auto t = A;
10+
for(auto &n : t){
11+
sum += n;
12+
n *= -1;
13+
}
14+
15+
int circle = sum + helper(t);
16+
return max(circle, nocircle);
17+
}
18+
19+
int helper(vector<int>& A){
20+
if(A.size() == 0) return 0;
21+
22+
int max_seen_so_far = INT_MIN, max_positive_seen = 0;
23+
for(auto num : A){
24+
if(max_positive_seen < 0)
25+
max_positive_seen = num;
26+
else
27+
max_positive_seen += num;
28+
max_seen_so_far = max(max_seen_so_far, max_positive_seen);
29+
}
30+
return max_seen_so_far;
31+
}
32+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* oddEvenList(ListNode* head) {
12+
if(!head) return NULL;
13+
14+
auto odd = head, even_head = odd->next, even = even_head;
15+
16+
while(even && even->next){
17+
odd->next = odd->next->next;
18+
even->next = even->next->next;
19+
odd = odd->next;
20+
even = even->next;
21+
}
22+
odd->next = even_head;
23+
return head;
24+
}
25+
};

0 commit comments

Comments
 (0)