Press "Enter" to skip to content

Posts published in “Algorithms”

花花酱 LeetCode 18. 4Sum

Problem

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

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

Solution 1: Sorting + Binary Search

Time complexity: O(n^3 log n + klogk)

Space complexity: O(k)

C++

C++ opt

Solution 2: Sorting + HashTable

Time complexity: O(n^3 + klogk)

Space complexity: O(n + k)

C++

Related Problems

花花酱 LeetCode 905. Sort Array By Parity

Problem

Given an arrayĀ AĀ of non-negative integers, return an array consisting of all the even elements ofĀ A, followed by all the odd elements ofĀ A.

You may return any answer array that satisfies this condition.

Example 1:

Note:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

Solution 1: Split Odd/Even

Time complexity: O(n)

Space complexity: O(n)

C++

Solution 2: Stable sort by key % 2

Time complexity: O(nlogn)

Space complexity: O(1) in-place

C++

花花酱 LeetCode 668. Kth Smallest Number in Multiplication Table

Problem

Nearly every one have used theĀ Multiplication Table. But could you find out theĀ k-thĀ smallest number quickly from the multiplication table?

Given the heightĀ mĀ and the lengthĀ nĀ of aĀ m * nĀ Multiplication Table, and a positive integerĀ k, you need to return theĀ k-thĀ smallest number in this table.

Example 1:

Input: m = 3, n = 3, k = 5
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6
3	6	9

The 5-th smallest number is 3 (1, 2, 2, 3, 3).

Example 2:

Input: m = 2, n = 3, k = 6
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6

The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).

Note:

  1. TheĀ mĀ andĀ nĀ will be in the range [1, 30000].
  2. TheĀ kĀ will be in the range [1, m * n]

Ā 

Solution 1: Nth element (MLE)

Time complexity: O(mn)

Space complexity: O(mn)

C++

Solution2 : Binary Search

Find first x such that there are k elements less or equal to x in the table.

Time complexity: O(m*log(m*n))

Space complexity: O(1)

C++

Java

Python3

RelatedĀ Problems

花花酱 LeetCode 896. Monotonic Array

An array isĀ monotonicĀ if it is either monotone increasing or monotone decreasing.

An arrayĀ AĀ is monotone increasing if for allĀ i <= j,Ā A[i] <= A[j].Ā  An arrayĀ AĀ is monotone decreasing if for allĀ i <= j,Ā A[i] >= A[j].

ReturnĀ trueĀ if and only if the given arrayĀ AĀ is monotonic.

Solution:Ā 

C++

Java

Python

花花酱 LeetCode 566. Reshape the Matrix

Problem

In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.

You’re given a matrix represented by a two-dimensional array, and twoĀ positiveĀ integersĀ rĀ andĀ cĀ representing theĀ rowĀ number andĀ columnĀ number of the wanted reshaped matrix, respectively.

The reshaped matrix need to be filled with all the elements of the original matrix in the sameĀ row-traversingĀ order as they were.

If the ‘reshape’ operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

Example 1:

Input: 
nums = 
[[1,2],
 [3,4]]
r = 1, c = 4
Output: 
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

Example 2:

Input: 
nums = 
[[1,2],
 [3,4]]
r = 2, c = 4
Output: 
[[1,2],
 [3,4]]
Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

Note:

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.

Solution1: Brute Force

Time complexity: O(mn)

Space complexity: O(mn)