Skip to content

Instantly share code, notes, and snippets.

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int max=0,cur=0;
for(int i=0;i<nums.size();i++) {
if(nums[i]&1) {
max=max>++cur?max:cur;
} else cur=0;
}
return max;
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> res;
for(int i = 0; i < nums.size(); i++) {
nums[abs(nums[i]) - 1] = -nums[abs(nums[i]) - 1];
if (nums[abs(nums[i]) - 1] > 0) res.push_back(abs(nums[i]));
}
return res;
}
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> result;
for(int i = 0; i < nums.size(); i++) {
int m = abs(nums[i]) - 1;
nums[m] = nums[m] > 0 ? -nums[m] : nums[m];
}
for(int j = 0; j < nums.size(); j++) {
if (nums[j] > 0) result.push_back(j+1);
class MyCalendar {
vector<pair<int, int>> books;
public:
bool book(int start, int end) {
for (pair<int, int> p : books)
if (max(p.first, start) < min(end, p.second)) return false;
books.push_back({start, end});
return true;
}
};
class MyCalendar {
public:
MyCalendar() {
}
bool book(int start, int end) {
rec[start]++;
rec[end]--;
int c = 0;
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
map<long, int> m;
int j = 0;
for(int i = 0; i < nums.size(); i++) {
if (i - j > k && m[nums[j]] == j) m.erase(nums[j++]);
auto a = m.lower_bound((long)nums[i] - t);
if (a != m.end() && abs(a->first - nums[i]) <= t) return true;
m[nums[i]] = i;
class Solution {
private:
bool countWithSet(vector<int>& nums, int k) {
unordered_set<int> s;
for(int i = 0; i < nums.size(); i++) {
if (i > k) s.erase(nums[i - k - 1]);
if (s.find(nums[i]) != s.end()) return true;
s.insert(nums[i]);
}
return false;
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if (nums.size() == 0) return false;
sort(nums.begin(), nums.end());
int cur = -1;
for(int i = 0; i < nums.size(); i++) {
if (cur != nums[i]) {
cur = nums[i];
} else {
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int step1 = cost[0], step2 = cost[1];
for(int i = 2; i < cost.size(); i++) {
int res = min(step1, step2) + cost[i];
step1 = step2;
step2 = res;
}
return min(step1, step2);
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() <= 1) return 0;
int cum = 0;
for(int i = 1; i < prices.size(); i++) {
cum += (prices[i] > prices[i-1] ? prices[i] - prices[i-1] : 0);
}
return cum;
}