thepracticaldev.com thepracticaldev.com

LeetCode 198. House Robber (javascript solution)

Description: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given an integer array nums representing the amount of money of each house, return...

thepracticaldev.com thepracticaldev.com

LeetCode 46. Permutations (javascript solution)

Description: Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Solution: Time Complexity : O(n!) Space Complexity: O(n) var permute = function(choices, temp = [], permutations = []) { // Base case if(choices.length === 0){ permutations.push([...temp]); } for(let i = 0; i < choices.length; i++){ // Create new array without...

thepracticaldev.com thepracticaldev.com

LeetCode 208. Implement Trie (Prefix Tree) (javascript solution)

Description: A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. Implement the Trie class: Trie() Initializes the trie object. void insert(String word) Inserts the string word into the trie. boolean search(String word) Returns true if the string...

thepracticaldev.com thepracticaldev.com

LeetCode 64. Minimum Path Sum (javascript solution)

Description: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Solution: Time Complexity : O(n^2) Space Complexity: O(n^2) var minPathSum = function(grid) { // Create table const dp = new Array(grid.length).fill(0).map(() =>...

thepracticaldev.com thepracticaldev.com

LeetCode 142. Linked List Cycle II (javascript solution)

Description: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. Notice that you should not modify the linked...