Skip to content

Commit

Permalink
Add some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed May 16, 2024
1 parent d4bf288 commit dcabb80
Show file tree
Hide file tree
Showing 18 changed files with 316 additions and 0 deletions.
30 changes: 30 additions & 0 deletions leetcode/problems/cpp/largest-local-values-in-a-matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "vector"

class Solution {
public:
vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
int n = grid.size();

for (int i = 1; i < n - 1; ++i) {
for (int j = 1; j < n - 1; ++j) {
int temp = 0;

for (int k = i - 1; k <= i + 1; ++k) {
for (int l = j - 1; l <= j + 1; ++l) {
temp = max(temp, grid[k][l]);
}
}

grid[i - 1][j - 1] = temp;
}
}

// resizing
grid.resize(n - 2);
for (int i = 0; i < grid.size(); ++i) {
grid[i].resize(n - 2);
}

return grid;
}
};
19 changes: 19 additions & 0 deletions leetcode/problems/java/balanced-binary-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public boolean isBalanced(TreeNode root) {
return dfs(root) != -1;
}

int dfs(TreeNode root) {
if (root == null)
return 0;
int left = dfs(root.left);
if (left == -1)
return -1;
int right = dfs(root.right);
if (right == -1)
return -1;
if (Math.abs(left - right) > 1)
return -1;
return 1 + Math.max(left, right);
}
}
26 changes: 26 additions & 0 deletions leetcode/problems/java/binary-tree-level-order-traversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.List;
import java.util.ArrayList;

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> levels = new ArrayList<>();
if (root == null)
return levels;
List<TreeNode> level = new ArrayList<>();
level.add(root);
while (!level.isEmpty()) {
List<TreeNode> nextLevel = new ArrayList<>();
List<Integer> values = new ArrayList<>();
for (TreeNode node : level) {
values.add(node.val);
if (node.left != null)
nextLevel.add(node.left);
if (node.right != null)
nextLevel.add(node.right);
}
levels.add(values);
level = nextLevel;
}
return levels;
}
}
17 changes: 17 additions & 0 deletions leetcode/problems/java/binary-tree-maximum-path-sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int maxPathSum(TreeNode root) {
var result = new int[] { Integer.MIN_VALUE };
maxPathSum(root, result);
return result[0];
}

int maxPathSum(TreeNode root, int[] result) {
if (root == null) {
return 0;
}
int left = Math.max(maxPathSum(root.left, result), 0);
int right = Math.max(maxPathSum(root.right, result), 0);
result[0] = Math.max(left + right + root.val, result[0]);
return Math.max(left, right) + root.val;
}
}
19 changes: 19 additions & 0 deletions leetcode/problems/java/binary-tree-right-side-view.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.List;
import java.util.ArrayList;

class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> rightSide = new ArrayList<>();
rightView(root, 0, rightSide);
return rightSide;
}

void rightView(TreeNode root, int level, List<Integer> rightSide) {
if (root == null)
return;
if (level == rightSide.size())
rightSide.add(root.val);
rightView(root.right, level + 1, rightSide);
rightView(root.left, level + 1, rightSide);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.HashMap;

class Solution {
int pi = 0;

public TreeNode buildTree(int[] preorder, int[] inorder) {
HashMap<Integer, Integer> inorderMap = new HashMap<>();
for (int i = 0; i < inorder.length; i++)
inorderMap.put(inorder[i], i);
return solve(preorder, inorderMap, 0, inorder.length);
}

TreeNode solve(int[] preorder, HashMap<Integer, Integer> inorderMap, int left, int right) {
if (left == right)
return null;
TreeNode root = new TreeNode(preorder[pi++]);

int inorderIndex = inorderMap.get(root.val);
root.left = solve(preorder, inorderMap, left, inorderIndex);
root.right = solve(preorder, inorderMap, inorderIndex + 1, right);

return root;

}
}
19 changes: 19 additions & 0 deletions leetcode/problems/java/count-good-nodes-in-binary-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
int count = 0;

public int goodNodes(TreeNode root) {
dfs(root, root.val);
return count;
}

void dfs(TreeNode root, int max) {
if (root == null)
return;
if (root.val >= max) {
count++;
max = root.val;
}
dfs(root.left, max);
dfs(root.right, max);
}
}
17 changes: 17 additions & 0 deletions leetcode/problems/java/diameter-of-binary-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
int ans = 0;

public int diameterOfBinaryTree(TreeNode root) {
dfs(root);
return ans;
}

int dfs(TreeNode root) {
if (root == null)
return 0;
int left = dfs(root.left);
int right = dfs(root.right);
ans = Math.max(ans, left + right);
return 1 + Math.max(left, right);
}
}
20 changes: 20 additions & 0 deletions leetcode/problems/java/evaluate-boolean-binary-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public boolean evaluateTree(TreeNode root) {
if (root == null)
return false;
boolean left = evaluateTree(root.left);
if (left)
if (root.val == 2)
return true;
if (!left)
if (root.val == 3)
return false;
if (root.val == 2)
return left || evaluateTree(root.right);
if (root.val == 3)
return left && evaluateTree(root.right);
if (root.val == 0)
return false;
return true;
}
}
12 changes: 12 additions & 0 deletions leetcode/problems/java/invert-binary-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null)
return null;
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
}
23 changes: 23 additions & 0 deletions leetcode/problems/java/kth-smallest-element-in-a-bst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
int n, ans = 0;

public int kthSmallest(TreeNode root, int k) {
n = k;
dfs(root);
return ans;
}

void dfs(TreeNode root) {
if (root == null || n == 0)
return;
dfs(root.left);
if (n == 1) {
ans = root.val;
n--;
return;
} else {
n--;
}
dfs(root.right);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode curr = root;
while (curr != null) {
if (p.val < curr.val && q.val < curr.val) {
curr = curr.left;
} else if (p.val > curr.val && q.val > curr.val) {
curr = curr.right;
} else {
return curr;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.Arrays;

class Solution {
public long maximumHappinessSum(int[] happiness, int k) {
Arrays.sort(happiness);
long res = 0;
int n = happiness.length, j = 0;
for (int i = n - 1; i >= n - k; --i) {
happiness[i] = Math.max(happiness[i] - j++, 0);
res += happiness[i];
}
return res;
}
}
7 changes: 7 additions & 0 deletions leetcode/problems/java/maximum-depth-of-binary-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution {
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
11 changes: 11 additions & 0 deletions leetcode/problems/java/same-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
if (p == null || q == null)
return false;
if (p.val != q.val)
return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
19 changes: 19 additions & 0 deletions leetcode/problems/java/subtree-of-another-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null)
return false;
if (isSameTree(root, subRoot))
return true;
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}

Boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null)
return true;
if (p == null || q == null)
return false;
if (p.val != q.val)
return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
14 changes: 14 additions & 0 deletions leetcode/problems/java/validate-binary-search-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
TreeNode prev = null;

public boolean isValidBST(TreeNode root) {
if (root == null)
return true;
if (!isValidBST(root.left))
return false;
if (prev != null && prev.val >= root.val)
return false;
prev = root;
return isValidBST(root.right);
}
}
9 changes: 9 additions & 0 deletions leetcode/problems/sql/combine-two-tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT
p.firstName,
p.lastName,
a.city,
a.state
FROM
Person p
LEFT JOIN
Address a ON p.personId = a.personId;

0 comments on commit dcabb80

Please sign in to comment.