Mrudul (@dev_mrudul) header
Mrudul (@dev_mrudul) profile picture

Aspiring Software Developer ⚫ Coding in C++ ⚫ Learning JS and Java

India
Joined October 2021
LeetCode Problem 50. Pow(x, n) Implement pow(x, n), which calculates x raised to the power n (i.e., x^n). Sol : O(logn) time & O(1) space (Binary Exponentiation) #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
1
1
LeetCode Problem 134: Gas Station Given 2 arrays A & B, return starting index of circular path AB such that at no time summation(A[i]-B[i]) is negative Sol : O(n) time & O(1) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
2
3
Problem : Count Inversions Type (2) Link : leetcode.com/problems/global… Check if local and global inversions are different . Local Inversion : a[i]>a[i+1] Sol : O(n) time & O(1) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
1
1
1
This solution is tricky . Since array only contains numbers from 0 to n-1 (unique) and all local inversions are global inversions, we need to calculate a number which is displaced from its index more than 1 place i.e. abs(index-a[index])>1
Problem : Count Inversions in Array Two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j. Sol : O(nlogn) time & O(n) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
2
2
Problem : Find Duplicate and Missing Number Constant array of n integers, each is in the range [1, n] inclusive. 1 integer is repeated and 1 is missing Sol : O(n) time & O(1) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
1
LeetCode Problem 287: Find Duplicate Array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. Sol : O(n) time & O(1) space (C++/Java) #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
1
3
5
LeetCode Problem 88: Merge Sorted Array Merge into 1 sorted array A1 .Extra space is given in A1 to hold both arrays A1 = [1,2,3,0,0,0] A2 = [2,5,6] Sol : O(m+n) time & O(1) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
1
3
3
LeetCode Problem : Check Valid Matrix An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive). Sol : O(n^2) time & O(1) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
1
1
XOR of ‘x’ with 0 is 'x' XOR of ‘x’ with itself even number of times is 0 XOR of ‘x’ with itself odd number of times is 'x' Do XOR of arr[i] with its index i+1 array=[1,2,3] then, [1^1^2^2^3^3]=0 so no duplicates array=[1,1,3] then, [1^1^1^2^3^3]!=0 so duplicates present
LeetCode Problem 56: Merge Intervals Given an array of intervals where intervals[i] = [start-i, end-i], merge all overlapping intervals Sol : O(nlogn) time & O(n) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
1
5
4
This problem has many interesting edge cases . We use prev and curr to keep track of our overlapped window and subsequently merge the intervals
LeetCode Problem 48: Rotate Image Given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). Sol : O(n^2) time & O(1) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
2
2
LeetCode Problem 121: Buy & Sell Stock Prices[i] is the price of a given stock on the ith day. Maximize profit by choosing a day to buy & other day to sell Sol : O(n) time & O(1) space #100DaysOfCode #programming #codingisfun #code #SoftwareEngineer #developers #problemsolving
1
2
1
Small and big track smallest and biggest seen during traversal . We also track the largest profit we have seen in each iteration . If we find a smaller number we update both small and big because as previous value of big is now useless . If we find a bigger value we update big
LeetCode Problem 75: Sort Colors Given an integer array of 0,1,2 , sort it in O(n) time Solution : O(n) time and O(1) space #100DaysOfCode #programming #Coding #code #softwareengineer #Developers #ProblemSolving
2
4
LeetCode Problem 53. Maximum Subarray (Kadane's Algorithm) Given an integer array , find the contiguous subarray with largest sum Solution : O(n) time and O(1) space #100DaysOfCode #programming #Coding #code #softwareengineer #Developers #problemsolving
6
6
LeetCode Problem 31: Next Permutation Find next permutation of given sequence Solution : O(n) time and O(1) space #100DaysOfCode #programming #Coding #code #softwareengineer #javascript #Developers #problemsolving #CProgramming
1
3
1
First we find out the point where sequence stops increasing from back , if i is break point then we find the element just greater than arr[i] from i+1 to n-1 , we swap these 2 elements and reverse remaining array from i+1 to n-1 to get next permutation
1
LeetCode Problem 118: Pascal's Triangle Find Pascal's triangle with n rows Solution : O(n^2) time and space #100DaysOfCode #programming #Coding #SoftwareEngineer
1
2
3
We use a vector of vectors to return solution and start from 2nd row(0 based indexing) ,1st and last element of every row will be 1 and the numbers between them will be sum of = (number just above + number before above number)
1
LeetCode Problem 73: Set Matrinx Zeroes Set i row and j col 0 if matrix[i][j]=0 Solution1 : O(mn) time and O(m+n) space Efficient : O(mn) time and O(1) space #100DaysOfCode #programming #Coding #SoftwareEngineer
2
8
5
We use first block of every row and column as a flag for that row and column, whether it needs to be 0 or not . As a result, first row and column will be used as a flag for others and can't store their own result, so we use boolean col and row as indicators for 1st row and column
LeetCode Problem 231 : Power Of Two Find if n , an integer , is power of 2 . An integer is power of 2 if we can write n=2^x Recursion : O(log(n)) time and space Using Logarithms : O(log(n)) time and O(1) space #100DaysOfCode #programming #Coding #SoftwareEngineer
LeetCode Problem 198 : House Robber Given an array of numbers , find max sum such that no 2 numbers are consecutive Memoization : O(n) time and space #100DaysOfCode #programming #Coding #SoftwareEngineer
1
2
5
My first approach was memoization one , I couldn't guess O(1) space one