Press "Enter" to skip to content

Posts published in June 2018

花花酱 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 289. Game of Life

Problem

According to theĀ Wikipedia’s article: “TheĀ Game of Life, also known simply asĀ Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”

Given aĀ boardĀ withĀ mĀ byĀ nĀ cells, each cell has an initial stateĀ liveĀ (1) orĀ deadĀ (0). Each cell interacts with itsĀ eight neighborsĀ (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Write a function to compute the next state (after one update) of the board given its current state.Ā The next state is created by applying the above rules simultaneously to every cell in the current state, whereĀ births and deaths occur simultaneously.

Example:

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

Follow up:

  1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
  2. In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?

Solution: Simulation

Time complexity: O(mn)

Space complexity: O(1)

 

花花酱 LeetCode 856. Score of Parentheses

Problem

Given a balanced parentheses stringĀ S, compute the score of the string based on the following rule:

  • ()Ā has score 1
  • ABĀ has scoreĀ A + B, where A and B are balanced parentheses strings.
  • (A)Ā has scoreĀ 2 * A, where A is a balanced parentheses string.

 

Example 1:

Input: "()"
Output: 1

Example 2:

Input: "(())"
Output: 2

Example 3:

Input: "()()"
Output: 2

Example 4:

Input: "(()(()))"
Output: 6

Solution1: Recursion

Time complexity: O(n^2)

Space complexity: O(n)

 

Solution2: Counting

Time complexity: O(n)

Space complexity: O(1)

C++

 

花čƝ酱 LeetCode 859. Buddy Strings

Problem

Given two stringsĀ AĀ andĀ BĀ of lowercase letters, returnĀ trueĀ if and only if weĀ can swap two letters inĀ AĀ so that the result equalsĀ B.

 

Example 1:

Input: A = "ab", B = "ba"
Output: true

Example 2:

Input: A = "ab", B = "ab"
Output: false

Example 3:

Input: A = "aa", B = "aa"
Output: true

Example 4:

Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true

Example 5:

Input: A = "", B = "aa"
Output: false

Note:

  1. 0 <= A.length <= 20000
  2. 0 <= B.length <= 20000
  3. AĀ andĀ BĀ consist only of lowercase letters.

Solution: HashTable

Time complexity: O(n)

Space complexity: O(26)

 

花花酱 LeetCode 452. Minimum Number of Arrows to Burst Balloons

Problem

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104Ā balloons.

An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstartĀ and xendbursts by an arrow shot at x if xstartĀ ā‰¤ x ā‰¤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.

Example:

Input:
[[10,16], [2,8], [1,6], [7,12]]

Output:
2

Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).

Solution: Sweep Line

Time complexity: O(nlogn)

Space complexity: O(1)

C++

Related Problems