[LeetCode] 2336. Smallest Number in Infinite Set
Posted CNoodle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 2336. Smallest Number in Infinite Set相关的知识,希望对你有一定的参考价值。
You have a set which contains all positive integers [1, 2, 3, 4, 5, ...]
.
Implement the SmallestInfiniteSet
class:
SmallestInfiniteSet()
Initializes the SmallestInfiniteSet object to contain all positive integers.int popSmallest()
Removes and returns the smallest integer contained in the infinite set.void addBack(int num)
Adds a positive integernum
back into the infinite set, if it is not already in the infinite set.
Example 1:
Input ["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"] [[], [2], [], [], [], [1], [], [], []] Output [null, null, 1, 2, 3, null, 1, 4, 5] Explanation SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet(); smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change is made. smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set. smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set. smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set. smallestInfiniteSet.addBack(1); // 1 is added back to the set. smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and // is the smallest number, and remove it from the set. smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set. smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
Constraints:
1 <= num <= 1000
- At most
1000
calls will be made in total topopSmallest
andaddBack
.
无限集中的最小数字。
现有一个包含所有正整数的集合 [1, 2, 3, 4, 5, ...] 。
实现 SmallestInfiniteSet 类:
SmallestInfiniteSet() 初始化 SmallestInfiniteSet 对象以包含 所有 正整数。
int popSmallest() 移除 并返回该无限集中的最小整数。
void addBack(int num) 如果正整数 num 不 存在于无限集中,则将一个 num 添加 到该无限集中。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/smallest-number-in-infinite-set
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是最小堆 + hashset。初始化的时候,我们将 1 - 1000 这些数字都分别放入最小堆和 hashset 中。
pop() 很好判断,就是从最小堆中弹出一个元素即可,同时在 hashset 中也移除这个元素。
addBack() 需要判断最小堆中是否已经存在这个数字了,如果不存在才加回去。注意 testcase 中对于 add 操作,给的数字不一定是已经弹出的元素。
时间O(nlogn)
空间O(n)
Java实现
class SmallestInfiniteSet PriorityQueue<Integer> queue; Set<Integer> set; public SmallestInfiniteSet() queue = new PriorityQueue<>(); set = new HashSet<>(); for (int i = 1; i <= 1000; i++) queue.offer(i); set.add(i); public int popSmallest() if (!queue.isEmpty()) set.remove(queue.peek()); return queue.poll(); return -1; public void addBack(int num) if (!set.contains(num)) set.add(num); queue.offer(num); /** * Your SmallestInfiniteSet object will be instantiated and called as such: * SmallestInfiniteSet obj = new SmallestInfiniteSet(); * int param_1 = obj.popSmallest(); * obj.addBack(num); */
Leetcode 230. Kth Smallest Element in a BST
题目链接
https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
题目描述
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST‘s total elements.
Example 1:
Input: root = [3,1,4,null,2], k = 1
3
/ 1 4
2
Output: 1
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/ 3 6
/ 2 4
/
1
Output: 3
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
题解
最直接的方法就是中序遍历,得到有序数组,然后取得第k个值;
也可以使用二分法,因为平衡二叉树以根节点分为左右两部分,我们可以统计左边节点的个数,然后与k值相比较,如果大于k值,说明第k个值在左子树上,否则在当前节点或者右子树。递归遍历即可。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int kthSmallest(TreeNode root, int k) {
int count = countNodes(root.left);
if (k <= count) {
return kthSmallest(root.left, k);
} else if (k > count + 1) {
return kthSmallest(root.right, k - 1 - count);
}
return root.val;
}
public int countNodes(TreeNode n) {
if (n == null) { return 0; }
return 1 + countNodes(n.left) + countNodes(n.right);
}
}
以上是关于[LeetCode] 2336. Smallest Number in Infinite Set的主要内容,如果未能解决你的问题,请参考以下文章
908. Smallest Range I - LeetCode
Leetcode 230. Kth Smallest Element in a BST
Leetcode 230. Kth Smallest Element in a BST
LeetCode Find K Pairs with Smallest Sums