random select
3427 단어 select
random select
problem:
select m number from 0 - (n-1) randomly,
------
solution 1
for each number in 0 - (n-1), in increasing order, generate a random number x, using "(x%remain) < to_select"to decide will the number be choose,
until m number is choosen,
time:
o(n), when n is small, this is quick, but when n is very big, this will be very slow,
------
solution 2
use set to store choosen number, until enough,
time:
o(m*log(m))
space:
o(m)
disadvangate:
when n & m is quite closer, and n is big, this might be slow,
when m is big, will need a lot memory,
------
solution 3
init a n length array in increasing order,
mix the order of first m elements in original array,
then sort the first m elements,
then use the first m elements,
time:
o(n+m*log(m))
space:
o(n)
------
solution 4
base on solution 3,
don't init a n length array, only use a map to store first 5 m elements, and the elements that has been choose for swap with,
time:
o(m*log(m))
space:
o(m)
------
reverse select
if m*2 > n, we can choose to select n-m elements, and exclude them, the remain is m elements,
------
code:
/* @authur [email protected] */
/**
* problem:
select m number from 0 - (n-1) randomly,
*/
#include <stdio.h>
#include <stdlib.h>
/**
* solution 1:
for each number in 0 - (n-1), in increasing order, generate a random number x,
using "(x%remain) < to_select" to decide will the number be choose,until m number is choosen,
* time:
O(n), when n is small, this is quick, but when n is very big, this will be very slow,
*/
void random_m_n(const int m, const int n) {
int remain = n,to_select = m,i=0;
while(to_select>0) {
if(rand()%remain<to_select) {
printf("%d,",i);
to_select--;
}
remain--;
i++;
}
printf("
");
}
/**
* swap 2 int value,by pointer,
*/
void swap(int *a,int *b) {
int c = *a;
*a = *b;
*b = c;
}
/**
* compare function for int
*/
int int_cmp(int *a,int *b) {
return *a-*b;
}
/**
* solution 2:
init a n length array in increasing order,
mix the order of first m elements in original array,
then sort the first m elements,
then use the first m elements,
* time:
o(n+m*log(m))
* space:
o(n)
*/
void random_m_n_2(const int m, const int n) {
int arr[n],i,swp,x;
for(i=0;i<n;i++) {
arr[i] = i;
}
for(i=0;i<m;i++) {
x = rand()%n;
swap(arr+i,arr+x);
}
qsort(arr,m,4,int_cmp);
for(i=0;i<m;i++) {
printf("%d,",arr[i]);
}
printf("
");
}
int main(){
random_m_n(30,2000);
random_m_n(0,0);
random_m_n(20,20);
random_m_n_2(30,2000);
random_m_n_2(0,0);
random_m_n_2(20,20);
}
------
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
🕗[프로그래머스] 입양 시각 구하기(2)문제 설명 ANIMAL_OUTS 테이블은 동물 보호소에서 입양 보낸 동물의 정보를 담은 테이블입니다. ANIMAL_OUTS 테이블 구조는 다음과 같으며, ANIMAL_ID, ANIMAL_TYPE, DATETIME, ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.