Press "Enter" to skip to content

Posts tagged as “array”

花花酱 LeetCode 228. Summary Ranges

Problem

Given a sorted integer array without duplicates, return the summary of its ranges.

Example 1:

Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range;Ā 4,5 form a continuous range.

Example 2:

Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range;Ā 8,9 form a continuous range.

Solution

Time complexity: O(n)

Space complexity: O(k)

C++

 

花花酱 LeetCode 849. Maximize Distance to Closest Person

Problem

In a row ofĀ seats,Ā 1Ā represents a person sitting in that seat, andĀ 0Ā represents that the seat is empty.

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.

Return that maximum distance to closest person.

Example 1:

Input: [1,0,0,0,1,0,1]
Output: 2
Explanation: 
If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.

Example 2:

Input: [1,0,0,0]
Output: 3
Explanation: 
If Alex sits in the last seat, the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.

Note:

  1. 1 <= seats.length <= 20000
  2. seatsĀ contains only 0s or 1s, at least oneĀ 0, and at least oneĀ 1.

Solution

Time complexity: O(n)

Space complexity: O(1)

 

花花酱 LeetCode 832. Flipping an Image

Problem

Given a binary matrixĀ A, we want to flip the image horizontally, then invert it, and return the resulting image.

To flip an image horizontally means that each row of the image is reversed.Ā  For example, flippingĀ [1, 1, 0]Ā horizontally results inĀ [0, 1, 1].

To invert an image meansĀ that eachĀ 0Ā is replaced byĀ 1, and eachĀ 1Ā is replaced byĀ 0.Ā For example, invertingĀ [0, 1, 1]Ā results inĀ [1, 0, 0].

Example 1:

Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]

Example 2:

Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

Notes:

  • 1 <= A.length = A[0].length <= 20
  • 0 <= A[i][j]Ā <=Ā 1

Solution 1: Brute Force

Time complexity: O(m*n)

Space complexity: O(m*n)

C++

 

花花酱 LeetCode 448. Find All Numbers Disappeared in an Array

Problem

Given an array of integers where 1 ā‰¤ a[i] ā‰¤Ā nĀ (nĀ = size of array), some elements appear twice and others appear once.

Find all the elements of [1,Ā n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]

Solution

Time complexity: O(n)

Space complexity: O(1)

C++

 

花花酱 LeetCode 396. Rotate Function

Problem:

Given an array of integersĀ AĀ and letĀ nĀ to be its length.

AssumeĀ BkĀ to be an array obtained by rotating the arrayĀ AĀ kĀ positions clock-wise, we define a “rotation function”Ā FĀ onĀ AĀ as follow:

F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].

Calculate the maximum value ofĀ F(0), F(1), ..., F(n-1).

Note:
nĀ is guaranteed to be less than 105.

Example:

A = [4, 3, 2, 6]

F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26

So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.

Solution 1: Brute Force

Time complexity: O(n^2) TLE

Space complexity: O(1)

Solution 2: Math

F(K)          =               0A[K] + 1A[K+1] + 2A[K+2] + ... + (n-2)A[K-2] + (n-1)A[K-1]
F(K-1)        =     0A[K-1] + 1A[K] + 2A[K+1] + 3A[K+2] + ... + (n-1)A[K-2]
F(K) - F(K-1) = (n-1)A[K-1] - 1A[K] - 1A[K+1] - 1A[K+2] - ... -     1A[K-2]
              = (n-1)A[K-1] - (1A[K] + 1A[K+1] + ... + 1A[K-2] + 1A[K-1] - 1A[K-1])
              = nA[K-1] - sum(A)
compute F(0)
F(i)          = F(i-1) + nA[i-1] - sum(A)

 

Time complexity: O(n)

Space complexity: O(1)