Skip to content

Commit

Permalink
updated content
Browse files Browse the repository at this point in the history
  • Loading branch information
joric committed Jul 24, 2019
1 parent adb35d8 commit c530982
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 29 deletions.
2 changes: 1 addition & 1 deletion custom.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
github-stylized docsify-compatible theme
docsify-like github-stylized theme (untested on docsify)
https://github.com/joric/interviewbit
*/

Expand Down
52 changes: 52 additions & 0 deletions programming/arrays/min-steps-in-infinite-grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,58 @@ Take a few examples to convince yourself.

## Solution

### Editorial
```cpp
int Solution::coverPoints(vector<int> x, vector<int> 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<int> &A, vector<int> &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<int> &A, vector<int> &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<int> &A, vector<int> &B) {
int n = A.size();
Expand Down
5 changes: 0 additions & 5 deletions programming/checkpoints/divisor-sequences.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ Output 2:
4 (1, 3, 6, 24)
```

## Hint 1


## Solution Approach

## Solution

### Editorial
Expand Down
44 changes: 22 additions & 22 deletions programming/checkpoints/valid-binary-search-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -79,4 +62,21 @@ bool checkBST(TreeNode *root, int min, int max) {
int Solution::isValidBST(TreeNode *A) {
return checkBST(A, INT_MIN, INT_MAX);
}
```
```

### 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);
}
```
47 changes: 46 additions & 1 deletion programming/graph-data-structure-and-algorithms/level-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<vector<int> > &ret) {
if(root == NULL) return;
if(ret.size() == depth)
ret.push_back(vector<int>());

ret[depth].push_back(root->val);
buildVector(root->left, depth + 1, ret);
buildVector(root->right, depth + 1, ret);
Expand All @@ -56,6 +57,50 @@ vector<vector<int> > Solution::levelOrder(TreeNode *root) {
buildVector(root, 0, ret);
return ret;
}
```
### Lightweight
```cpp
vector<vector<int>> Solution::levelOrder(TreeNode *A) {
int levelSize = 1, nextLevel = 0;
vector<vector<int>> result;
vector<int> curLevel;
if (A == NULL)
return result;
queue<TreeNode *> 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
Expand Down

0 comments on commit c530982

Please sign in to comment.