-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14.cpp
More file actions
137 lines (124 loc) · 2.87 KB
/
day14.cpp
File metadata and controls
137 lines (124 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<bits/stdc++.h>
using namespace std;
//int maxSubArray(vector<int> &nums) {
// int maxi = INT_MIN;
// int n = nums.size();
// for (int i = 0; i < n; i++) {
// int sum = 0;
// for (int j = i; j < n; j++) {
// cout << nums[i] << "," << nums[j] << endl;
// sum += nums[j];
// cout << sum << endl;
// maxi = max(sum, maxi);
// }
// }
// return maxi;
//
//}
int maxSubArray(vector<int> &nums) {
int maxi = INT_MIN;
int n = nums.size();
int sum = 0;
for (int i = 0; i < n; i++) {
sum += nums[i];
if (sum < 0) {
sum = 0;
}
if (sum > maxi) {
maxi = sum;
}
}
return maxi;
}
int maxProfit(vector<int> &prices) {
int n = prices.size();
int maxlen = 0;
int sum = INT_MAX;
for (int i = 0; i < n; i++) {
sum = min(sum, prices[i]);
maxlen = max(maxlen, prices[i] - sum);
}
return maxlen;
}
vector<int> rearrangeArray(vector<int> &nums) {
int n = nums.size();
vector<int> pos;
vector<int> neg;
vector<int> temp;
for (int i = 0; i < n; i++) {
if (nums[i] > 0) {
pos.push_back(nums[i]);
} else {
neg.push_back(nums[i]);
}
}
for (int i = 0; i < n / 2; i++) {
temp.push_back(pos[i]);
temp.push_back(neg[i]);
}
nums = temp;
return nums;
}
void nextPermutation(vector<int> &nums) {
next_permutation(nums.begin(), nums.end());
}
vector<int> leaders(vector<int> &arr) {
// Code here
int maxLeader = INT_MIN;
vector<int> temp;
int n = arr.size();
for (int i = n - 1; i >= 0; i--) {
if (arr[i] >= maxLeader) {
temp.push_back(arr[i]);
}
maxLeader = max(maxLeader, arr[i]);
}
reverse(temp.begin(), temp.end());
arr = temp;
return arr;
}
int longestConsecutive(vector<int> &nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
int counter = 1;
int maxLen = 0;
if (n == 0) return 0;
for (int i = 0; i < n; i++) {
if (nums[i] == nums[i + 1] - 1) {
counter++;
maxLen = max(counter, maxLen);
}
}
return maxLen;
}
int main() {
vector<int> nums = {100, 4, 200, 1, 3, 2};
// cout << maxSubArray(nums);
// cout << maxProfit(nums);
// rearrangeArray(nums);
// nextPermutation(nums);
cout << longestConsecutive(nums);
// leaders(nums);
// for (auto i: nums) {
// cout << i << " ";
// }
return 0;
}
//class Solution {
//public:
// int maxSubArray(vector<int>& nums) {
// int res = nums[0];
// int total = 0;
//
// for (int n : nums) {
// if (total < 0) {
// total = 0;
// }
//
// total += n;
// res = max(res, total);
// }
//
// return res;
// }
//};