Press "Enter" to skip to content

Posts published in July 2018

花花酱 LeetCode 704. Binary Search

Problem

Given aĀ sortedĀ (in ascending order) integer arrayĀ numsĀ ofĀ nĀ elements and aĀ targetĀ value, write a function to searchĀ targetĀ inĀ nums. IfĀ targetĀ exists, then return its index, otherwise returnĀ -1.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1

 

Note:

  1. You may assume that all elements inĀ numsĀ are unique.
  2. nĀ will be in the rangeĀ [1, 10000].
  3. The value of each element inĀ numsĀ will be in the rangeĀ [-9999, 9999].

Solution: Binary Search

Time complexity: O(logn)

Space complexity: O(1)

STL

 

花花酱 LeetCode 709. To Lower Case

Problem

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Solution

Time complexity: O(n)

Space complexity: O(1)

C++

Java

Python3

Python3 1-linear

花花酱 LeetCode 775. Global and Local Inversions

Problem

We have some permutationĀ AĀ ofĀ [0, 1, ..., N - 1], whereĀ NĀ is the length ofĀ A.

The number of (global) inversions is the number ofĀ i < jĀ withĀ 0 <= i < j < NĀ andĀ A[i] > A[j].

The number of local inversions is the number ofĀ iĀ withĀ 0 <= i < NĀ andĀ A[i] > A[i+1].

ReturnĀ trueĀ if and only if the number of global inversions is equal to the number of local inversions.

Example 1:

Input: A = [1,0,2]
Output: true
Explanation: There is 1 global inversion, and 1 local inversion.

Example 2:

Input: A = [1,2,0]
Output: false
Explanation: There are 2 global inversions, and 1 local inversion.

Note:

  • AĀ will be a permutation ofĀ [0, 1, ..., A.length - 1].
  • AĀ will have length in rangeĀ [1, 5000].
  • The time limit for this problem has been reduced.

Solution1: Brute Force (TLE)

Time complexity: O(n^2)

Space complexity: O(1)

C++

 

Solution2: MergeSort

Time complexity: O(nlogn)

Space complexity: O(n)

C#

 

Solution3: Input Property

Input is a permutation of [0, 1, …, N – 1]

Time Complexity: O(n)

Space Complexity: O(1)

 

 

花花酱 LeetCode 864. Shortest Path to Get All Keys

Problem

We are given a 2-dimensionalĀ grid.Ā "."Ā is an empty cell,Ā "#"Ā isĀ a wall,Ā "@"Ā is the starting point, ("a",Ā "b", …) are keys, and ("A",Ā "B", …) are locks.

We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.Ā  We cannot walk outside the grid, or walk into a wall.Ā  If we walk over a key, we pick it up.Ā  We can’t walk over a lock unless we have the corresponding key.

For someĀ 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the firstĀ KĀ letters of the English alphabet in the grid.Ā  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks wereĀ chosen in the same order as the English alphabet.

Return the lowest number of moves to acquire all keys.Ā  IfĀ it’s impossible, returnĀ -1.

Example 1:

Input: ["@.a.#","###.#","b.A.B"]
Output: 8

Example 2:

Input: ["@..aA","..B#.","....b"]
Output: 6

Note:

  1. 1 <= grid.lengthĀ <= 30
  2. 1 <= grid[0].lengthĀ <= 30
  3. grid[i][j]Ā contains onlyĀ '.',Ā '#',Ā '@',Ā 'a'-'f'Ā andĀ 'A'-'F'
  4. The number of keys is inĀ [1, 6].Ā  Each key has a different letter and opens exactly one lock.

Solution: BFS

Time complexity: O(m*n*64)

Space complexity: O(m*n*64)

C++

Python3

Related Problems

 

花花酱 LeetCode 839. Similar String Groups

Problem

Two stringsĀ XĀ andĀ YĀ are similar if we can swap two letters (in different positions) ofĀ X, so thatĀ it equalsĀ Y.

For example,Ā "tars"Ā andĀ "rats"Ā are similar (swapping at positionsĀ 0Ā andĀ 2), andĀ "rats"Ā andĀ "arts"Ā are similar, butĀ "star"Ā is not similar toĀ "tars",Ā "rats", orĀ "arts".

Together, these form two connected groups by similarity:Ā {"tars", "rats", "arts"}Ā andĀ {"star"}.Ā  Notice thatĀ "tars"Ā andĀ "arts"Ā are in the same group even though they are not similar.Ā  Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.

We are given a listĀ AĀ of strings.Ā  Every string inĀ AĀ is an anagram of every other string inĀ A.Ā  How many groups are there?

Example 1:

Input: ["tars","rats","arts","star"]
Output: 2

Note:

  1. A.length <= 2000
  2. A[i].length <= 1000
  3. A.length * A[i].length <= 20000
  4. All words inĀ AĀ consist of lowercase letters only.
  5. All words inĀ AĀ have the same length and are anagrams of each other.
  6. The judging time limit has been increased for this question.

Solution: Brute Force + Union Find

Time Complexity: O(n^2 * L)

Space Complexity: O(n)

C++