121. Best Time to Buy and Sell Stock using JavaScript

0 Problem You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example … Read more

42. Trapping Rain Water using JavaScript

0 Problem Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. Example 1: Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are … Read more

167. Two Sum II – Input Array Is Sorted using JavaScript

0 Problem Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. The tests are generated such that there is exactly … Read more

125. Valid Palindrome using JavaScript

0 Problem A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. Example 1:Input: s = “A man, a plan, a canal: Panama” Output: true Explanation: “amanaplanacanalpanama” … Read more

36. Valid Sudoku using JavaScript

0 Problem Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Note: Example 1: Input: board = [[“5″,”3″,”.”,”.”,”7″,”.”,”.”,”.”,”.”] ,[“6″,”.”,”.”,”1″,”9″,”5″,”.”,”.”,”.”] ,[“.”,”9″,”8″,”.”,”.”,”.”,”.”,”6″,”.”] ,[“8″,”.”,”.”,”.”,”6″,”.”,”.”,”.”,”3″] ,[“4″,”.”,”.”,”8″,”.”,”3″,”.”,”.”,”1″] ,[“7″,”.”,”.”,”.”,”2″,”.”,”.”,”.”,”6″] ,[“.”,”6″,”.”,”.”,”.”,”.”,”2″,”8″,”.”] ,[“.”,”.”,”.”,”4″,”1″,”9″,”.”,”.”,”5″] ,[“.”,”.”,”.”,”.”,”8″,”.”,”.”,”7″,”9″]] Output: true Example 2:Input: board = [[“8″,”3″,”.”,”.”,”7″,”.”,”.”,”.”,”.”] ,[“6″,”.”,”.”,”1″,”9″,”5″,”.”,”.”,”.”] ,[“.”,”9″,”8″,”.”,”.”,”.”,”.”,”6″,”.”] ,[“8″,”.”,”.”,”.”,”6″,”.”,”.”,”.”,”3″] ,[“4″,”.”,”.”,”8″,”.”,”3″,”.”,”.”,”1″] ,[“7″,”.”,”.”,”.”,”2″,”.”,”.”,”.”,”6″] ,[“.”,”6″,”.”,”.”,”.”,”.”,”2″,”8″,”.”] ,[“.”,”.”,”.”,”4″,”1″,”9″,”.”,”.”,”5″] ,[“.”,”.”,”.”,”.”,”8″,”.”,”.”,”7″,”9″]] Output: false Explanation: Same as … Read more

238. Product of Array Except Self

0 Problem Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation. Example 1:Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2:Input: nums = [-1,1,0,-3,3] … Read more

347. Top K Frequent Elements

0 Problem Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Example 1:Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2:Input: nums = [1], k = 1 Output: [1] Constraints: Solution First attempt : (Can be optimized further) Optimized solution: Aadi AhlawatA freelance web developer with … Read more

49. Group Anagrams

0 Problem Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example 1:Input: strs = [“eat”,”tea”,”tan”,”ate”,”nat”,”bat”] Output: [[“bat”],[“nat”,”tan”],[“ate”,”eat”,”tea”]] Example 2:Input: strs = [“”] Output: [[“”]] Example … Read more

217. Contains Duplicate

0 Problem Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example 1:Input: nums = [1,2,3,1] Output: true Example 2:Input: nums = [1,2,3,4] Output: false Example 3:Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true Constraints: Solution First attempt : (Can be optimized further) Aadi AhlawatA freelance web developer … Read more

242. Valid Anagram

0 Problem Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example 1:Input: s = “anagram”, t = “nagaram” Output: true Example 2:Input: s = “rat”, t = “car” Output: false Constraints: … Read more

Skip to content