Press "Enter" to skip to content

Posts published in December 2017

花花酱 LeetCode 743. Network Delay Time

There areĀ NĀ network nodes, labelledĀ 1Ā toĀ N.

GivenĀ times, a list of travel times asĀ directedĀ edgesĀ times[i] = (u, v, w), whereĀ uĀ is the source node,Ā vĀ is the target node, andĀ wĀ is the time it takes for a signal to travel from source to target.

Now, we send a signal from a certain nodeĀ K. How long will it take for all nodes to receive the signal? If it is impossible, returnĀ -1.

Note:

  1. NĀ will be in the rangeĀ [1, 100].
  2. KĀ will be in the rangeĀ [1, N].
  3. The length ofĀ timesĀ will be in the rangeĀ [1, 6000].
  4. All edgesĀ times[i] = (u, v, w)Ā will haveĀ 1 <= u, v <= NĀ andĀ 1 <= w <= 100.

Idea:

Construct the graph and do a shortest path from K to all other nodes.

Solution 2:

C++ / Bellman-Ford

Time complexity: O(ne)

Space complexity: O(n)

 

Solution3:

C++ / Floyd-Warshall

Time complexity: O(n^3)

Space complexity: O(n^2)

v2

花花酱 LeetCode 745. Prefix and Suffix Search

Link:Ā https://leetcode.com/problems/prefix-and-suffix-search/description/

Problem:

Given manyĀ words,Ā words[i]Ā has weightĀ i.

Design a classĀ WordFilterĀ that supports one function,Ā WordFilter.f(String prefix, String suffix). It will return the word with givenĀ prefixĀ andĀ suffixĀ with maximum weight. If no word exists, return -1.

Examples:

Note:

  1. wordsĀ has length in rangeĀ [1, 15000].
  2. For each test case, up toĀ words.lengthĀ queriesĀ WordFilter.fĀ may be made.
  3. words[i]Ā has length in rangeĀ [1, 10].
  4. prefix, suffixĀ have lengths in rangeĀ [0, 10].
  5. words[i]Ā andĀ prefix, suffixĀ queries consist of lowercase letters only.

Idea:

Construct all possible filters

 

Solution1:

C++

Time complexity: O(NL^3 + QL)Ā  where N is the number of words, L is the max length of the word, Q is the number of queries.

Space complexity: O(NL^3)

Version #2

Solution 2:

C++ / Trie

Time complexity: O(NL^2 + QL)Ā  where N is the number of words, L is the max length of the word, Q is the number of queries.

Space complexity: O(NL^2)

Related Problems:

花花酱 LeetCode 744. Find Smallest Letter Greater Than Target

Problem:

Given a list of sorted charactersĀ lettersĀ containing only lowercase letters, and given a target letterĀ target, find the smallest element in the list that is larger than the given target.

Letters also wrap around. For example, if the target isĀ target = 'z'Ā andĀ letters = ['a', 'b'], the answer isĀ 'a'.

Examples:

Note:

  1. lettersĀ has a length in rangeĀ [2, 10000].
  2. lettersĀ consists of lowercase letters, and contains at least 2 unique letters.
  3. targetĀ is a lowercase letter.

Link:Ā https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/Ā 

Idea:

  1. Scan the array Time complexity: O(n)
  2. Binary searchĀ Time complexity: O(logn)

Solution 1:

C++ / Scan

C++ / Set

C++ / Binary Search

 

花花酱 LeetCode 164. Maximum Gap

https://leetcode.com/problems/maximum-gap/description/

Problem:

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.

Try to solve it in linear time/space.

Return 0 if the array contains less than 2 elements.

You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.

题ē›®å¤§ę„:

ē»™ä½ äø€äøŖę²”ęœ‰ęŽ’åŗēš„ę­£ę•“ꕰꕰē»„ć€‚č¾“å‡ŗꎒåŗåŽļ¼Œē›ø邻元ē“ ēš„å·®ēš„ęœ€å¤§å€¼(Max Gap)ć€‚éœ€č¦åœØēŗæę€§ę—¶é—“å†…č§£å†³ć€‚

Example:

Input: Ā [5, 0, 4, 2, 12, 10]

Output: 5

Explanation:Ā 

Sorted: [0, 2, 4, 5, 10, 12]

max gap is 10 – 5 = 5

Idea:

Bucket sort. Use n buckets to store all the numbers. For each bucket, only track the min / max value.

ꔶꎒåŗć€‚ē”ØnäøŖę”¶ę„å­˜ę”¾ę•°å­—ć€‚åƹäŗŽęƏäøŖꔶļ¼ŒåŖč·ŸčøŖ存å‚Øęœ€å¤§å€¼å’Œęœ€å°å€¼ć€‚

max gap must come from two “adjacent” buckets, b[i], b[j], j > i, b[i+1] … b[j – 1] must be empty.

max gap åŖåÆčƒ½ę„č‡Ŗ”ē›ø邻”ēš„äø¤äøŖꔶ b[i] 和 b[j], j > i, b[i] 和 b[j] 之闓ēš„ꔶļ¼ˆå¦‚ęžœęœ‰ļ¼‰åæ…é”»äøŗē©ŗ怂

max gap = b[j].min – b[i].min

Time complexity: O(n)

ę—¶é—“å¤ę‚åŗ¦: O(n)

Space complexity: O(n)

ē©ŗé—“å¤ę‚åŗ¦: O(n)

Solution:

C++

 

花花酱 LeetCode 530. Minimum Absolute Difference in BST

Link

Problem:

Given a binary search tree with non-negative values, find the minimumĀ absolute differenceĀ between values of any two nodes.

Example:

Note:Ā There are at least two nodes in this BST.


Idea:

Sorting via inorder traversal gives us sorted values, compare current one with previous one to reduce space complexity from O(n) to O(h).

Solution:

C++ O(n) space

C++ O(h) space

Java

Python

Related Problems:

  • [č§£é¢˜ęŠ„å‘Š] LeetCode 98. Validate Binary Search Tree