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 from arr
- a < b
- b - a equals to the minimum absolute difference of any two elements in arr
1 | Input: arr = [3,8,-10,23,19,-4,-14,27] |
1 | class Solution { |
Ugly Number III
Write a program to find the n-th ugly number.
Ugly numbers are positive integers which are divisible by a or b or c.
容斥加二分
1 | class Solution { |
Smallest String With Swaps
You are given a string s, and an array of pairs of indices in the string pairs where pairs[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 | class Solution { |
1203. Sort Items by Groups Respecting Dependencies
拓扑排序
There are n items each belonging to zero or one of m groups where group[i] is the group that the i-th item belongs to and it’s equal to -1 if the i-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.
Return a sorted list of the items such that:
- The items that belong to the same group are next to each other in the sorted list.
- There are some relations between these items where beforeItems[i] is a list containing all the items that should come before the i-th item in the sorted array (to the left of the i-th item).
Return any solution if there is more than one solution and return an empty list if there is no solution.
- 第一步,构造小组的依赖关系和组内项目的依赖关系。
- 第二步,判断小组间是否有冲突,没冲突计算出小组的拓扑排序。
- 第三步:判断每个小组内项目是否有冲突,没冲突计算出小组内项目的拓扑排序。
1 | class Solution { |