LC Blind 75 Flashcards
Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
Naive Solution: cycle through list for each element and identify the indices of the target values
Optimal Solution: Create dict with [key, value] as [value, index] and check for target - value as a key. If in the dict, return values, if not then add to the dict
Best Time to Buy and Sell Stock
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.
Initialize currMin and currMax
Check if current delta with Min is > Max
check if value < min and reset if necessary
Consider Kadane’s Algorithm for max subarray problem in general
Climbing Stairs
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Fibonacci Number
Each step can be reached from two places - step (n-1) and step (n-2). To calculate each step’s possible outcomes one must calculate the prior two steps the whole way.
Contains Duplicate
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.
- Loop through array, check if element in set. Return if true, add if not
- compare length of array to set of array. Duplicates will result in a reduced size for the set
Set Matrix Zeroes
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0’s, and return the matrix.
You must do it in place.
Optimal Speed Solution using O(m+n) space:
- Set array for row and for column equal to length of appropriate dimension
- iterate through whole array checking for zeroes. If detected, set appropriate row and col marker to 0.
- Run through array again, setting any values that match the row or col 0 markers to be 0.
To make this O(1) space:
use the first row and column of the array as the markers
Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
Sliding Window Technique
- If s[r] has not been seen, increment the output.
- If s[r] has been seen, but the value is less than the left marker, increment the output
- If s[r] has been seen, but the value is greater than the left marker, move the left marker to the index past where it was seen
- increment/set the index for the right marker in seen
Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth.
A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Recursively check each layer, looking for leaf nodes. At leaf nodes return (1).
Anywhere the node splits, return the max of the helper function while adding 1 each layer.
Only edge case is empty tree, return 0.
House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
- Check base conditions: array fewer than 3 values and compare first two values.
- Each step, we will iterate the array and rewrite the value with the optimal value at this point in the array. We will compare the previous value with the sum of the current and n-2 values.
- After one iteration, we can return the final value
Reverse a Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list.
- Set base conditions: check for empty node
- Define required variables: prev and curr to track reverse and forward linked list respectively. Set prev.next to be None.
- while curr is not empty, set the next values as a temp variable. Then redirect the current node to prev (the reverse node. Then set prev to be curr (the reversed list including the most recent node). Then reset curr to be the next node we had set aside at the beginning.
- These steps will repeat, reversing the list one step at a time. Return the reversed list (prev).
Product of Array Except Self
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.
The trivial solution to this is to use the division operator, but that is one of the conditions listed. So we will make a two pass solution.
Pass 1: we will fill our output array with the product of each number leading up to that point, but not including that index.
Pass 2: we will do the same, but in reverse. This results in creating an output array that has multiplied all values together and managed to skip the value at each index.
Longest Repeating Character Replacement
You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.
Return the length of the longest substring containing the same letter you can get after performing the above operations.
Declare max length, max count, and count (dict).
Much like longest subsequence, we will be iterating and checking at each iteration if we’re at a new best or not.
First, we will increment the dict entry for the next character of the string.
Then we will check the max count as the max against itself and the count at the current character.
max length will always be k + max count. This is because whatever the max count in a range is, we can add k to that for our max length. If max length is less than k + max count, increment max length (it should never increment by more than one).
If max length did not grow, then we must contract the window from the back, decrement the character at max length distance from the front of the window.
This will repeat and is very similar to Kadane’s algorithm. We will return max length
Minimum Window Substring
Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string “”.
The testcases will be generated such that the answer is unique.
A substring is a contiguous sequence of characters within the string.
Pieces to account for:
- Hash table to store needed char frequency (collections.Counter)
- Total characters needed (missing = len(t))
- Start and End initialized to 0
- Iterator i initialized to 0 (to account for left side window)
We will do a sliding window over the enumerate (s, 1)
Each step we will do the following
- Check if the char’s hash is greater than 0. If so, decrement missing by 1 and decrement the character’s hash
- If missing == 0, then all values have been located in the substring.
- If missing == 0, we will start sliding i/left forward until we find the true start of this substring. After we find the true substring, we will proceed with previous steps or increment i
Coin Change
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Dynamic Programming Solution
Begin by initializing the dp array, which is [0] + [amount + 1 for n in amount]. We will be checking the best solution for each value from 1 to amount.
Next, we will start a nested loop. The outer loop holding the coins, the inner loop ranging from (coin, amount). The inner loop will compare to find the min at each step comparing dp[i] and dp[i - coin] + 1. This is because we want whatever is lower of using this coin, and using the coin implies the latter. The former suggests that a previous denomination had a more optimal result and we will use that for now.
We will return the last value of the array IFF it is >= amount. Otherwise return -1.
Same Tree
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Recursive Solution:
Each step we want to check that the two tree values being compared are 1. not null, and 2. the same.
If p and q, then we will return the result of the vals of p and q, and the recursive step for left and right.
To account for Null/Null case, we will return p is q after this step to close the recursive step
Invert Binary Tree
Given the root of a binary tree, invert the tree, and return its root.
We will be recursively stepping through the tree, each time flipping the left and right children and then calling the invert function on the left and right children respectively.
Detect Cycle in a Linked List
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
The method we will be using is two pointers, one fast and one slow.
First we must check if the head is null and initialize the fast pointer (we will be using the head as the slow pointer).
Then while the fast and fast.next are valid, we will continue to loop through and increment the slow pointer (by one) and fast pointer (by two).
If there is a cycle, the fast pointer will “catch up” to the slow pointer and eventually equal it. If there is no cycle, then we will reach the end of the linked list.
Clone Graph
Given a reference of a node in a connected undirected graph.
Return a deep copy (clone) of the graph.
Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
Test case format:
For simplicity, each node’s value is the same as the node’s index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list.
An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.
We will be approaching with iterative DFS approach.
- First we must check the edge case of an empty graph and return
- Next we must define the necessary pieces: nodeCopy (initialized instance of Node with empty neighbors), dic ({node: nodeCopy}), and stack (our iteration mechanic).
- While stack is not empty we will pop values off (neighbor) and then iterate through each neighbor.
- If we have not seen a neighbor before, we must establish an instance of the node (Node(neighbor.val, [])). This will be neighborCopy
4a. Then we must set the dic[neighbor] to neighborCopy.
4b. Next we will append neighborCopy to dic[Node].neighbors
4c. Last we will append neighbor to stack - If we have seen the neighbor before, we will just append dic[neighbor] to dic[node].neighbors
- return nodeCopy
Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
A subarray is a contiguous part of an array.
We will be creating a dynamic programming approach
- initialize current and max sum variables
- iterate througheach num in nums from 1 to the end
- at each step, set the current to the max of the current + num and the num. This will reset the count if any individual num is greater than the highest total before it.
- set max to the max of current and max
- return max