diff --git a/solved/LeetCode/Challenges/2020/January/01.cc b/solved/LeetCode/Challenges/2020/January/01.cc new file mode 100644 index 00000000..0973fad0 --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/01.cc @@ -0,0 +1,29 @@ +#include + +using namespace std; + +class Solution { + public: + bool canFormArray(vector& arr, vector>& pieces) { + int idx = 0; + while (idx < arr.size()) { + int p_id = -1; + for (int i = 0; i < pieces.size(); i++) { + if (pieces[i][0] == arr[idx]) { + p_id = i; + break; + } + } + if (p_id == -1) { + return false; + } + for (int j = 0; j < pieces[p_id].size(); j++) { + if (arr[idx] != pieces[p_id][j]) { + return false; + } + idx++; + } + } + return idx == arr.size(); + } +}; diff --git a/solved/LeetCode/Challenges/2020/January/02.cc b/solved/LeetCode/Challenges/2020/January/02.cc new file mode 100644 index 00000000..e9dbb86d --- /dev/null +++ b/solved/LeetCode/Challenges/2020/January/02.cc @@ -0,0 +1,25 @@ +#include + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +class Solution { + public: + TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) { + if (cloned == nullptr) { + return nullptr; + } + if (cloned->val == target->val) { + return cloned; + } + TreeNode* left = getTargetCopy(original, cloned->left, target); + if (left != nullptr) { + return left; + } + return getTargetCopy(original, cloned->right, target); + } +};