Weekly Contest 155
1200. Minimum Absolute Difference
Given an array of distinct integers
arr
, find all pairs of elements with the minimum absolute difference of any two elements.Return a list of pairs in ascending order(with respect to pairs), each pair
[a, b]
follows
a, b
are fromarr
a < b
b - a
equals to the minimum absolute difference of any two elements inarr
1
2
3 Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
1 | class Solution: |
1201. Ugly Number III
Write a program to find the
n
-th ugly number.Ugly numbers are positive integers which are divisible by
a
orb
orc
.
1
2
3 >Input: n = 3, a = 2, b = 3, c = 5
>Output: 4
>Explanation: The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 3rd is 4.
There are x/a number can be divisible by a, x/b number can be divisible by b and x/c number can be divisible by c.
However, in such case, if one number can be divisible by both (a,b), we count it twice. We have to subtract those cases from our total count.Lucky enough, if a number can be divisible by both (a,b), it must can be divisible by lcm(a,b). So we need to calucalte least common multiple between (a,b) (b,c) (a,c) and (a,b,c).
Then we know, there are x/lcm(a,b) number can be divisible by both (a,b), x/lcm(b,c) number can be divisible by both (b,c, and x/lcm(a,c) number can be divisible by both (a,c).
However, after subtracting them, we don’t count number which can be divisible by (a,b,c) together, to fix it, we simply need to add x/lcm(a,lcm(b,c)).
1 | class Solution { |
1202. Smallest String With Swaps
You are given a string
s
, and an array of pairs of indices in the stringpairs
wherepairs[i] = [a, b]
indicates 2 indices(0-indexed) of the string.You can swap the characters at any pair of indices in the given
pairs
any number of times.Return the lexicographically smallest string that
s
can be changed to after using the swaps
1
2
3
4
5 >Input: s = "dcab", pairs = [[0,3],[1,2]]
>Output: "bacd"
>Explaination:
>Swap s[0] and s[3], s = "bcad"
>Swap s[1] and s[2], s = "bacd"
1 <= s.length <= 10^5
0 <= pairs.length <= 10^5
0 <= pairs[i][0], pairs[i][1] < s.length
s
only contains lower case English letters.
-
他的意思是我们可以使用并查集来做,对所用同一集合内的元素我们直接排序便是。然后按次序从每个集合中拿出来排序最小字符,最终得到最小字符串。
The core of the idea is that if (0, 1) is an exchange pair and (0, 2) is an exchange pair, then any 2 in (0, 1, 2) can be exchanged.
This implies, we can build connected components where each component is a list of indices that can be exchanged with any of them. In Union find terms, we simply iterate through each pair, and do a union on the indices in the pair.
At the end of the union of all the pairs, we have built connected component of indices that can be exchanged with each other.
1 | import collections |