Skip to main content
350, Intersection of Two Arrays II

I Problem

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order .

Example 1
Input: nums1 = [1, 2, 2, 1], nums2 = [2, 2]
Output: [2, 2]


MikeAbout 2 minhashtableeasyarrayhash tablebinary searchsortingtwo pointers
349, Intersection of Two Arrays

I Problem

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

Example 1
Input: nums1 = [1, 2, 2, 1], nums2 = [2, 2]
Output: [2]
Explanation:


MikeAbout 1 minhashtableeasyarrayhash tablebinary searchsortingtwo pointers
209, Minimum Size Subarray Sum

I Problem

Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Example 1:
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.


MikeAbout 2 minarraymediumarraybinary searchsliding windowprefix sum
704, Binary Search

I Problem

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums.
If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4


MikeAbout 2 minarrayeasyarraybinary search
367, Valid Perfect Square

I Problem

Given a positive integer num, return true if num is a perfect square or false otherwise.

A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.

You must not use any built-in library function, such as sqrt.


MikeAbout 1 minarrayeasymathbinary search
69, Sqrt(x)

I Problem

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

You must not use any built-in exponent function or operator.

For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.


MikeAbout 2 minarrayeasymathbinary search
34, Find First and Last Position of Element in Sorted Array

I Problem

Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

If target is not found in the array, return [-1, -1].

You must write an algorithm with O(log n) runtime complexity.

Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]


MikeAbout 1 minarraymediumarraybinary search
35, Search Insert Position

I Problem

Given a sorted array of distinct integers 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 must write an algorithm with O(log n) runtime complexity.

Example 1:


MikeAbout 1 minarrayeasyarraybinary search