Press "Enter" to skip to content

Posts published in September 2018

花花酱 LeetCode 909. Snakes and Ladders

Problem

On an N x NĀ board, the numbers fromĀ 1Ā toĀ N*NĀ are writtenĀ boustrophedonicallyĀ starting from the bottomĀ left of the board, and alternating direction each row.Ā  For example, for a 6 x 6 board, the numbers are written as follows:

You start on squareĀ 1Ā of the board (which is always in the last row andĀ first column).Ā  Each move, starting from squareĀ x, consists of the following:

  • You choose a destination squareĀ SĀ with numberĀ x+1,Ā x+2,Ā x+3,Ā x+4,Ā x+5, orĀ x+6, provided thisĀ number isĀ <=Ā N*N.
    • (This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations.)
  • IfĀ SĀ has a snake or ladder, you move to the destination of that snake or ladder.Ā  Otherwise, you move toĀ S.

A board square on rowĀ rĀ and columnĀ cĀ has a “snake or ladder” ifĀ board[r][c] != -1.Ā  The destination of that snake or ladder isĀ board[r][c].

Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of anotherĀ snake or ladder, you doĀ notĀ continue moving.Ā  (For example, if the board is [[4,-1],[-1,3]], and on the first move your destination square is 2, then you finish your first move atĀ 3, because you doĀ notcontinue moving to 4.)

Return the least number of moves required to reach squareĀ N*N.Ā  If it is not possible, returnĀ -1.

Example 1:

Input: [
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,35,-1,-1,13,-1],
[-1,-1,-1,-1,-1,-1],
[-1,15,-1,-1,-1,-1]]
Output: 4
Explanation: 
At the beginning, you start at square 1 [at row 5, column 0].
You decide to move to square 2, and must take the ladder to square 15.
You then decide to move to square 17 (row 3, column 5), and must take the snake to square 13.
You then decide to move to square 14, and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
It can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4.

Note:

  1. 2 <= board.length = board[0].lengthĀ <= 20
  2. board[i][j]Ā is betweenĀ 1Ā andĀ N*NĀ or is equal toĀ -1.
  3. The boardĀ square with numberĀ 1Ā has no snake or ladder.
  4. The board square with numberĀ N*NĀ has no snake or ladder.

Solution: BFS

Time complexity: O(n*n)

Space complexity: O(n*n)

C++

花花酱 LeetCode 911. Online Election

Problem

n an election, theĀ i-thĀ vote was cast forĀ persons[i]Ā at timeĀ times[i].

Now, we would like to implement the following query function:Ā TopVotedCandidate.q(int t)Ā will return the number of the person that was leading the election at timeĀ t.

Votes cast at timeĀ tĀ will count towards our query.Ā  In the case of a tie, the most recent vote (among tied candidates) wins.

Example 1:

Input: ["TopVotedCandidate","q","q","q","q","q","q"], [[[0,1,1,0,0,1,0],[0,5,10,15,20,25,30]],[3],[12],[25],[15],[24],[8]]
Output: [null,0,1,1,0,0,1]
Explanation: 
At time 3, the votes are [0], and 0 is leading.
At time 12, the votes are [0,1,1], and 1 is leading.
At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
This continues for 3 more queries at time 15, 24, and 8.

Note:

  1. 1 <= persons.length = times.length <= 5000
  2. 0 <= persons[i] <= persons.length
  3. timesĀ is a strictly increasing array with all elements inĀ [0, 10^9].
  4. TopVotedCandidate.qĀ is called at mostĀ 10000Ā times per test case.
  5. TopVotedCandidate.q(int t)Ā is always called withĀ t >= times[0].

Solution: HashTable + Binary Search

Compute the leads for each t in times using a hash table.

binary search the upper bound of t, and return the lead of previous entry.

Time complexity: Constructor O(n), Query: O(logn)

Space complexity: O(n)

C++

花花酱 LeetCode 908. Smallest Range I

Problem

Given an arrayĀ AĀ of integers, for each integerĀ A[i]Ā we may choose anyĀ xĀ withĀ -K <= x <= K, and addĀ xtoĀ A[i].

After this process, we have some arrayĀ B.

Return the smallest possible difference between the maximum value ofĀ BĀ and the minimum value ofĀ B.

Example 1:

Input: A = [1], K = 0
Output: 0
Explanation: B = [1]

Example 2:

Input: A = [0,10], K = 2
Output: 6
Explanation: B = [2,8]

Example 3:

Input: A = [1,3,6], K = 3
Output: 0
Explanation: B = [3,3,3] or B = [4,4,4]

Note:

  1. 1 <= A.length <= 10000
  2. 0 <= A[i] <= 10000
  3. 0 <= K <= 10000

Solution 0: Brute Force (TLE)

Try all pairs

Time complexity: O(n^2)

Space complexity: O(1)

Solution 1: Math

Time complexity: O(n)

Space complexity: O(1)

Find the min/max element of the array.

min + k v.s. max – k

ans = max(0, (max – min) – 2 * k))

C++

Python3

花花酱 LeetCode 19. Remove Nth Node From End of List

Problem

Given a linked list, remove theĀ n-th node from the end of list and return its head.

Example:

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:

GivenĀ nĀ will always be valid.

Follow up:

Could you do this in one pass?

Solution 0: Cheating! store the nodes in an array

C++

Solution 1: Two passes

Time complexity: O(L)

Space complexity: O(1)

C++

Solution 2: Fast/Slow Pointers + Dummy Head / Prev

Fast pointer moves n steps first, and then slow pointer starts moving.

When fast pointer reaches tail, slow pointer is n-th node from the end.

Time complexity: O(L)

Space complexity: O(1)

C++

Java

Python3

花花酱 LeetCode 18. 4Sum

Problem

Given an arrayĀ numsĀ ofĀ nĀ integers and an integerĀ target, are there elementsĀ a,Ā b,Ā c, andĀ dĀ inĀ numsĀ such thatĀ aĀ +Ā bĀ +Ā cĀ +Ā dĀ =Ā target? Find all unique quadruplets in the array which gives the sum ofĀ target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

Solution 1: Sorting + Binary Search

Time complexity: O(n^3 log n + klogk)

Space complexity: O(k)

C++

C++ opt

Solution 2: Sorting + HashTable

Time complexity: O(n^3 + klogk)

Space complexity: O(n + k)

C++

Related Problems