Analysis:
Using Reservior sampling. Continuously sampling from the list and find an element that met
rand(0, cnt) < k
Time Complexity:
- O(n)
Space Complexity:
- O(1)
Code
1 | import random |
A geek & a dreamer
Using Reservior sampling. Continuously sampling from the list and find an element that met
rand(0, cnt) < k
- O(n)
- O(1)
1 | import random |
there are two layers: first determine the base number from [1-9], then DFS through all combinations
- O(n)
- O(n)
1 | class Solution(object): |
- You should use recursion and treat every three nodes as a group
- Then you can find that the left most node must be the root, then we return it as parent node
- remember to reset root’s left node and right node to NULL
- O(n)
- O(n)
1 | class Solution(object): |
- find
j
in array in reverse order that nums[j]<nums[j+1]- find the first element that nums[i]>nums[j]
- swap the elements nums[i], nums[j]
- reverse nums[j+1:]
- O(n)
- O(1)
1 | class Solution(object): |
use a dict to store sum of range sum as key and index as value
we can check if the remaining in the dict. If so, find the largest gap of them.
- O(n)
- O(n)
1 | class Solution(object): |
The method to solve this problem, please refer to Fisher–Yates shuffle
- O(n)
- O(n)
1 | class Solution(object): |
use set in defaultdict to store the index
- O(1)
- O(n)
1 | class RandomizedCollection(object): |
define a dictionary to store the elements and a vector to store the index of them.
the key of removal is to move the tail element to the slot of removal element and update the tail element index.
- O(1)
- O(n)
1 | class RandomizedSet(object): |
define a array dp[i] to depict the possible combination of
i
, then we have
ifi+j <= target
thendp[i+j] += dp[i]
- O(n^2)
- O(n)
1 | class Solution(object): |
using a heap to store the sum numbers, using a two-dimensional array to mark the visited pairs,
also using a defaultdict to store the indexes.
- O(nlogn)
- O(n)
1 | class Solution(object): |
tag:
缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia根目录)执行以下命令:
npm i hexo-generator-json-content --save
3、在根目录_config.yml里添加配置:
jsonContent: meta: false pages: false posts: title: true date: true path: true text: false raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: true