Posts

Showing posts from August, 2020

Plus One

  Given a   non-empty   array of digits representing a non-negative integer, increment one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself.   Example 1: Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Example 2: Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Example 3: Input: digits = [0] Output: [1]   Constraints: 1 <= digits.length <= 100 0 <= digits[i] <= 9 Solution: Algorithm: 1) Add 1 to the nth element in the array. If the sum is greater than 9 then take the ten's element (save it to 'carry' ) and move to the (n-1)th element.  2) Continue step 1 till we don't have a carry. Time Complexity:  O(n) Space Complexity: O(1)

Climbing Stairs

You are climbing a stair case. It takes  n  steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Example 1: Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example 2: Input: 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step   Constraints: 1 <= n <= 45 Solution: The point to observe here is that while climbing stairs, you can take either 1 or 2 steps. So for every step that you climb, you have two options in front of you to choose from i.e., 1 step or 2 step. This is a typical case of recursion where in you let the system decide the number of ways to climb the stairs by writing minimal code. The entire problem is broken down into sub-problems and you will encounter similar sub-problems. So to increase the efficiency of the solution, we can store the outpu...

Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new  sorted  list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4  Output: 1->1->2->3->4->4   Solution: The approach here is similar to the "merge" step in Merge Sort . Time Complexity:  O(m+n) where m and n are length of two linked lists. Space Complexity: O(m+n) where m and n are length of two linked lists.

Valid Parentheses

Given a string containing just the characters  '(' ,  ')' ,  '{' ,  '}' ,  '['  and  ']' , determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1: Input: "()" Output: true Example 2: Input: "()[]{}" Output: true Example 3: Input: "(]" Output: false Example 4: Input: "([)]" Output: false Example 5: Input: "{[]}"  Output: true   Solution: We will use stacks here.  Algorithm: 1) If the element encountered is '{' , '[' and '(', we will push it into stack. 2)  If the element encountered is ']','}' and ')', we will check if it's complement is present at the top of stack.  If the complement is not present then we will return false else we will repeat s...

Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string  "" . Example 1: Input: ["flower","flow","flight"] Output: "fl" Example 2: Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Note: All given inputs are in lowercase letters  a-z . Solution: The requirement here is to find longest common prefix amongst an array of strings.  There are many approaches to this problem but the one that I am going to discuss here uses Trie to store the array of strings and then scan the trie to get the longest common prefix. To know more about Trie, you can visit here . Algorithm: 1) Store each string from the array of strings in Trie. 2) Get the deepest Trie Node such that all the nodes from the root of the trie to that node has only one children. Space Complexity: O(n) ...

Roman To Integer

Roman numerals are represented by seven different symbols:  I ,  V ,  X ,  L ,  C ,  D  and  M . Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as  II  in Roman numeral, just two one's added together. Twelve is written as,  XII , which is simply  X  +  II . The number twenty seven is written as  XXVII , which is  XX  +  V  +  II . Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not  IIII . Instead, the number four is written as  IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as  IX . There are six instances where subtraction is used: I  can be placed before  V  (5) and  X  (10) to ...

Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Could you solve it without converting the integer to a string?   Solution: One approach is to convert the number to string and then compare the string with it's reverse. Second approach which is also the answer to the follow-up question is to reverse the number and then compare the original and the reversed number. Time Complexity : O(log x) Space Complexity: O(1)

Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2 31 ,  2 31  − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Solution: Time Complexity: O(log x) Space Complexity: O(1)

Remove Element from Unsorted Array - Easy

  Given an array   nums   and a value   val , remove all instances of that value   in-place   and return the new length. Do not allocate extra space for another array, you must do this by  modifying the input array  in-place  with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Example 1: Given nums = [3,2,2,3] , val = 3 , Your function should return length = 2 , with the first two elements of nums being 2 . It doesn't matter what you leave beyond the returned length. Example 2: Given nums = [0,1,2,2,3,0,4,2] , val = 2 , Your function should return length = 5 , with the first five elements of nums containing  0 , 1 , 3 , 0 , and  4 . Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length. Clarification: Confused why the returned value is an integer but your answer is an array? Note t...

Remove Duplicates - Easy

Given a sorted array  nums , remove the duplicates  in-place  such that each element appear only  once  and return the new length. Do not allocate extra space for another array, you must do this by  modifying the input array  in-place  with O(1) extra memory. Example 1: Given nums = [1,1,2] , Your function should return length = 2 , with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length. Example 2: Given nums = [0,0,1,1,1,2,2,3,3,4] , Your function should return length = 5 , with the first five elements of nums being modified to  0 , 1 , 2 , 3 , and  4 respectively. It doesn't matter what values are set beyond the returned length. Clarification: Confused why the returned value is an integer but your answer is an array? Note that the input array is passed in by  reference , which means modification to the input array will be known to the caller as we...

Two Sum

Given an array of integers, return   indices   of the two numbers such that they add up to a specific target. You may assume that each input would have  exactly  one solution, and you may not use the  same  element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[ 0 ] + nums[ 1 ] = 2 + 7 = 9, return [ 0 , 1 ]. Solution: Since for each element 'x' present in the array, we will have to check whether a number 'y' exists in the array such that sum of two numbers is target and return indices of the number.  We can also say that, for any element 'x' present in the array , find a number 'target-x' which is also present in the array. One approach to the problem is to scan the complete array for finding number 'target-x' for each element 'x'.  Another approach is to use dictionaries. For each element 'x' in the array, check whether the dictionary contains element 'target-x'. So here, first we will scan the...