373 Find K Pairs with Smallest Sums
class Solution {
public:
struct Node
{
int sum,i,j;
Node(int s,int x,int y):sum(s),i(x),j(y){};
};
vector<Node> sums;
static bool cmp(Node x, Node y)
{
if (x.sum==y.sum) return x.i>y.i;
return x.sum<y.sum;
}
vector<pair<int,int> > kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k)
{
vector<pair<int, int> > ans;
ans.clear();
int l1 = nums1.size(),l2=nums2.size();
for(int i=0;i<l1;i++)
{
for(int j=0;j<l2;j++)
{
int s = nums1[i] + nums2[j];
Node node = Node(s,nums1[i],nums2[j]);
sums.push_back(node);
}
}
sort(sums.begin(),sums.end(),cmp);
int iupper = min(k,l1*l2);
for(int i=0;i<iupper;i++)
{
ans.push_back(pair<int,int>(sums[i].i,sums[i].j));
}
return ans;
}
};
322 Coin Change
// The complete backpack DP
// for i=1..N
// for v=0..V
// f[v]=max{f[v],f[v-cost]+weight}
class Solution {
public:
static bool cmp(int x,int y)
{
return x>y;
}
int coinChange(vector<int>& coins, int amount)
{
if(amount == 0) return 0;
int lcoins = coins.size();
sort(coins.begin(),coins.end(),cmp);
vector<long long> f(amount+10,INT_MAX);
for(int i=0;i<lcoins;i++)
{
// Here causes RE, I should verify the value of coins[i] bounding to amount
if (coins[i]<=amount)
f[coins[i]]=1;
}
for(int i=0;i<lcoins;i++)
{
for(int j=0;j<=amount;j++)
{
if (j-coins[i]>0)
{
f[j]=min(f[j-coins[i]]+1,f[j]);
}
}
}
if (f[amount]==INT_MAX) return -1;
else return f[amount];
}
};
// -----------------RE 1st------------
// Using backpack DP
// the RE data piont
// [2147483647]
// 2
// I forgot to verify the value of coins[i], they should below the value of amount,
// otherwise it will cause RE.
// Code:
/*
class Solution {
public:
static bool cmp(int x,int y)
{
return x>y;
}
int coinChange(vector<int>& coins, int amount)
{
if(amount == 0) return 0;
sort(coins.begin(),coins.end(),cmp);
int lcoins = coins.size();
vector<long long> f(amount+10,INT_MAX);
for(int i=0;i<lcoins;i++)
f[coins[i]]=1;
for(int i=0;i<lcoins;i++)
{
for(int j=amount;j>=0;j--)
{
if (j-coins[i]>0)
{
f[j]=min(f[j-coins[i]]+1,f[j]);
}
}
}
if (f[amount]==INT_MAX) return -1;
else return f[amount];
}
};
*/
324 Wiggle Sort II
// -----------Seeing the Discuss--------------
// https://discuss.leetcode.com/topic/32929/o-n-o-1-after-median-virtual-indexing
// https://discuss.leetcode.com/topic/41464/step-by-step-explanation-of-index-mapping-in-java/2
class Solution {
public:
void wiggleSort(vector<int>& nums)
{
int n = nums.size();
auto midptr = nums.begin()+n/2;
nth_element(nums.begin(),midptr,nums.end());
int mid = *midptr;
int i= 0, j=0,k = n-1;
#define A(i) nums[(1+2*(i)) % (n|1)]
while(j<=k)
{
if (A(j)>mid)
swap(A(i++),A(j++));
else if (A(j)<mid)
swap(A(j),A(k--));
else
j++;
}
}
};
// ----------- WA 1st------
// 41 / 44 test cases passed.
// the WA data points:
// Input:
// [4,5,5,6]
// Output:
// [4,5,5,6]
// Expected:
// [5,6,4,5]
// totally wrong thought:
// code
/*
class Solution {
public:
void wiggleSort(vector<int>& nums)
{
int lnums = nums.size();
sort(nums.begin(),nums.end());
vector<int> tmpnums = nums;
int half = lnums / 2, k = 0;
if (lnums % 2 == 0)
{
for(int i=0;i<half;i++)
{
nums[k++]=tmpnums[i];
nums[k++]=tmpnums[i+half];
}
}
else
{
for(int i=0;i<half;i++)
{
nums[k++]=tmpnums[i];
nums[k++]=tmpnums[i+half+1];
}
nums[k++]=tmpnums[half];
}
return ;
}
};
*/
316 Remove Duplicate Letters
// --------------Seeing Disscudd
// Analysis
/*
""" Given the string s, the greedy choice (i.e., the leftmost letter in the answer) is the smallest s[i], s.t.
the suffix s[i .. ] contains all the unique letters. (Note that, when there are more than one smallest s[i]'s,
we choose the leftmost one. Why? Simply consider the example: "abcacb".)
After determining the greedy choice s[i], we get a new string s' from s by
removing all letters to the left of s[i],
removing all s[i]'s from s.
We then recursively solve the problem w.r.t. s'.
*/
//Code
class Solution {
public:
string removeDuplicateLetters(string s)
{
if (s.size()==0) return "";
vector<int> cnt(26,0);
int pos = 0;// the position for the smallest s[i]
for(int i=0;i<s.size();i++)
cnt[s[i]-'a']++;
for(int i=0;i<s.size();i++)
{
if (s[i]<s[pos]) pos = i;
if(--cnt[s[i]-'a'] ==0) break;
}
string str ="";
for(int i=pos+1;i<s.size();i++)
{
if (s[i]!=s[pos])
{
str += s.substr(i,1);
}
}
return s.substr(pos,1)+removeDuplicateLetters(str);
}
};