Category solutions

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…

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…

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.

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…