Press "Enter" to skip to content

Posts tagged as “hashtable”

花花酱 LeetCode 916. Word Subsets

Problem

We are given two arraysĀ AĀ andĀ BĀ of words.Ā  Each word is a string of lowercase letters.

Now, say thatĀ wordĀ bĀ is a subset of wordĀ aĀ if every letter inĀ bĀ occurs inĀ a,Ā including multiplicity.Ā  For example,Ā "wrr"Ā is a subset ofĀ "warrior", but is not a subset ofĀ "world".

Now say a wordĀ aĀ fromĀ AĀ isĀ universalĀ if for everyĀ bĀ inĀ B,Ā bĀ is a subset ofĀ a.

Return a list of all universal words inĀ A.Ā  You can return the words in any order.

Example 1:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
Output: ["facebook","google","leetcode"]

Example 2:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
Output: ["apple","google","leetcode"]

Example 3:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
Output: ["facebook","google"]

Example 4:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
Output: ["google","leetcode"]

Example 5:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["ec","oc","ceo"]
Output: ["facebook","leetcode"]

Note:

  1. 1 <= A.length, B.length <= 10000
  2. 1 <= A[i].length, B[i].lengthĀ <= 10
  3. A[i]Ā andĀ B[i]Ā consist only of lowercase letters.
  4. All words inĀ A[i]Ā are unique: there isn’tĀ i != jĀ withĀ A[i] == A[j].

Solution: Hashtable

Find the max requirement for each letter from B.

Time complexity: O(|A|+|B|)

Space complexity: O(26)

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 904. Fruit Into Baskets

Problem

In a row of trees, theĀ i-th treeĀ producesĀ fruit with typeĀ tree[i].

YouĀ start at any treeĀ of your choice, then repeatedly perform the following steps:

  1. Add one piece of fruit from this tree to your baskets.Ā  If you cannot, stop.
  2. Move to the next tree to the right of the current tree.Ā  If there is no tree to the right, stop.

Note that you do not have any choice after the initial choice of starting tree:Ā you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop.

You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each.

What is the total amount of fruit you can collect with this procedure?

Example 1:

Input: [1,2,1]
Output: 3
Explanation: We can collect [1,2,1].

Example 2:

Input: [0,1,2,2]
Output: 3
Explanation: We can collect [1,2,2].
If we started at the first tree, we would only collect [0, 1].

Example 3:

Input: [1,2,3,2,2]
Output: 4
Explanation: We can collect [2,3,2,2].
If we started at the first tree, we would only collect [1, 2].

Example 4:

Input: [3,3,3,1,2,1,1,2,3,3,4]
Output: 5
Explanation: We can collect [1,2,1,1,2].
If we started at the first tree or the eighth tree, we would only collect 4 fruits.

Note:

  1. 1 <= tree.length <= 40000
  2. 0 <= tree[i] < tree.length

Solution: Hashtable + Sliding window

Time complexity: O(n)

Space complexity: O(1)

Keep track of the last index of each element. If a third type of fruit comes in, the new window starts after the fruit with smaller last index. Otherwise extend the current window.

[1 3 1 3 1 1] 4 1 4 … <- org window, 3 has a smaller last index than 1.

1 3 1 3 [1 1 4] 1 4 … <- new window

C++

花花酱 LeetCode 893. Groups of Special-Equivalent Strings

Problem

You are given an arrayĀ AĀ of strings.

Two stringsĀ SĀ andĀ TĀ areĀ special-equivalentĀ if after any number ofĀ moves, S == T.

AĀ moveĀ consists of choosing two indicesĀ iĀ andĀ jĀ withĀ i % 2 == j % 2, and swappingĀ S[i]Ā withĀ S[j].

Now, aĀ group of special-equivalent strings fromĀ AĀ is aĀ non-empty subset S ofĀ AĀ such that any string not in SĀ is not special-equivalent with any string in S.

Return the number of groups of special-equivalent strings fromĀ A.

Example 1:

Input: ["a","b","c","a","c","c"]
Output: 3
Explanation: 3 groups ["a","a"], ["b"], ["c","c","c"]

Example 2:

Input: ["aa","bb","ab","ba"]
Output: 4
Explanation: 4 groups ["aa"], ["bb"], ["ab"], ["ba"]

Example 3:

Input: ["abc","acb","bac","bca","cab","cba"]
Output: 3
Explanation: 3 groups ["abc","cba"], ["acb","bca"], ["bac","cab"]

Example 4:

Input: ["abcd","cdab","adcb","cbad"]
Output: 1
Explanation: 1 group ["abcd","cdab","adcb","cbad"]

Note:

  • 1 <= A.length <= 1000
  • 1 <= A[i].length <= 20
  • AllĀ A[i]Ā have the same length.
  • AllĀ A[i]Ā consist of only lowercase letters.

Ā 

Solution: Signature

All Special-Equivalent Strings should have the same signature if we sort all the odd-index characters and all the even-index characters.

E.g.Ā [“abcd”,”cdab”,”adcb”,”cbad”] are all in the same graph.

“abcd”: odd “ac”, even: “bd”
“cdab”: odd “ac”, even: “bd”
“adcb”: odd “ac”, even: “bd”
“cbad”: odd “ac”, even: “bd”

We can concatenate the odd and even strings to create a hashable signature.

Time complexity: O(n)

Space complexity: O(n)

C++

Java

Python

Python (1-linear)

花花酱 LeetCode 888. Fair Candy Swap

Problem

Alice and Bob have candy bars of different sizes:Ā A[i]Ā is the size of theĀ i-th bar of candy that Alice has, andĀ B[j]Ā is the size of theĀ j-th bar of candy that Bob has.

Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same totalĀ amount of candy.Ā  (The total amount of candyĀ a person has is the sum of the sizes of candyĀ bars they have.)

Return an integer arrayĀ ansĀ whereĀ ans[0]Ā is the size of the candy bar that Alice must exchange, andĀ ans[1]Ā is the size of the candy bar that Bob must exchange.

If there are multiple answers, you may return any one of them.Ā  It is guaranteed an answer exists.

Example 1:

Input: A = [1,1], B = [2,2]
Output: [1,2]

Example 2:

Input: A = [1,2], B = [2,3]
Output: [1,2]

Example 3:

Input: A = [2], B = [1,3]
Output: [2,3]

Example 4:

Input: A = [1,2,5], B = [2,4]
Output: [5,4]

Note:

  • 1 <= A.length <= 10000
  • 1 <= B.length <= 10000
  • 1 <= A[i] <= 100000
  • 1 <= B[i] <= 100000
  • It is guaranteed that Alice and Bob have different total amounts ofĀ candy.
  • It is guaranteed there exists anĀ answer.

 

Solution: HashTable

Time complexity: O(A+B)

Space complexity: O(B)

Clean version

Faster version