Posts

Showing posts from August, 2024

55. Jump Game

  Approach calculate the maxIndex possible from every point if index is maximum from maxIndex than return false else return true at end Complexity Time complexity: O(n) Space complexity: O(n) Code Java class Solution { public boolean canJump ( int [ ] nums ) { int maxIndex = 0 ; for ( int i = 0 ; i < nums . length ; i ++ ) { if ( maxIndex < i ) { return false ; } else { maxIndex = Math . max ( maxIndex , i + nums [ i ] ) ; } } return true ; } }

3264. Final Array State After K Multiplication Operations I

  /** * @param {number[]} nums * @param {number} k * @param {number} multiplier * @return {number[]} */ var getFinalState = function(nums, k, multiplier) { for(let i = 0; i < k; i++){ let x = minIndex(nums); nums[x] = nums[x] * multiplier; } return nums; }; var minIndex = (arr) => { let min = Number.POSITIVE_INFINITY; arr.forEach((e,i) => { min = Math.min(min,e); }); return arr.indexOf(min); };

2634. Filter Elements from Array

Intuition Using forEach loop to access all elements from given array Approach create a new array pass all value of the given array to to the function if the function return true add the value into the new array Complexity Time complexity: O(n) Space complexity: O(n) Code:  /** * @param {number[]} arr * @param {Function} fn * @return {number[]} */ var filter = function(arr, fn) { let newArr = new Array(); let i = 0; arr.forEach((e , index) => { if(fn(e,index)){ newArr[i] = e; i++; } }); return newArr; };

122. Best Time to Buy and Sell Stock II

 Intuition go for every possible solution Approach we can solve it using greedy algorithm where we calculate the profite without looking for fourther Complexity Time complexity: O(n) Space complexity: O(1) Code class Solution {     public int maxProfit(int[] prices) {         int max = 0;         int buyPrice = prices[0];         for(int i = 0; i < prices.length; i++){             if(prices[i] > buyPrice){                 max += prices[i] - buyPrice;             }             buyPrice = prices[i];         }         return max;     } }
 80. Remove Duplicates from Sorted Array II Approach two pointer solution Complexity Time complexity: O(n) Space complexity: o(1) Code class Solution {     public int removeDuplicates(int[] nums) {         if(nums.length <= 2){             return nums.length;         }         int j = 2;         for(int i = 2; i < nums.length; i++){             if(nums[i] != nums[j-2]){                 nums[j] = nums[i];                 j++;             }         }         return j;     } }
  58. Length of Last Word Intuition find the last index of space Approach slice the string from the last index of space than return the length of the string Complexity Time complexity: O(n) Space complexity: O(n) Code java class Solution {     public int lengthOfLastWord(String s) {         s = s.trim();         int lastIndex = s.lastIndexOf(" ");         return s.substring(lastIndex+1).length();     } } javaScript /**  * @param {string} s  * @return {number}  */ var lengthOfLastWord = function(s) {     s = s.trim();     let lastIndex = s.lastIndexOf(" ");     s = s.slice(lastIndex+1);     return s.length;      };
121. Best Time to Buy and Sell Stock Intuition Best time of buy is as low as possible and sell it on the it is high price Approach Start with let if we buy on first date and check for 2nd day if the stoke price is lower in second day change the buy day to second and else find the max sell price Complexity Time complexity: O(n) Space complexity: O(1) Code Java class Solution { public int maxProfit ( int [ ] prices ) { int max = 0 ; int buy = prices [ 0 ] ; for ( int i = 0 ; i < prices . length ; i ++ ) { if ( prices [ i ] > buy ) { max = Math . max ( max , ( prices [ i ] - buy ) ) ; } else { buy = prices [ i ] ; } } return max ; } }