Articles

  • 154 - Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates.

    Read More »

  • 153 - Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element.

    Read More »

  • 81 - Search in Rotated Sorted Array II

    Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array.

    Read More »

  • 33 - Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array.

    Read More »

  • 162 - Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] != num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that num[-1] = num[n] = -inf. For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

    Read More »

  • 74 - Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row.

    Read More »

  • 278 - First Bad Version

    The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version.

    Read More »

  • 34 - Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1].

    Read More »

  • 35 - Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume NO duplicates in the array.

    Read More »

  • Binary Search

    For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity. If the target number does not exist in the array, return -1.

    Read More »

  • 215 - Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, given [3,2,1,5,6,4] and k = 2, return 5.

    Read More »

  • Partition Array by Odd and Even

    Partition an integers array into odd number first and even number second.

    Read More »

  • Median of unsorted array

    Given a unsorted array with integers, find the median of it. A median is the middle number of the array after it is sorted. If there are even numbers in the array, return the N/2 th number after sorted.

    Read More »

  • Merge Sorted Array II

    Merge two given sorted integer array A and B into a new sorted integer array.

    Read More »

  • 88 - Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

    Read More »

  • 80 - Remove Duplicates from Sorted Array II

    Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?

    Read More »

  • 26 - Remove Duplicates from Sorted Array

    Given a sorted array, 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 in place with constant memory. For example, Given input array 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 new length.

    Read More »

  • 16 - 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

    Read More »

  • 18 - 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

    Read More »

  • 15 - 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

    Read More »

  • 41 - First Missing Positive

    Given an unsorted integer array, find the first missing positive integer.

    Read More »

  • 31 - Partition Array

    Given an array nums of integers and an int k, partition the array (i.e move the elements in “nums”) such that: All elements < k are moved to the leff. All elements >= k are moved to the right Return the partitioning index, i.e the first index i nums[i] >= k.

    Read More »

  • 238 - Product of Array Exclude Itself

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

    Read More »

  • Recover Rotated Sorted Array

    Given a rotated sorted array, recover it to sorted array in-place.

    Read More »

  • 139 - Subarray Sum Closest

    Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number.

    Read More »

  • 139 - Subarray Sum II

    Given an integer array, find a subarray where the sum of numbers is between two given interval. Your code should return the number of possible answer.

    Read More »

  • Subarray Sum K

    Given an nonnegative integer array, find a subarray where the sum of numbers is k. Your code should return the index of the first number and the index of the last number.

    Read More »

  • 138 - Subarray Sum

    Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.

    Read More »

  • 27 - Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

    Read More »

  • 38 - Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:

    Read More »