From c53098225dd41f33b20a2e7bcedfefa61364df65 Mon Sep 17 00:00:00 2001 From: joric Date: Wed, 24 Jul 2019 18:47:19 +0500 Subject: [PATCH] updated content --- custom.css | 2 +- .../arrays/min-steps-in-infinite-grid.md | 52 +++++++++++++++++++ programming/checkpoints/divisor-sequences.md | 5 -- .../checkpoints/valid-binary-search-tree.md | 44 ++++++++-------- .../level-order.md | 47 ++++++++++++++++- 5 files changed, 121 insertions(+), 29 deletions(-) diff --git a/custom.css b/custom.css index a4c8ddd..753af1a 100644 --- a/custom.css +++ b/custom.css @@ -1,5 +1,5 @@ /* - github-stylized docsify-compatible theme + docsify-like github-stylized theme (untested on docsify) https://github.com/joric/interviewbit */ diff --git a/programming/arrays/min-steps-in-infinite-grid.md b/programming/arrays/min-steps-in-infinite-grid.md index 45080cb..4ac47f4 100644 --- a/programming/arrays/min-steps-in-infinite-grid.md +++ b/programming/arrays/min-steps-in-infinite-grid.md @@ -61,6 +61,58 @@ Take a few examples to convince yourself. ## Solution +### Editorial +```cpp +int Solution::coverPoints(vector x, vector y) { + if (x.size() <= 1) return 0; + assert(x.size() == y.size()); + int ans = 0; + for (int i = 1; i < x.size(); i++) { + ans += max(abs(x[i] - x[i-1]), abs(y[i] - y[i-1])); + } + return ans; +} +``` + +### Fastest +```cpp +int Solution::coverPoints(vector &A, vector &B) { + int n, a, b, distance = 0; + for (n = 1; n < A.size(); n++) { + a = (A[n] > A[n - 1]) ? A[n] - A[n - 1] : A[n - 1] - A[n]; + b = (B[n] > B[n - 1]) ? B[n] - B[n - 1] : B[n - 1] - B[n]; + if (a > b) + distance += a; + else + distance += b; + } + return distance; +} +``` + +### Lightweight +```cpp +int Solution::coverPoints(vector &A, vector &B) { + int count = A.size(); + int i = 0; + int steps = 0, diff = 0; + for (i = 1; i < count; i++) { + if (abs(A[i] - A[i - 1]) == abs(B[i] - B[i - 1])) { + steps += abs(A[i] - A[i - 1]); + } else if ((diff = abs(abs(A[i] - A[i - 1]) - abs(B[i] - B[i - 1]))) > 0) { + if (abs(A[i] - A[i - 1]) > abs(B[i] - B[i - 1])) { + steps += (diff + abs(B[i] - B[i - 1])); + } else { + steps += (diff + abs(A[i] - A[i - 1])); + } + } + } + return steps; +} +``` + +### Mine + ```cpp int Solution::coverPoints(vector &A, vector &B) { int n = A.size(); diff --git a/programming/checkpoints/divisor-sequences.md b/programming/checkpoints/divisor-sequences.md index d7f8ecb..a84b20b 100644 --- a/programming/checkpoints/divisor-sequences.md +++ b/programming/checkpoints/divisor-sequences.md @@ -32,11 +32,6 @@ Output 2: 4 (1, 3, 6, 24) ``` -## Hint 1 - - -## Solution Approach - ## Solution ### Editorial diff --git a/programming/checkpoints/valid-binary-search-tree.md b/programming/checkpoints/valid-binary-search-tree.md index 0a4ffd3..e2da718 100644 --- a/programming/checkpoints/valid-binary-search-tree.md +++ b/programming/checkpoints/valid-binary-search-tree.md @@ -33,18 +33,8 @@ Return 0 / 1 ( 0 for false, 1 for true ) for this problem ## Solution +### Editorial ```cpp -// editorial - -/** - * Definition for binary tree - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ #define ll long long int int isBST(TreeNode *a, ll min, ll max) { if (a == NULL) @@ -58,16 +48,9 @@ int Solution::isValidBST(TreeNode *a) { return 0; return isBST(a, 1LL * (1LL * INT_MIN - 1), 1LL * (1LL * INT_MAX + 1)); } - -/** - * Definition for binary tree - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ +``` +### Mine +```cpp bool checkBST(TreeNode *root, int min, int max) { if (!root) return true; @@ -79,4 +62,21 @@ bool checkBST(TreeNode *root, int min, int max) { int Solution::isValidBST(TreeNode *A) { return checkBST(A, INT_MIN, INT_MAX); } -``` \ No newline at end of file +``` + +### Another one + +```cpp +int checkBST(TreeNode *root, TreeNode *l=0, TreeNode *r=0) { + if (!root) return true; + if (l && l->val >= root->val) return false; + if (r && r->val <= root->val) return false; + return checkBST(root->left, l, root) + && checkBST(root->right, root, r); +} + +int Solution::isValidBST(TreeNode *root) { + return checkBST(root); +} +``` + diff --git a/programming/graph-data-structure-and-algorithms/level-order.md b/programming/graph-data-structure-and-algorithms/level-order.md index 6a5a33f..a940ceb 100644 --- a/programming/graph-data-structure-and-algorithms/level-order.md +++ b/programming/graph-data-structure-and-algorithms/level-order.md @@ -40,12 +40,13 @@ Approach 2: This is important. A lot of times, you'd be asked to do a traditiona ## Solution +### Editorial + ```cpp void buildVector(TreeNode *root, int depth, vector > &ret) { if(root == NULL) return; if(ret.size() == depth) ret.push_back(vector()); - ret[depth].push_back(root->val); buildVector(root->left, depth + 1, ret); buildVector(root->right, depth + 1, ret); @@ -56,6 +57,50 @@ vector > Solution::levelOrder(TreeNode *root) { buildVector(root, 0, ret); return ret; } +``` +### Lightweight + +```cpp +vector> Solution::levelOrder(TreeNode *A) { + int levelSize = 1, nextLevel = 0; + vector> result; + vector curLevel; + + if (A == NULL) + return result; + + queue q; + + q.push(A); + + while (q.empty() != true) { + TreeNode *node = q.front(); + q.pop(); + + if (node->left != NULL) { + q.push(node->left); + nextLevel++; + } + + if (node->right != NULL) { + q.push(node->right); + nextLevel++; + } + + levelSize--; + curLevel.push_back(node->val); + if (levelSize == 0) { + result.push_back(curLevel); + levelSize = nextLevel; + nextLevel = 0; + curLevel.clear(); + } + delete (node); + } + + return result; +} + ``` ## Asked in