Commit 2175f768 authored by anqiwa's avatar anqiwa 😲
Browse files

11/21 2021 update

parent 89635d80
/*
* @lc app=leetcode id=106 lang=cpp
*
* [106] Construct Binary Tree from Inorder and Postorder Traversal
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution
{
unordered_map<int, int> inorder_map; //{val, idx}
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
// make a map for inorder
const int n = inorder.size();
for (int i = 0; i < inorder.size(); ++i)
{
inorder_map[inorder[i]] = i;
}
return helper(inorder, postorder, 0, n - 1, 0, n - 1);
}
TreeNode *helper(vector<int> &inorder, vector<int> &postorder, int inSt, int inEnd, int postSt, int postEnd)
{
// base case
if (inSt > inEnd)
return nullptr;
// int the postorder, the last node int range is the root
int rootVal = postorder[postEnd];
TreeNode *root = new TreeNode(rootVal);
int pos = inorder_map[rootVal];
root->left = helper(inorder, postorder, inSt, pos - 1, postSt, postSt + pos - 1 - inSt);
root->right = helper(inorder, postorder, pos + 1, inEnd, postSt + pos - inSt, postEnd - 1);
return root;
}
};
// @lc code=end
using PII = pair<int, int>;
class Solution
{
static bool comp(PII &a, PII &b)
{
if (a.first != b.first)
{
return a.first < b.first;
}
return a.second < b.second;
}
public:
vector<int> minAvailableDuration(vector<vector<int>> &slots1,
vector<vector<int>> &slots2,
int duration)
{
// sweeping line method
// overlap: 1->2 begin time
// overlap: 2->1 end time
// if end - begin >= duration
// return {begin,end}
vector<PII> time;
for (auto &s : slots1)
{
time.push_back({s[0], 1});
time.push_back({s[1], -1});
}
for (auto &s : slots2)
{
time.push_back({s[0], 1});
time.push_back({s[1], -1});
}
sort(time.begin(), time.end(), comp);
int overlap = 0;
PII range;
for (auto &t : time)
{
int x = t.second;
overlap += x;
if (x == 1 && overlap == 2)
{
range.first = t.first;
}
else if (x == -1 && overlap == 1)
{
range.second = t.first;
if (range.second - range.first >= duration)
{
return {range.first, range.first + duration};
}
}
}
return {};
}
};
\ No newline at end of file
/*
* @lc app=leetcode id=1288 lang=cpp
*
* [1288] Remove Covered Intervals
*/
// @lc code=start
class Solution
{
static bool comp(vector<int> &a, vector<int> &b)
{
if (a[0] != b[0])
return a[0] < b[0];
return a[1] > b[1]; // want to be longest first, so we can cover the following
}
public:
int removeCoveredIntervals(vector<vector<int>> &intervals)
{
// sort + pq
// 按首端点排序
// ##----
// ----
// than the beginning of the first interval is unique and cannot be covered
sort(intervals.begin(), intervals.end(), comp);
const int n = intervals.size();
int i = 0;
int cnt = 0;
while (i < n)
{
int j = i + 1;
cnt++;
while (j < n && intervals[i][1] >= intervals[j][1])
{
// interval[j] is covered by interval[i]
++j;
}
i = j;
}
return cnt;
}
};
// @lc code=end
// sorted by thae starting point
// => min num of intervals to cover the whole range
// [1,4], [1,2], [3,4]
// i
// j
/*
* @lc app=leetcode id=162 lang=cpp
*
* [162] Find Peak Element
*/
// @lc code=start
class Solution
{
public:
int findPeakElement(vector<int> &nums)
{ //经典binary search
int l = 0, r = nums.size() - 1;
while (l < r)
{
int mid = l + (r - l + 1) / 2; // [0,1] mid->1
if (nums[mid - 1] < nums[mid])
{
l = mid;
}
else
{
r = mid - 1;
}
}
return l;
}
};
// @lc code=end
/*
* @lc app=leetcode id=229 lang=cpp
*
* [229] Majority Element II
*/
// @lc code=start
class Solution
{
public:
vector<int> majorityElement(vector<int> &nums)
{
// use the algo in eecs477 lec14
// O(n) time
// O(1) space
int c1 = 0, c2 = 0;
int elt1, elt2;
for (auto &i : nums)
{
if (c1 == 0 && elt2 != i)
elt1 = i;
else if (c2 == 0 && elt1 != i)
elt2 = i;
if (i == elt1)
++c1;
else if (i == elt2)
++c2;
else
{
--c1;
--c2;
}
}
// check promise
vector<int> res;
c1 = 0;
c2 = 0;
for (auto &i : nums)
{
if (i == elt1)
++c1;
if (i == elt2)
++c2;
}
if (c1 > nums.size() / 3)
res.push_back(elt1);
if (c2 > nums.size() / 3)
res.push_back(elt2);
return res;
}
};
// @lc code=end
/*
* @lc app=leetcode id=352 lang=cpp
*
* [352] Data Stream as Disjoint Intervals
*/
// @lc code=start
class SummaryRanges
{
public:
SummaryRanges()
{
}
void addNum(int val)
{
}
vector<vector<int>> getIntervals()
{
}
};
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges* obj = new SummaryRanges();
* obj->addNum(val);
* vector<vector<int>> param_2 = obj->getIntervals();
*/
// @lc code=end
/*
* @lc app=leetcode id=435 lang=cpp
*
* [435] Non-overlapping Intervals
*/
// @lc code=start
class Solution
{
static bool comp(vector<int> &a, vector<int> &b)
{
return a[1] < b[1];
}
public:
int eraseOverlapIntervals(vector<vector<int>> &intervals)
{
// 这个题是区间型贪心法的模板题
// max num of interval that non-overlapping -> sorted by end point
sort(intervals.begin(), intervals.end(), comp);
const int n = intervals.size();
int i = 0;
int cnt = 0;
while (i < n)
{
cnt++;
int j = i + 1;
while (j < n && intervals[j][0] < intervals[i][1])
++j;
i = j;
}
return n - cnt;
}
};
// @lc code=end
/*
* @lc app=leetcode id=448 lang=cpp
*
* [448] Find All Numbers Disappeared in an Array
*/
// @lc code=start
class Solution
{
public:
vector<int> findDisappearedNumbers(vector<int> &nums)
{ // O(n)
vector<int> res;
nums.insert(nums.begin(), 0);
// indexing sort?
const int n = nums.size();
int i = 1;
while (i < n)
{
while (nums[i] != i && nums[i] != nums[nums[i]])
{
swap(nums[i], nums[nums[i]]);
}
++i;
}
for (int i = 1; i < n; ++i)
{
if (nums[i] != i)
res.push_back(i);
}
return res;
}
};
// @lc code=end
/*
* @lc app=leetcode id=461 lang=cpp
*
* [461] Hamming Distance
*/
// @lc code=start
class Solution
{
public:
int hammingDistance(int x, int y)
{
int t = x ^ y;
int cnt = 0;
while (t)
{
cnt += t & 1;
t = t >> 1;
}
return cnt;
}
};
// @lc code=end
/*
* @lc app=leetcode id=557 lang=cpp
*
* [557] Reverse Words in a String III
*/
// @lc code=start
class Solution
{
public:
string reverseWords(string s)
{
stringstream ss(s);
string res;
string word;
while (ss >> word)
{
reverse(word.begin(), word.end());
res += word;
res += " ";
}
res.pop_back();
return res;
}
};
// @lc code=end
/*
// Definition for an Interval.
class Interval {
public:
int start;
int end;
Interval() {}
Interval(int _start, int _end) {
start = _start;
end = _end;
}
};
*/
using PII = pair<int, int>;
class Solution
{
struct comp
{
bool operator()(PII &a, PII &b)
{
if (a.first != b.first)
return a.first > b.first;
return a.second < b.second;
}
};
public:
vector<Interval> employeeFreeTime(vector<vector<Interval>> schedule)
{
priority_queue<PII, vector<PII>, comp> pq;
for (int i = 0; i < schedule.size(); ++i)
{
for (int j = 0; j < schedule[i].size(); ++j)
{
pq.push({schedule[i][j].start, 1});
pq.push({schedule[i][j].end, -1});
}
}
int overlap = 0;
vector<Interval> res;
Interval curr(-1, -1);
while (!pq.empty())
{
auto [time, num] = pq.top();
pq.pop();
overlap += num;
if (overlap == 0 && num == -1)
{
curr.start = time;
}
else if (overlap == 1 && num == 1 && curr.start != -1)
{
curr.end = time;
res.push_back(curr);
}
}
return res;
}
};
// ----| |
// | |----
// ----| |
// ----
// overlap 1->0 is the begin
// overlap 0->1 is the end
\ No newline at end of file
/*
* @lc app=leetcode id=986 lang=cpp
*
* [986] Interval List Intersections
*/
// @lc code=start
using PII = pair<int, int>;
class Solution
{
static bool comp(PII &a, PII &b)
{
if (a.first != b.first)
{
return a.first < b.first;
}
return a.second > b.second;
}
public:
vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList, vector<vector<int>> &secondList)
{
vector<PII> time;
for (auto &s : firstList)
{
time.push_back({s[0], 1});
time.push_back({s[1], -1});
}
for (auto &s : secondList)
{
time.push_back({s[0], 1});
time.push_back({s[1], -1});
}
sort(time.begin(), time.end(), comp);
int overlap = 0;
PII range;
vector<vector<int>> res;
for (auto &t : time)
{
int x = t.second;
overlap += x;
if (x == 1 && overlap == 2)
{
range.first = t.first;
}
else if (x == -1 && overlap == 1)
{
range.second = t.first;
res.push_back({range.first, range.second});
}
}
return res;
}
};
// @lc code=end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment