LeetCode Study Plan - Algorithm I

Ace your homework & exams now with Quizwiz!

19. LeetCode Study Plan - Algorithm I - Day 9 - Matrix 994. Rotting Oranges Medium You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1. Example 1: Input: grid = [[2,1,1],[1,1,0],[0,1,1]] Output: 4 Example 2: Input: grid = [[2,1,1],[0,1,1],[1,0,1]] Output: -1 Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally. Example 3: Input: grid = [[0,2]] Output: 0 Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0. Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 10 grid[i][j] is 0, 1, or 2.

Hints: - BFS. - Make the first pass to add all the cells with value 2 (rotten orange) to a queue (deque). Add additional (-1, -1) to the queue to delimit each set of rotten orange cells. This will be used to identify that one minute passed. - Also, in the same pass, count all the fresh oranges. - Also, initialize the minutes variable with -1. - While looping, check if there is (-1,-1) in the queue, then increment the minutes variable by 1. - Inside the loop, while setting the cells value to 2 (rotten), decrement the fresh orange by 1.

LeetCode Study Plan - Algorithm I - Day 7 - Breadth-First Search / Depth-First Search 695. Max Area of Island Medium You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. The area of an island is the number of cells with a value 1 in the island. Return the maximum area of an island in grid. If there is no island, return 0. Example 1: Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]] Output: 6 Explanation: The answer is not 11, because the island must be connected 4-directionally. Example 2: Input: grid = [[0,0,0,0,0,0,0,0]] Output: 0 Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 50 grid[i][j] is either 0 or 1.

Hints: - BFS. - Start with a loop and call BFS only for the cells with values 1. - One call for each island. - Each BFS call for the islands will do recursion to find the area of the island. - Use two variables for area. One for max area and another for area of each island which will be reset before calling for each island. class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) self.currArea = 0 def areaOfIsland(r, c): if r < 0 or r > m-1 or c < 0 or c > n-1 or grid[r][c] == 0: return 0 if grid[r][c] == 1: grid[r][c] = 0 self.currArea += 1 areaOfIsland(r+1, c) areaOfIsland(r-1, c) areaOfIsland(r, c+1) areaOfIsland(r, c-1) return self.currArea maxArea = 0 for r in range(m): for c in range(n): if grid[r][c] == 1: maxArea = max(maxArea, areaOfIsland(r,c)) self.currArea = 0 return maxArea

LeetCode Study Plan - Algorithm I - Day 8 - Breadth-First Search / Depth-First Search 617. Merge Two Binary Trees Easy You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree. Return the merged tree. Note: The merging process must start from the root nodes of both trees. Example 1: Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7] Output: [3,4,5,5,4,null,7] Example 2: Input: root1 = [1], root2 = [1,2] Output: [2,2] Constraints: The number of nodes in both trees is in the range [0, 2000]. -104 <= Node.val <= 104

Hints: - Both BFS and DFS will work. - DFS is simpler. - In each iteration, pass two nodes, merge both nodes and return node1. Accepted code: O(n) # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]: # DFS # if root1 is None: # return root2 # if root2 is None: # return root1 # root1.val += root2.val # root1.left = self.mergeTrees(root1.left, root2.left) # root1.right = self.mergeTrees(root1.right, root2.right) # return root1 #BFS if root1 is None: return root2 stack = [] stack.append((root1, root2)) while stack: nodes = stack.pop() if nodes[0] is None or nodes[1] is None: continue nodes[0].val += nodes[1].val if nodes[0].left is None: nodes[0].left = nodes[1].left else: stack.append((nodes[0].left, nodes[1].left)) if nodes[0].right is None: nodes[0].right = nodes[1].right else: stack.append((nodes[0].right, nodes[1].right)) return root1

18. LeetCode Study Plan - Algorithm I - Day 9 - Matrix 542. 01 Matrix Medium Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1. Example 1: Input: mat = [[0,0,0],[0,1,0],[0,0,0]] Output: [[0,0,0],[0,1,0],[0,0,0]] Example 2: Input: mat = [[0,0,0],[0,1,0],[1,1,1]] Output: [[0,0,0],[0,1,0],[1,2,1]] Constraints: m == mat.length n == mat[i].length 1 <= m, n <= 104 1 <= m * n <= 104 mat[i][j] is either 0 or 1. There is at least one 0 in mat.

Hints: - DP or BFS - For DP, two passes, One from 0 index to last element and another pass from last element to 0 index. - We need two passes to see from which direction the shortest distance is from a zero. - Initiate the output matrix with the largest number i.e. math.inf. - A straightforward BFS will also work. - Initialize the output matrix. - During BFS, add an additional cell to the queue only if you modify that cell. - You do not need an "visited" set to avoid an infinite loop.

LeetCode Study Plan - Algorithm I - Day 7 - Breadth-First Search / Depth-First Search 733. Flood Fill Easy An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and newColor. You should perform a flood fill on the image starting from the pixel image[sr][sc]. To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with newColor. Return the modified image after performing the flood fill. Example 1: Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2 Output: [[2,2,2],[2,2,0],[2,0,1]] Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color. Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel. Example 2: Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2 Output: [[2,2,2],[2,2,2]] Constraints: m == image.length n == image[i].length 1 <= m, n <= 50 0 <= image[i][j], newColor < 216 0 <= sr < m 0 <= sc < n

Hints: - Recursion. - BFS. - Starting cell is the root. - The four-directional cells form one level of BFS. Accepted Code: O(n) class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]: visited = set() m, n = len(image), len(image[0]) oldColor = image[sr][sc] def fill(r,c): if r < 0 or r > m-1 or c < 0 or c > n-1: return if (r,c) not in visited: visited.add((r,c)) if image[r][c] == oldColor: image[r][c] = newColor fill(r+1,c) fill(r-1,c) fill(r, c+1) fill(r, c-1) fill(sr,sc) return image

LeetCode Study Plan - Algorithm I - Day 6 - Sliding Window 567. Permutation in String Medium Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1's permutations is the substring of s2. Example 1: Input: s1 = "ab", s2 = "eidbaooo" Output: true Explanation: s2 contains one permutation of s1 ("ba"). Example 2: Input: s1 = "ab", s2 = "eidboaoo" Output: false Constraints: 1 <= s1.length, s2.length <= 104 s1 and s2 consist of lowercase English letters.

Hints: - Sliding window. - Use Counter. - One iteration of size len(s1) - Another iteration of size len(s2) with sliding window. Accepted code: O(n) class Solution: def checkInclusion(self, s1: str, s2: str) -> bool: ## using built-in function # m, n = len(s1), len(s2) # count1 = Counter(s1) # for i in range(n-m+1): # if count1 == Counter(s2[i:i+m]): # return True # return False ## faster way m, n = len(s1), len(s2) if m > n: return False count = Counter(s1) for i in range(m): if s2[i] in count: count[s2[i]] -= 1 if min(count.values()) == 0 and max(count.values()) == 0: return True start, end = 1, m while end < n: if s2[start-1] in count: count[s2[start-1]] += 1 if s2[end] in count: count[s2[end]] -= 1 if min(count.values()) == 0 and max(count.values()) == 0: return True start += 1 end += 1 return False

LeetCode Study Plan - Algorithm I - Day 4 - Two Pointers 557. Reverse Words in a String III Easy Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Example 2: Input: s = "God Ding" Output: "doG gniD" Constraints: 1 <= s.length <= 5 * 104 s contains printable ASCII characters. s does not contain any leading or trailing spaces. There is at least one word in s. All the words in s are separated by a single space.

Hints: - Split the string into words - Convert each word into list and reverse them using list slicing method. - Join each reversed word into string Accepted code: O(n) class Solution: def reverseWords(self, s: str) -> str: ## without using built-in list slicing def reverse(word): i, j = 0, len(word)-1 while i < j: word[i], word[j] = word[j], word[i] i += 1 j -= 1 return ''.join(word) words = s.split() return ' '.join([reverse(list(word)) for word in words]) One line solution using built-in methods Accepted code: O(n) class Solution: def reverseWords(self, s: str) -> str: return ' '.join([word[::-1] for word in s.split(' ')])

9. LeetCode Study Plan - Algorithm I - Day 4 - Two Pointers 344. Reverse String Easy Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. Example 1: Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"] Example 2: Input: s = ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"] Constraints: 1 <= s.length <= 105 s[i] is a printable ascii character.

Hints: - Start from 0 and len(s) - 1 - At each iteration, swap start with end. - Meet at the center to finish the iteration. Accepted code: O(n) class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ ## not using in-built method i, j = 0, len(s)-1 while i < j: s[i], s[j] = s[j], s[i] i += 1 j -= 1

LeetCode Study Plan - Algorithm I - Day 8 - Breadth-First Search / Depth-First Search 116. Populating Next Right Pointers in Each Node Medium You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Example 1: Input: root = [1,2,3,4,5,6,7] Output: [1,#,2,3,#,4,5,6,7,#] Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level. Example 2: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 212 - 1]. -1000 <= Node.val <= 1000 Follow-up: You may only use constant extra space. The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.

Hints: - This is just doing things from left most parent's perspective. - Complete one level at a time and come down to all the way down to leave nodes Accepted code: O(n) """ # Definition for a Node. class Node: def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): self.val = val self.left = left self.right = right self.next = next """ class Solution: def connect(self, root: 'Optional[Node]') -> 'Optional[Node]': leftMost = root while leftMost: levelHead = leftMost while levelHead: if levelHead.left and levelHead.right: levelHead.left.next = levelHead.right if levelHead.next and levelHead.next.left and levelHead.next.right: levelHead.right.next = levelHead.next.left levelHead = levelHead.next leftMost = leftMost.left return root

LeetCode Study Plan - Algorithm I - Day 6 - Sliding Window 3. Longest Substring Without Repeating Characters Medium Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints: 0 <= s.length <= 5 * 104 s consists of English letters, digits, symbols and spaces.

Hints: - This problem will look simple but fails for most test cases. - Sliding window - Use hashmap to store seen letters and their last seen indices. - Maintain a variable "start" to reset the length of the string from the last seen non-repeating character's index. - At any index, the length of the longest substring with non-repeating characters is max(longest, current index - start + 1) Accepted code: O(n) class Solution: def lengthOfLongestSubstring(self, s: str) -> int: seenSet, longest, start = dict(), 0, 0 for i, c in enumerate(s): if c in seenSet: start = max(seenSet[c], start) longest = max(longest, i - start + 1) seenSet[c] = i + 1 return longest

8. LeetCode Study Plan - Algorithm I - Day 3 - Two Pointers 283. Move Zeroes Easy Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Example 1: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] Example 2: Input: nums = [0] Output: [0] Constraints: 1 <= nums.length <= 104 -231 <= nums[i] <= 231 - 1 Follow up: Could you minimize the total number of operations done?

Hints: - Two Pointers - Use Queue to track the zero indices - Swap non-zero and zero using zero index poped from queue by FIFO operation on queue. - Smart method without using extra space - The input array either starts with 0 or 1. - We initialize last seen zero index as 0, the first index. - If we encounter 0 at the first iteration, we save that index in an variable - If we encounter 1 at the first iteration, we anyway do a dummy swap which does not change anything and increment the last seen zero index by 1. - This is goes on with self swap until we find a zero and store the index in last seen zero index. - This is very smart thinking. Accepted code: O(n) Space O(n) class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ zeroIndices = deque() i = 0 while i < len(nums): if nums[i] == 0: zeroIndices.append(i) elif zeroIndices: nums[zeroIndices.popleft()], nums[i] = nums[i], 0 zeroIndices.append(i) i += 1 Accepted code: O(n) Space O(1) class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ lastSeenZeroIndex, n = 0, len(nums) for i in range(n): if nums[i] != 0: nums[lastSeenZeroIndex], nums[i] = nums[i], nums[lastSeenZeroIndex] lastSeenZeroIndex += 1

7. LeetCode Study Plan - Algorithm I - Day 3 - Two Pointers 167. Two Sum II - Input Array Is Sorted Medium 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 one solution. You may not use the same element twice. Your solution must use only constant extra space. Example 1: Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. Example 2: Input: numbers = [2,3,4], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. Example 3: Input: numbers = [-1,0], target = -1 Output: [1,2] Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2]. Constraints: 2 <= numbers.length <= 3 * 104 -1000 <= numbers[i] <= 1000 numbers is sorted in non-decreasing order. -1000 <= target <= 1000 The tests are generated such that there is exactly one solution.

Hints: - Two pointers - Since array is sorted, let us take advantage of its sorted characteristics. - start from 0 and end at the same time and check if target - number at the end of the array is equal to the number at start of the array. - Either move forward from start or move backward from end based on if target - number at the end is greater or less than the number at the start. Accepted code: O(n) Space O(1) class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: i, j = 0, len(numbers) - 1 while i < j: if numbers[i] == target - numbers[j]: return [i+1,j+1] elif numbers[i] < target - numbers[j]: i += 1 elif numbers[i] > target - numbers[j]: j -= 1

LeetCode Study Plan - Algorithm I - Day 5 - Two Pointers 876. Middle of the Linked List Easy Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node. Example 1: Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. Example 2: Input: head = [1,2,3,4,5,6] Output: [4,5,6] Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one. Constraints: The number of nodes in the list is in the range [1, 100]. 1 <= Node.val <= 100

Hints: - Two pointers. - Use slow and fast pointers. - Move slow one node at a time while moving fast two node at a time. - By the time fast reaches either end of the list or pass the end of the list, slow reaches the middle. - We can also use an array as temporary storage of the nodes and return middle element of the array. - Another way is to do two pass. One to get count of the nodes in the linked list and another one is to get middle of the linked list i.e. (length // 2) + 1 Accepted Code: O(n) # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: ## using extra space # arr = [] # node = head # while node: # arr.append(node) # node = node.next # return arr[(len(arr)//2)] ## two pass without extra space # node = head # count = 0 # arr = [] # while node: # arr.append(node) # count += 1 # node = node.next # mid = (count // 2) + 1 # node = head # i = 1 # while i <= mid: # if i == mid: # return node # i += 1 # node = node.next ## Fast and slow pointer slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next return slow

LeetCode Study Plan - Algorithm I - Day 5 - Two Pointers 19. Remove Nth Node From End of List Medium 8954429Add to ListShare Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] Constraints: The number of nodes in the list is sz. 1 <= sz <= 30 0 <= Node.val <= 100 1 <= n <= sz Follow up: Could you do this in one pass?

Hints: - Use two pointers. - Create a dummy node and add it before the head. - Set head to dummy.next . - Set two points forward and following to dummy. - First, Forward pointers is moved from dummy to nth node. - Then Following pointer from dummy and Forward poiner from nth node, move towards the end of the list. - By the time the Forward pointer reaches passing the end node, the Following node will be at the nth node. - Set Forword.next = Forward.next.next Accepted code: O(n) - One Pass class Solution: def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: dummy = ListNode(0) dummy.next = head following = forward = dummy i = 1 while i <= n+1: i += 1 forward = forward.next while forward: following = following.next forward = forward.next following.next = following.next.next return dummy.next


Related study sets

SPC 205 Ch. 14 Using Visual Aids

View Set

core practical 7 use a light microscope to : make observations i.transverse sections of roots, stems, leaves ii.plant tissues iii. identify sclerenchyma fibres ,phloem sieve tubes ,xylem vessels and their location

View Set

Chapter 13: The Spinal Cord, Spinal Nerves, and Somatic Reflexes

View Set

fin. apps // factoring test 3.4.20

View Set

Identifying Linear Functions, FIND "X" AND "Y" INTERCEPTS

View Set

PHYSICS CH.3 REVIEW: DESCRIBING SOUND WAVES

View Set

Soft Tissues Injuries and Infections

View Set

HDFS 3710 Exam 3 Assessment Questions

View Set