123 Best Time to Buy and Sell Stock III
class Solution {
public:
int maxProfit(vector<int>& prices)
{
int len=prices.size();
if (len<2) return 0;
int release2=0,hold2=INT_MIN,release1=0,hold1=INT_MIN;
for(int i=0;i<len;i++)
{
release2=max(release2,hold2+prices[i]);
hold2=max(hold2,release1-prices[i]);
release1=max(release1,hold1+prices[i]);
hold1=max(hold1,-prices[i]);
}
return release2;
}
};
381 Insert Delete GetRandom O(1) - Duplicates allowed
class RandomizedCollection {
public:
/** Initialize your data structure here. */
RandomizedCollection()
{
s.clear();
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
bool insert(int val)
{
bool flag=true;
if (s.find(val)!=s.end()) flag=false;
s.insert(val);
return flag;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val)
{
set<int>::iterator it=s.find(val);
if (it==s.end())
return false;
s.erase(it);
return true;
}
/** Get a random element from the set. */
int getRandom()
{
int len=s.size();
int r=rand() % len;
set<int>::iterator it=s.begin();
for(int i=0;i<r;i++)
it++;
return (*it);
}
private:
multiset<int> s;
};
331 Verify Preorder Serialization of a Binary Tree
class Solution(object):
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
str = preorder.split(',')
diff = 1
for i in str:
if diff==0:
return False
if i=='#':
diff-=1
else:
diff+=1
return diff==0
375 Guess Number Higher or Lower II
class Solution {
public:
vector< vector<int> > f;
int getMoneyAmount(int n)
{
vector<vector<int> > g(n + 2, vector<int>(n + 2, 0));
f=g;
return recur(1,n);
}
int recur(int left,int right)
{
if (left>=right) return 0;
if (f[left][right]>0) return f[left][right];
f[left][right]=INT_MAX;
for(int i=left;i<=right;i++)
{
f[left][right]=min(f[left][right],i+max(recur(left,i-1),recur(i+1,right)));
}
return f[left][right];
}
};
86 Partition List
class Solution
{
public:
ListNode* partition(ListNode* head, int x)
{
ListNode* lessx = new ListNode(0);
ListNode* l=lessx;
ListNode* morex = new ListNode(0);
ListNode* m=morex;
ListNode* p = head;
while(p!=NULL)
{
if (p->val < x)
{
ListNode* k=new ListNode(p->val);
lessx->next=k;
lessx=lessx->next;
}
else
{
ListNode* k=new ListNode(p->val);
morex->next=k;
morex=morex->next;
}
p=p->next;
}
ListNode* h=new ListNode(0);
p=h;
if (l!=lessx)
{
while (l!=NULL && l->next!=NULL)
{
p->next=l->next;
l=l->next;
p=p->next;
}
}
if (m!=morex)
{
int k=0;
while (m!=NULL && m->next!=NULL)
{
p->next=m->next;
m=m->next;
p=p->next;
}
}
return h->next;
}
};
207 Course Schedule
class Solution
{
public:
vector< vector< int> > grid;
vector<int> income;
set<int> active;
bool canFinish(int numCourses, vector<pair<int, int> >& prerequisites)
{
int len=prerequisites.size();
if (len==0) return true;
grid.clear();
grid.resize(numCourses+5);
active.clear();
for(int i=0;i<=numCourses;i++)
{
income.push_back(0);
active.insert(i);
}
for(int i=0;i<len;i++)
{
int f = prerequisites[i].first;
int s = prerequisites[i].second;
income[s]++;
grid[f].push_back(s);
}
bool flag = true;
while(flag)
{
flag = false;
for(set<int>::iterator i=active.begin();i!=active.end();i++)
{
int x=(*i);
if (income[x]==0)
{
flag = true;
for(int j=0;j<grid[x].size();j++)
{
income[grid[x][j]]--;
}
active.erase(i);
break;
}
}
}
if (active.size()!=0)
{
return false;
}
return true;
}
};
210 Course Schedule II
class Solution
{
public:
vector< vector< int> > grid;
vector<int> income;
set<int> active;
vector<int> findOrder(int numCourses, vector<pair<int, int> >& prerequisites)
{
vector<int>ans;
ans.clear();
int len=prerequisites.size();
grid.clear();
grid.resize(numCourses+5);
active.clear();
for(int i=0;i<numCourses;i++)
{
income.push_back(0);
active.insert(i);
}
for(int i=0;i<len;i++)
{
int s = prerequisites[i].first;
int f = prerequisites[i].second;
income[s]++;
grid[f].push_back(s);
}
bool flag = true;
while(flag)
{
flag = false;
for(set<int>::iterator i=active.begin();i!=active.end();i++)
{
int x=(*i);
if (income[x]==0)
{
ans.push_back(x);
flag = true;
for(int j=0;j<grid[x].size();j++)
{
income[grid[x][j]]--;
}
active.erase(i);
break;
}
}
}
if (active.size()==0) return ans;
else
{
ans.clear();
return ans;
}
}
};