Skip to content

Instantly share code, notes, and snippets.

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
class Solution {
public:
vector<int> maxSumOfThreeSubarrays(vector<int>& nums, int k) {
int len = nums.size();
if(len < 3 * k)return {};
//store index, use presum to get the value
vector<int> left(len, -1), right(len, -1), presums(len + 1, 0);
int sum = 0;
//init presum
for(int i = 0; i < len; ++i)
class Solution {
public:
/*
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
int maxTwoSubArrays(vector<int> &nums) {
int len = nums.size(), localMax = INT_MIN;
vector<int> dp(len, 0);
//left to right
class Solution {
public:
/**
* @param nums: A list of integers
* @param k: An integer denote to find k non-overlapping subarrays
* @return: An integer denote the sum of max k non-overlapping subarrays
*/
int maxSubArray(vector<int> &nums, int k) {
int len = nums.size();
if(len < k)return 0;
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if(len < 2)return 0;
vector<int> dp(2, 0);
dp[1] = max(0, prices[1] - prices[0]);
int maxDiff = dp[0] - prices[2], e = 0;
for(int i = 2; i < len; ++i, e ^= 1)
{
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.empty())return 0;
int len = prices.size(), globalMax = 0, minPrice = prices[0];
for(int i = 1; i < len; ++i)
{
int localMax = prices[i] - minPrice;
globalMax = max(globalMax, localMax);
minPrice = min(minPrice, prices[i]);
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int len = nums.size(), res = 0;
if (len < 3)return 0;
sort(nums.begin(), nums.end());
for (int i = 0; i + 2 < len; ++i)
{
int lo = i + 1, hi = len - 1;
while (lo < hi)
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> map;
int res = 0;
for (int i = 0; i < A.size(); ++i)
{
for (int j = 0; j < B.size(); ++j)
{
int sum = A[i] + B[j];