Press "Enter" to skip to content

Posts published in August 2018

花花酱 LeetCode 189. Rotate Array

Problem

Given an array, rotate the array to the right byĀ kĀ steps, whereĀ kĀ is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3 
Output: [5,6,7,1,2,3,4]
Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] 
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input:[-1,-100,3,99] and k = 2 
Output: [3,99,-1,-100] 
Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

Solution 1: Simulate rotation with three reverses.

If k >= n, rotating k times has the same effect as rotating k % n times.

[1,2,3,4,5,6,7], K = 3

[5,6,7,1,2,3,4]

We can simulate the rotation with three reverses.

  1. reverse the whole array O(n) [7,6,5,4,3,2,1]
  2. reverse the left part 0 ~ k – 1 O(k) [5,6,7,4,3,2,1]
  3. reverse the right part k ~ n – 1 O(n-k)Ā [5,6,7,1,2,3,4]

Time complexity: O(n)

Space complexity: O(1) in-place

C++

 

花花酱 LeetCode 97. Interleaving String

Problem

GivenĀ s1,Ā s2,Ā s3, find whetherĀ s3Ā is formed by the interleaving ofĀ s1Ā andĀ s2.

Example 1:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true

Example 2:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false

Solution: DP

Subproblems : whether s3[0:i+j] can be formed by interleaving s1[0:i] and s2[0:j].

Time complexity: O(mn)

Space complexity: O(mn)

Recursion + Memorization

 

花花酱 LeetCode 882. Reachable Nodes In Subdivided Graph

Problem

Starting with anĀ undirectedĀ graph (the “original graph”) with nodes fromĀ 0Ā toĀ N-1, subdivisions are made to some of the edges.

The graph is given as follows:Ā edges[k]Ā is a list of integer pairsĀ (i, j, n)Ā such thatĀ (i, j)Ā is an edge of the original graph,

andĀ nĀ is the total number ofĀ newĀ nodes on that edge.

Then, the edgeĀ (i, j)Ā is deleted from the original graph,Ā nĀ new nodesĀ (x_1, x_2, ..., x_n)Ā are added to the original graph,

andĀ n+1Ā newĀ edgesĀ (i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j)Ā are added to the originalĀ graph.

Now, you start at nodeĀ 0Ā from the original graph, and in each move, you travel along oneĀ edge.

Return how many nodes you can reach in at mostĀ MĀ moves.

 

Example 1:

Input: edge = [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3 
Output: 13 
Explanation:  The nodes that are reachable in the final graph after M = 6 moves are indicated below. 

Example 2:

Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4 
Output: 23

Note:

  1. 0 <= edges.length <= 10000
  2. 0 <= edges[i][0] <Ā edges[i][1] < N
  3. There does not exist anyĀ i != jĀ for whichĀ edges[i][0] == edges[j][0]Ā andĀ edges[i][1] == edges[j][1].
  4. The original graphĀ has no parallel edges.
  5. 0 <= edges[i][2] <= 10000
  6. 0 <= M <= 10^9
  7. 1 <= N <= 3000

Solution: Dijkstra Shortest Path

Compute the shortest from 0 to rest of the nodes. Use HP to mark the maximum moves left to reach each node.

HP[u] = a, HP[v] = b, new_nodes[u][v] = c

nodes covered between a<->b = min(c, a + b)

Time complexity: O(ElogE)

Space complexity: O(E)

C++

Optimized Dijkstra (replace hashmap with vector)

Using SPFA

 

BFS

 

花花酱 LeetCode 885. Boats to Save People

Problem

TheĀ i-th person has weightĀ people[i], and each boat can carry a maximum weight ofĀ limit.

Each boat carries at most 2 people at the same time, provided the sum of theĀ weight of those people is at mostĀ limit.

Return the minimum number of boats to carry every given person.Ā  (It is guaranteed each person can be carried by a boat.)

Example 1:

Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)

Example 2:

Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)

Example 3:

Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)

Note:

  • 1 <=Ā people.length <= 50000
  • 1 <= people[i] <=Ā limit <= 30000

Solution: Greedy + Two Pointers

Time complexity: O(nlogn)

Space complexity: O(1)

Put one heaviest guy and put the lightest guy if not full.

 

花花酱 LeetCode 887. Projection Area of 3D Shapes

Problem

On aĀ NĀ *Ā NĀ grid, we place someĀ 1 * 1 * 1Ā cubes that are axis-aligned with the x, y, and z axes.

Each valueĀ v = grid[i][j]Ā represents a tower ofĀ vĀ cubes placed on top of grid cellĀ (i, j).

Now we view theĀ projectionĀ of these cubesĀ onto the xy, yz, and zx planes.

A projection is like a shadow, thatĀ maps our 3 dimensional figure to a 2 dimensional plane.

Here, we are viewing the “shadow” when looking at the cubes from the top, the front, and the side.

Return the total area of all three projections.

Example 1:

Input: [[2]]
Output: 5

Example 2:

Input: [[1,2],[3,4]]
Output: 17
Explanation: 
Here are the three projections ("shadows") of the shape made with each axis-aligned plane.

Example 3:

Input: [[1,0],[0,2]]
Output: 8

Example 4:

Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 14

Example 5:

Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 21

Note:

  • 1 <= grid.length = grid[0].lengthĀ <= 50
  • 0 <= grid[i][j] <= 50

Solution: Brute Force

Sum of max heights for each cols / rows + # of non-zero-height bars.

Time complexity: O(mn)

Space complexity: O(1)

C++