Skip to content

Instantly share code, notes, and snippets.

View vamsitallapudi's full-sized avatar
🚩
Be better than yesterday

Vamsi Tallapudi vamsitallapudi

🚩
Be better than yesterday
View GitHub Profile
class Solution {
public int rob(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
dp[0] = nums[0];
for(int i = 1; i< n;i++) {
int pick = nums[i];
if(i>1) pick += dp[i-2];
int nonPick = dp[i-1];
dp[i] = Math.max(pick, nonPick);
@vamsitallapudi
vamsitallapudi / HouseRobber.java
Created March 30, 2023 15:35
Leetcode Problem 198 - House Robber
class Solution {
public int rob(int[] nums) {
int n = nums.length;
int[] dp = new int[n+1];
Arrays.fill(dp,-1);
return rec(n-1, nums, dp);
}
private int rec(int i, int[] nums, int[] dp) {
if(i == 0) return nums[i];
import java.util.ArrayDeque
import java.util.LinkedList
fun postOrderIterative(root: TreeNode?): List<Int> {
if(root == null) return emptyList()
val stack = ArrayDeque<TreeNode>()
val list = LinkedList<Int>()
stack.push(root)
while (stack.isNotEmpty()) {
val node = stack.pop()
fun postOrderRecursive(root: TreeNode?) : List<Int>{
if(root == null) return emptyList()
return postOrderRecursive(root.left) + postOrderRecursive(root.right) + listOf(root.data)
}
import java.util.ArrayDeque
fun inorderTraversalIterative(root: TreeNode?): List<Int> {
val list = mutableListOf<Int>()
if (root == null) return list
var node = root
val stack = ArrayDeque<TreeNode>()
// traversing the tree whenever right node is not null or the stack contains items
while (node != null || stack.isNotEmpty()) {
// processing all the left nodes of the current node
fun inOrderTraversalRecursive(root: TreeNode?) : List<Int> {
if (root == null) return emptyList()
return inOrderTraversalRecursive(root.left) + listOf(root.data) + inOrderTraversalRecursive(root.right)
}
import java.util.ArrayDeque
fun preOrderTraversalIterative(root: TreeNode?): List<Int> {
val myList = mutableListOf<Int>()
// creating stack to store the left and right nodes while processing root node
val stack = ArrayDeque<TreeNode>()
// checking edge case and returning empty list
if (root == null) return myList
var node = root
while (node != null || stack.isNotEmpty()) {
class TreeNode(var data: Int, var left: TreeNode? = null, var right: TreeNode? = null)
fun initializeBinaryTree(): TreeNode {
val c = TreeNode(4)
val d = TreeNode(5)
val a = TreeNode(2, c, d)
val b = TreeNode(3)
return TreeNode(1, a, b)
}
fun preorderTraversal(root: TreeNode?): List<Int> {
// if root is null, return list of empty array
if (root == null) {
return listOf()
}
// iterate recursively into left child and right child
return listOf(root.data) + preorderTraversal(root.left) + preorderTraversal(root.right)
}
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
# to return an empty array if root is empty
if not root:
return []
# appending the root value in array and recursively going into left and right subtrees
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)