Category leetcode

Leetcode 78: Subsets

The original question can be found here. The question is asking to return all subsets of an integer array, and each element in the array is unique. Recursion Normally the question which is asking for all possible solutions are searching.…

Leetcode 209: Minimum Size Subarray Sum

The original question can be found here. The question is asking us to the minimum length of subarray that it’s sum is no less than target value. Brute Force The brute force we can immediately think of is to enumerate…

Leetcode 74: Search a 2D Matrix

The original question can be found here. The question is asking us to find the target element in a 2-dimensional array. Each row is greater than the row above it. Within each row, elements are sorted as well. Brute Force…

Leetcode 35: Search Insert Position

The original question can be found here. The question is asking for a specific target value, if it exists in the array, return the index. Otherwise, return the index position that it should be inserted. Brute Force We just iterate…

Leetcode 162: Find Peak Element

The original question can be found here. It’s asking us to find an element that is grater than both left and right neighbors. Since it’s asking for a O(logn) solution, let’s directly jump to the binary search solution. There’re 4…

Leetcode 33: Search in Rotated Sorted Array

The original question can be found here. The question is asking us to find a target element in rotated sorted array. Since the question is explictly asking for a O(logn) solution, so let’s jump to the binary search solution. After…

Leetcode 852: Peak Index in a Mountain Array

The original question can be found here. The question is asking us to return a peak element, which means, an element is greater than both 2 adjacent neighbors. Brute Force Scan the array: Time complexity: O(n). Space complexity: O(1). Binary…

Leetcode 658: Find K Closest Elements

The original question can be found here. The question is asking to return the k closet elements to target x. The array is sorted. Heap First solution is using a heap to keep track the current closest elements to x.…

Leetcode 704: Binary Search

The original question can be found here. The question is asking us to perform a binary search on an sorted array, which is a classical algorithm. If you haven’t, please visit my another post which detailed describe this algorithm. Time…

Infer Algorithm By Time Complexity

Introduction Sometimes we need to infer algorithms by time complexity. After we bring out the brute force solution, the interviewer may ask: Can you solve by time complexity of O(xxx)? We don’t always know what’s the optimal solution, but we…

Binary Search

Introduction Binary search is a very popular algorithm for finding a target element in a sorted array. Algorithm Here’s a standard way for implementing this algorithm: Time complexity: O(logn). Space complexity: O(1). However, this implementation will be problematic if we…

Leetcode 509: Fibonacci Number

The original question can be found here. The question is asking to return the Nth fibonacci number. F(0) = 0, F(1) = 1 and F(n) = F(n – 1) + F(n – 2). The easist solution is using recursion. If…

Recursion

Introduction Recursion is a common topic during coding interview. This post talks about how to succesfully write a correct recursion solution for a coding exercise. Method signature Method signature we care in a recursion method is defined by all the…

Quick Select

Introduction Quick select is an algorithm used to quickly locate the Kth largest/smallest element in an array. Technically speaking, it’s part of quick sort algorithm. Algorithm Let’s say we want to find the Kth largest element in an array. Every…

Leetcode 912: Sort an Array

The original question can be found here. It’s asking us to provide O(nlogn)solutions. Quick Sort The algorithm can be found here in my another post. Merge Sort The algorithm can be found here in my another post.

Merge Sort vs Quick Sort

Introduction Both merge sort and quick sort are popular sorting algorithms. They both use recursion and leverage devide and conquer principles. But what are their differences? Time Complexity Merge sort has time complexity as O(nlogn), where n is the size…

Merge Sort

Introduction Merge sort is another popular algorithm, with time complexity as O(nlogn). It’s leveraging the principle of divide and conquer, and this is definitely the algorithm we should master. Algorithm We recursively split the array into 2 parts, sort them…

Quick Sort

Introduction Quick sort is one of the most popular algorithms for sorting. It’s not uncommon to see this algorithm or other algorithms(e.g. partition) leveraging the principle of quick sort during interview. Algorithm The idea behind quick sort is divide and…

Leetcode 1: Two Sum

This is probably our very first coding preparation question. The original question can found here. This question is asking us to return the indices of 2 elements such that their sum equals to the target number. Brute Force The brute…

Leetcode 680: Valid Palindrome II

The original question can be found here. The question is asking if a string is palindromic if at most 1 character can be removed from string. Let’s look at an example: Let’s say we still use the 2 pointers apporach…

Leetcode 125: Valid Palindrome

The original question can be found here. The question is asking for a given string, determine if it’s palindromic. Only alphanumeric letters are taking into account and letters’ cases are ignored. Reverse and Compare The first solution we can think…

Leetcode 5: Longest Palindromic Substring

The original question can be found here. There’re multiple solutions to this question, and we’ll start with the brute force one. Brute Force We enumerate all substrings and check if it’s palindromic. If yes, we update the final result by…

Leetcode 349: Intersection of two arrays

The original question can be found here. The question is aksing for the intersection of two arrays, and element in final result should be unique. There’re multiple solutions for this question. Hash Set Load one array into a hashset, and…

Leetcode 88: Merge Sorted Array

The original question can be found here. It’s asking us to merge array nums2 into array nums1. The most straightforward solution is: for each element inside array nums2, we insert into nums1 to find an appropriate position. Once we find…

Leetcode 560: Subarray Sum Equals K

The orignal question can be found here. The question is asking the number of subarrays which the sum value equals to K. The brute force solution is very straight forward. We simply enumerate all the possible subarrays to find out…

Leetcode 53: Maximum Subarray

The original question can be found here. The question is aksing for the largest sum of the subarray. We can enumerate all possible subarrays and return the lagest sum. The time complexity is O(n^2)which is not quite acceptable. To find…

Leetcode 239: Sliding Window Maximum

The original question can be found here. The question is asking to find the maximum number inside each sliding window(subarray). Brute Force Go through each subarray and find the maximum value. The time complexity is O(n*k), where n is the…

Prefix Sum

Prefix sum is a very useful technique which can be used for some subarray questions. Let’s look at how it works. PrefixSum[i] stands for the sum of first i elements, which means the sum from array[0] to array[i – 1].…