Commit 4d2c7601 authored by anqiwa's avatar anqiwa 😲
Browse files

10/30/2021 update

parent adad553e
/*
* @lc app=leetcode id=103 lang=cpp
*
* [103] Binary Tree Zigzag Level Order 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
{
public:
vector<vector<int>> zigzagLevelOrder(TreeNode *root)
{
int count = 0; // even, left->right; odd reverse order
if (!root)
return {};
queue<TreeNode *> q;
vector<vector<int>> res;
q.push(root);
while (!q.empty())
{
int len = q.size();
vector<int> level;
for (int i = 0; i < len; ++i)
{
TreeNode *curr = q.front();
q.pop();
if (curr->left)
q.push(curr->left);
if (curr->right)
q.push(curr->right);
level.push_back(curr->val);
}
if (count % 2 != 0)
reverse(level.begin(), level.end());
++count;
res.push_back(level);
}
return res;
}
};
// @lc code=end
/**
* 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
{
double currMax = DBL_TRUE_MIN;
public:
double maximumAverageSubtree(TreeNode *root)
{
auto currP = helper(root);
return currMax;
}
// returns a pair {size of sub tree, sum of subtree}
// do postorder traversal
pair<int, int> helper(TreeNode *root)
{
if (!root)
return {0, 0};
auto leftP = helper(root->left);
auto rightP = helper(root->right);
int subTreeSum = leftP.second + rightP.second + root->val;
int subTreeSize = leftP.first + rightP.first + 1;
currMax = max(currMax, double(subTreeSum) / double(subTreeSize));
return {subTreeSize, subTreeSum};
}
};
\ No newline at end of file
/*
* @lc app=leetcode id=146 lang=cpp
*
* [146] LRU Cache
*/
// @lc code=start
class LRUCache
{
int capacity_;
//* we keep the back node as the most recently used
list<int> cache; //* O(1) remove + O(1) add to back
unordered_map<int, list<int>::iterator> key2itr; //* allows use to access node in linked list in O(1)
unordered_map<int, int> key2val; //* from key to val in O(1) time
public:
LRUCache(int capacity) : capacity_(capacity) {}
int get(int key)
{
if (key2val.find(key) == key2val.end())
return -1;
auto itr = key2itr[key];
int val = key2val[key];
cache.erase(itr);
cache.push_back(key);
key2itr[key] = prev(cache.end());
return val;
}
void put(int key, int value)
{
if (this->get(key) != -1)
{
key2val[key] = value;
}
else
{
if (key2itr.size() == capacity_)
{
int LRUkey = *cache.begin();
key2val.erase(LRUkey);
cache.erase(cache.begin());
key2itr.erase(LRUkey);
}
cache.push_back(key);
key2val[key] = value;
key2itr[key] = prev(cache.end());
}
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
// @lc code=end
// key4 ... key7 key3 key2 key8 end
//
\ No newline at end of file
/*
* @lc app=leetcode id=1510 lang=cpp
*
* [1510] Stone Game IV
*/
// @lc code=start
class Solution
{
public:
bool winnerSquareGame(int n)
{
vector<int> memo(n + 1, 0);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j * j <= i; ++j)
{
if (memo[i - j * j] == 0)
{
memo[i] = 1;
break;
}
}
}
return memo[n];
}
};
// @lc code=end
// define dp[i] <- the the win or lose for the i stones in the pile
/*
* @lc app=leetcode id=222 lang=cpp
*
* [222] Count Complete Tree Nodes
*/
// @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
{
public:
int countNodes(TreeNode *root)
{
// setup a bound for binary search
//* 2^(h-1) ~ 2^h-1
// find h
int h = 0;
TreeNode *ptr = root;
while (ptr)
{
++h;
ptr = ptr->left;
}
int lo = pow(2, h - 1);
int hi = pow(2, h) - 1;
int mid;
while (lo < hi)
{
mid = lo + (hi - lo + 1) / 2;
bool hasKEle = hasK(root, mid);
if (hasKEle)
{
lo = mid;
}
else
{
hi = mid - 1;
}
}
return lo;
}
// for this problem, it is very important ot use the special attributes of complete binary tree
// imagine the tree as indexed by position 1,2,3,4...
// then for any node, its left child is (node->idx) * 2,
// its right child is (node->idx) *2 + 1
//* given treeRoot and int k, return if the tree has k nodes
bool hasK(TreeNode *root, int k)
{
// from k to root
vector<int> path;
while (k > 0)
{
path.push_back(k);
k /= 2;
}
// reverselly treverse
for (int i = path.size() - 1; i >= 0; --i)
{
if (!root)
{
return false;
}
if (i == 0)
return true;
if (path[i - 1] == 2 * path[i])
{
root = root->left;
}
else
{
root = root->right;
}
}
return false;
}
};
// @lc code=end
// 1
// 2 3
// 4 5 6 7
//
// 4 ~ 7
// mid = 5
// low = 6 hight = 7
// mid = 6
// 7+7/2
//[6,3,1]
//[7,3,1]
\ No newline at end of file
/*
* @lc app=leetcode id=23 lang=cpp
*
* [23] Merge k Sorted Lists
*/
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution
{
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
ListNode dummy(-1, nullptr);
ListNode *dumPtr = &dummy;
while (hasVal(lists))
{
int minNode = findMinNode(lists);
cout << minNode << endl;
ListNode *temp = lists[minNode]->next;
lists[minNode]->next = nullptr;
dumPtr->next = lists[minNode];
dumPtr = dumPtr->next;
lists[minNode] = temp;
}
return dummy.next;
}
bool hasVal(vector<ListNode *> &lists)
{
for (auto &node : lists)
{
if (node)
return true;
}
return false;
}
int findMinNode(vector<ListNode *> lists)
{
ListNode *res = nullptr;
int idx = 0;
for (int i = 0; i < lists.size(); ++i)
{
if (lists[i])
{
res = lists[i];
idx = i;
break;
}
}
for (int j = idx; j < lists.size(); ++j)
{
if (lists[j] && lists[j]->val < res->val)
{
res = lists[j];
idx = j;
}
}
return idx;
}
};
// @lc code=end
// 1,4,5
// 1,3,4
// 2,6
\ No newline at end of file
/*
* @lc app=leetcode id=23 lang=cpp
*
* [23] Merge k Sorted Lists
*/
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution
{
struct cmp
{
bool operator()(ListNode *a, ListNode *b)
{
if (a->val != b->val)
{
return a->val > b->val;
}
return true;
}
};
public:
ListNode *mergeKLists(vector<ListNode *> &lists)
{
ListNode dummy(-1, nullptr);
ListNode *dumPtr = &dummy;
priority_queue<ListNode *, vector<ListNode *>, cmp> pq;
// add all the top nodes into the list
for (int i = 0; i < lists.size(); ++i)
{
if (lists[i])
{
pq.push(lists[i]);
}
}
while (!pq.empty())
{
ListNode *min = pq.top();
pq.pop();
if (min->next)
pq.push(min->next);
min->next = nullptr;
dumPtr->next = min;
dumPtr = dumPtr->next;
} //while
return dummy.next;
}
};
// @lc code=end
// 1,4,5
// 1,3,4
// 2,6
\ No newline at end of file
/*
* @lc app=leetcode id=501 lang=cpp
*
* [501] Find Mode in Binary Search Tree
*/
// @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
{
int currFrq = 0; // the curr element frequency
int maxFrq = 0; // the max frq
int prev = 0; // the prev element
vector<int> res; // the final result set
public:
// in bst, whenever order matters, we use inorder traversal
vector<int> findMode(TreeNode *root)
{
inorder(root);
return res;
}
void inorder(TreeNode *root)
{
if (!root)
return;
inorder(root->left);
// do things
if (maxFrq == 0)
{
currFrq = maxFrq = 1;
res = {root->val};
}
else
{
if (root->val == prev)
{
currFrq++;
if (currFrq > maxFrq)
{
res = {root->val};
maxFrq = currFrq;
}
else if (currFrq == maxFrq)
{
res.push_back(root->val);
}
}
else
{
currFrq = 1;
if (currFrq == maxFrq)
{
res.push_back(root->val);
}
}
}
prev = root->val;
inorder(root->right);
}
};
// @lc code=end
/*
* @lc app=leetcode id=938 lang=cpp
*
* [938] Range Sum of BST
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;05
* 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
{
int sum = 0;
public:
int rangeSumBST(TreeNode *root, int low, int high)
{
helper(root, low, high);
return sum;
}
void helper(TreeNode *root, int low, int high)
{
if (!root)
return;
else if (root->val <= high && root->val >= low)
{
sum += root->val;
helper(root->left, low, high);
helper(root->right, low, high);
}
else if (root->val < low)
helper(root->right, low, high);
else if (root->val > high)
helper(root->left, low, high);
}
};
// @lc code=end
/*
* @lc app=leetcode id=95 lang=cpp
*
* [95] Unique Binary Search Trees II
*/
// @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
{
public:
vector<TreeNode *> generateTrees(int n)
{
if (n == 0)
return {};
return dfs(1, n);
}
// return all the possible bst in the closed range [begin, end]
// 5,5 6,5 5,4
vector<TreeNode *> dfs(int begin, int end)
{
if (begin > end)
{
return {NULL};
}
vector<TreeNode *> subTree;
for (int i = begin; i <= end; ++i)
{
auto leftSubTree = dfs(begin, i - 1);
auto rightSubTree = dfs(i + 1, end);
// idea: use multi rule to find form all possible bst
for (int j = 0; j < leftSubTree.size(); ++j)
for (int k = 0; k < rightSubTree.size(); ++k)
{
TreeNode *node = new TreeNode(i);
node->left = leftSubTree[j];
node->right = rightSubTree[k];
subTree.push_back(node);