JAVA_LeetCode 398_Random Pick Index 풀이 class Solution { int[] nums; Random rand = new Random(); public Solution(int[] nums) { this.nums = nums; } public int pick(int target) { int count = 0, result = 0; // 값이 target과 일치할 경우 그만큼 체크한다음, 랜덤으로 찾는다. for(int i = 0; i < nums.length; i++){ if(nums[i] == target){ count++; if(rand.nextInt(count) == 0) result = i; } } return result; } } random, Reservoir Sampling(저수지 샘플링) 반환될 값의 균등성(1/cnt)를 보장하는 방법 if문의 조건을 rand.nextInt(count) == 0으로 한 이유 **개 이상부터 체크하기 위해 ...