Press "Enter" to skip to content

Huahua's Tech Road

花花酱 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

花花酱 LeetCode 852. Peak Index in a Mountain Array

Problem

Let’s call an arrayĀ AĀ aĀ mountainĀ if the following properties hold:

  • A.length >= 3
  • There exists someĀ 0 < iĀ < A.length - 1Ā such thatĀ A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]

Given an array that is definitely a mountain, return anyĀ iĀ such thatĀ A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].

Example 1:

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

Example 2:

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

Note:

  1. 3 <= A.length <= 10000
  2. 0 <= A[i] <= 10^6
  3. AĀ is a mountain, as defined above.

Solution1: Liner Scan

Time complexity: O(n)

Space complexity: O(1)

C++

C++/STL

Solution 2: Binary Search

Find the smallest l such that A[l] > A[l + 1].

Time complexity: O(logn)

Space complexity: O(1)

C++

Java

Python3

花花酱 LeetCode 848. Shifting Letters

Problem

We have a stringĀ SĀ of lowercase letters, and an integer arrayĀ shifts.

Call theĀ shiftĀ of a letter, the next letter in the alphabet, (wrapping around so thatĀ 'z'Ā becomesĀ 'a').

For example,Ā shift('a') = 'b',Ā shift('t') = 'u', andĀ shift('z') = 'a'.

Now for eachĀ shifts[i] = x, we want to shift the firstĀ i+1Ā letters ofĀ S,Ā xĀ times.

Return the final stringĀ after all such shifts toĀ SĀ are applied.

Example 1:

Input: S = "abc", shifts = [3,5,9]
Output: "rpl"
Explanation: 
We start with "abc".
After shifting the first 1 letters of S by 3, we have "dbc".
After shifting the first 2 letters of S by 5, we have "igc".
After shifting the first 3 letters of S by 9, we have "rpl", the answer.

Note:

  1. 1 <= S.length = shifts.length <= 20000
  2. 0 <= shifts[i] <= 10 ^ 9

Solution

Time complexity: O(n)

Space complexity: O(1)

C++