[LeetCode] 382. Linked List Random Node_Medium tag: linked list, math
2021/8/23 9:28:45
本文主要是介绍[LeetCode] 382. Linked List Random Node_Medium tag: linked list, math,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.
Implement the Solution
class:
Solution(ListNode head)
Initializes the object with the integer array nums.int getRandom()
Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be choosen.
Example 1:
Input ["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"] [[[1, 2, 3]], [], [], [], [], []] Output [null, 1, 3, 2, 2, 3] Explanation Solution solution = new Solution([1, 2, 3]); solution.getRandom(); // return 1 solution.getRandom(); // return 3 solution.getRandom(); // return 2 solution.getRandom(); // return 2 solution.getRandom(); // return 3 // getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
Constraints:
- The number of nodes in the linked list will be in the range
[1, 104]
. -104 <= Node.val <= 104
- At most
104
calls will be made togetRandom
.
Ideas:
1. T: O(n) S: O(n) 直接将list 转换为array,然后利用random.random()* len(arr)得到random 的index, 最后返回相应的数值即可
random.random() => random float in range [0, 1]
random.randint(start, end) both include
Code
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def __init__(self, head: Optional[ListNode]): """ @param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. """ self.range = [] self.n = 0 while head: self.range.append(head.val) self.n += 1 head = head.next def getRandom(self) -> int: """ Returns a random node's value. """ pick = int(random.random() * self.n) return self.range[pick]
2. Follow up , what if the list is very big and its length is unknown?
Use "reservoir sampling" (随机抽样,并且概率相同)
1. 如果list 很大, 那么我们先从前i个里面选一个概率为1/ i
2. 然后再换这个node的概率为1/ (i + 1),也就是说保持这个node的概率为 ( 1 - 1/ (i + 1)) = i/(i + 1)
3. 然后每次index + 1, 这样的话概率为 1/ (i + 2), 保持这个node的概率为 ( 1 - 1/ (i + 2)) = i + 1/(i + 2)
4. 在n 次之后, 就是1/ i * (i / i + 1) * (i + 1/ i + 2) .... n - 1/ n = 1/ n
5. 也就是说每个node最后保持到最后的概率为1/ n
Code
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def __init__(self, head: Optional[ListNode]): """ @param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. """ self.head = head def getRandom(self) -> int: """ Returns a random node's value. """ result, node, index = self.head, self.head.next, 1 while node: if random.randint(0, index) == 0: result = node node = node.next index += 1 return result.val
这篇关于[LeetCode] 382. Linked List Random Node_Medium tag: linked list, math的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-30用PydanticAI和Gemini 2.0构建Airflow的AI助手
- 2024-12-30阿里云ECS教程:新手入门必读
- 2024-12-30使用vxe-table的插槽时页面卡顿怎么优化?-icode9专业技术文章分享
- 2024-12-30在 Kotlin 中使用 Coil 怎么实现高斯模糊效果?-icode9专业技术文章分享
- 2024-12-30有哪些常见的方法和工具查看和分析域名访问量?-icode9专业技术文章分享
- 2024-12-30aar 文件和jar 文件的区别是什么?-icode9专业技术文章分享
- 2024-12-30Gradle引用依赖 annotationProcessor 和implementation 的区别是什么?-icode9专业技术文章分享
- 2024-12-30packaging 类型 jar.sha256 和 jar的区别是什么?-icode9专业技术文章分享
- 2024-12-30.aar 是什么文件?-icode9专业技术文章分享
- 2024-12-30Build > Build Bundle(s) / APK(s) 的作用是什么?-icode9专业技术文章分享