Data Structure I: Day 1
Diffstat:
2 files changed, 21 insertions(+), 0 deletions(-)
@@ -1,3 +1,4 @@
// memorization approach
class Solution {
public:
int maxSubArray(vector<int> &nums) {
@@ -12,3 +13,14 @@
public:
return maxi;
}
};
// optimized, memorize only the previous value
class Solution {
public:
int maxSubArray(vector<int> &nums) {
int n = nums.size(), maxi, prev;
maxi = prev = nums[0];
for (int i = 1; i < n; i++) maxi = max(maxi, prev = nums[i] + max(prev, 0));
return maxi;
}
};
@@ -10,3 +10,12 @@
public:
return false;
}
};
// Fun oneliner
class Solution {
public:
bool containsDuplicate(vector<int> &nums) {
return nums.size() > unordered_set<int>(nums.begin(), nums.end()).size();
}
};