Press "Enter" to skip to content

Posts published in October 2018

花花酱 LeetCode 470. Implement Rand10() Using Rand7()

Problem

Given a functionĀ rand7Ā which generates a uniform random integer in the range 1 to 7, write a functionĀ rand10Ā which generates a uniform random integer in the range 1 to 10.

Do NOT use system’sĀ Math.random().

Example 1:

Input: 1
Output: [7]

Example 2:

Input: 2
Output: [8,4]

Example 3:

Input: 3
Output: [8,1,10]

Note:

  1. rand7Ā is predefined.
  2. Each testcase has one argument:Ā n, the number of times thatĀ rand10Ā is called.

Follow up:

  1. What is theĀ expected valueĀ for the number of calls toĀ rand7()Ā function?
  2. Could you minimize the number of calls toĀ rand7()?

Solution:

Time complexity: O(1)

Space complexity: O(1)

C++

花花酱 LeetCode 922. Sort Array By Parity II

Problem

Given an arrayĀ AĀ of non-negative integers, half of the integers in A are odd, and half of the integers are even.

Sort the array so that wheneverĀ A[i]Ā is odd,Ā iĀ is odd; and wheneverĀ A[i]Ā is even,Ā iĀ is even.

You may return any answer array that satisfies this condition.

Example 1:

Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

Note:

  1. 2 <= A.length <= 20000
  2. A.length % 2 == 0
  3. 0 <= A[i] <= 1000

Solution 1: Brute Force

Time complexity: O(n)

Space complexity: O(n)

C++

花花酱 LeetCode 921. Minimum Add to Make Parentheses Valid

Given a stringĀ SĀ ofĀ '('Ā andĀ ')'Ā parentheses, we add the minimum number of parentheses (Ā '('Ā orĀ ')', and in any positions ) so that the resulting parentheses string is valid.

Formally, a parentheses string is valid if and only if:

  • It is the empty string, or
  • It can be written asĀ ABĀ (AĀ concatenated withĀ B), whereĀ AĀ andĀ BĀ are valid strings, or
  • It can be written asĀ (A), whereĀ AĀ is a valid string.

Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid.

 

Example 1:

Input: "())"
Output: 1

Example 2:

Input: "((("
Output: 3

Example 3:

Input: "()"
Output: 0

Example 4:

Input: "()))(("
Output: 4

Note:

  1. S.length <= 1000
  2. SĀ only consists ofĀ '('Ā andĀ ')'Ā characters.

Solution: Counting

Time complexity: O(n)

Space complexity: O(1)

C++

花花酱 LeetCode 923. 3Sum With Multiplicity

Problem

Given an integer arrayĀ A, and an integerĀ target, return the number ofĀ tuplesĀ i, j, kĀ  such thatĀ i < j < kĀ andĀ A[i] + A[j] + A[k] == target.

As the answer can be very large, return it moduloĀ 10^9 + 7.

Example 1:

Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation: 
Enumerating by the values (A[i], A[j], A[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.

Example 2:

Input: A = [1,1,2,2,2,2], target = 5
Output: 12
Explanation: 
A[i] = 1, A[j] = A[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.

Note:

  1. 3 <= A.length <= 3000
  2. 0 <= A[i] <= 100
  3. 0 <= target <= 300

Solution: Math / Combination

Time complexity: O(n + |target|^2)

Space complexity: O(|target|)

C++

花花酱 LeetCode 924. Minimize Malware Spread

Problem

In a network of nodes, each nodeĀ iĀ is directly connected to another nodeĀ jĀ if and only ifĀ graph[i][j] = 1.

Some nodesĀ initialĀ are initially infected by malware.Ā  Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware.Ā  This spread of malware will continue until no more nodes can be infected in this manner.

SupposeĀ M(initial)Ā is the final number of nodes infected with malware in the entire network, after the spread of malware stops.

We willĀ remove one node from the initial list.Ā  Return the node that if removed, would minimizeĀ M(initial).Ā  If multiple nodes could be removed to minimizeĀ M(initial), return such a node with the smallest index.

Note that if a node was removed from theĀ initialĀ list of infected nodes, it may still be infected later as a result of the malware spread.

 

Example 1:

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0

Example 2:

Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
Output: 0

Example 3:

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
Output: 1

Note:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 0 <= initial[i] < graph.length

Solution: BFS

Time complexity: O(n^3)

Space complexity: O(n^2)

C++