Press "Enter" to skip to content

Posts published in June 2018

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

花花酱 LeetCode 849. Maximize Distance to Closest Person

Problem

In a row ofĀ seats,Ā 1Ā represents a person sitting in that seat, andĀ 0Ā represents that the seat is empty.

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.

Return that maximum distance to closest person.

Example 1:

Input: [1,0,0,0,1,0,1]
Output: 2
Explanation: 
If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.

Example 2:

Input: [1,0,0,0]
Output: 3
Explanation: 
If Alex sits in the last seat, the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.

Note:

  1. 1 <= seats.length <= 20000
  2. seatsĀ contains only 0s or 1s, at least oneĀ 0, and at least oneĀ 1.

Solution

Time complexity: O(n)

Space complexity: O(1)

 

花花酱 LeetCode 847. Shortest Path Visiting All Nodes

Problem

题ē›®å¤§ę„ļ¼šę±‚锶ē‚¹č¦†ē›–ēš„ęœ€ēŸ­č·Æå¾„ć€‚

https://leetcode.com/problems/shortest-path-visiting-all-nodes/description/

An undirected, connected graph of N nodes (labeledĀ 0, 1, 2, ..., N-1) is given asĀ graph.

graph.length = N, andĀ j != iĀ is in the listĀ graph[i]Ā exactly once, if and only if nodesĀ iĀ andĀ jĀ are connected.

Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.

Example 1:

Input: [[1,2,3],[0],[0],[0]]
Output: 4
Explanation: One possible path is [1,0,2,0,3]

Example 2:

Input: [[1],[0,2,4],[1,3,4],[2],[1,2]]
Output: 4
Explanation: One possible path is [0,1,4,2,3]

Solution: BFS

Time complexity: O(n*2^n)

Space complexity: O(n*2^n)

C++

C++ / vector

Related Problems

花花酱 LeetCode 846. Hand of Straights

Problem

题ē›®å¤§ę„ļ¼šē»™ä½ äø€äŗ›ē‰Œļ¼Œé—®ä½ čƒ½å¦åˆ†ē»„ļ¼Œč¦ę±‚ęƏē»„wå¼ čæžē»­ēš„ē‰Œć€‚

https://leetcode.com/problems/hand-of-straights/description/

Alice has aĀ handĀ of cards, given as an array of integers.

Now she wants to rearrange the cards into groups so that each group is sizeĀ W, and consists ofĀ WĀ consecutive cards.

ReturnĀ trueĀ if and only if she can.

Example 1:

Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].

Example 2:

Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.

 

Note:

  1. 1 <= hand.length <= 10000
  2. 0 <= hand[i]Ā <= 10^9
  3. 1 <= W <= hand.length

Solution: Greedy

Time complexity: O(nlogn)

Space complexity: O(n)