일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- high order function
- 스와이프
- fragment
- 고차함수
- espresso
- ActivityTestRule
- binding adapter
- searchview
- 안드로이드
- 코딜리티
- LayoutManger
- 안드로이드13
- Fragment 수동 추가
- 생명주기
- 뷰변경 감지
- ui test
- Android
- 테마 아이콘
- recyclerview
- Error:Execution failed for task ':app:mergeDebugResources'
- 구분선
- IntentTestRule
- 재사용
- 리사이클러뷰
- 안드로이드개발레벨업교과서
- 안드로이드스튜디오
- viewholder
- Fragment에서 Activity의 함수 사용하기
- adapter
- 코틀린
- Today
- Total
룬아님의 취중코딩
Codility 31번 CountSemiprimes 본문
CountSemiprimes
A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.
A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.
You are given two non-empty arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.
Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.
For example, consider an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20
The number of semiprimes within each of these ranges is as follows:
- (1, 26) is 10,
- (4, 10) is 4,
- (16, 20) is 0.
Write a function:
class Solution { public int[] solution(int N, int[] P, int[] Q); }
that, given an integer N and two non-empty arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.
For example, given an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20
the function should return the values [10, 4, 0], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- M is an integer within the range [1..30,000];
- each element of arrays P, Q is an integer within the range [1..N];
- P[i] ≤ Q[i].
class Solution {
public int[] solution(int N, int[] P, int[] Q) {
// write your code in Java SE 8
int[] array = new int[50001];
int[] flag = new int[50001];
array[1]=1;
int cnt = 0;
for(int i=2; i<array.length; i++){
for(int j=2; i*j<array.length; j++){
if(array[i] == 1){
break;
}
array[i*j]=1;
}
}
for(int i=2; i<array.length; i++){
for(int j=2; i*j<array.length; j++){
if(array[i] == 0 && array[j] == 0){
array[i*j]=2;
}
}
}
for(int i=0; i<array.length; i++){
if(array[i]==2){
cnt++;
}
flag[i] = cnt;
}
int[] ans = new int[P.length];
for(int i = 0; i<ans.length; i++){
ans[i]= flag[Q[i]] - flag[P[i]-1];
}
return ans;
}
}
'개발 > 알고리즘' 카테고리의 다른 글
Codility 33번 ChocolatesByNumbers (0) | 2019.10.29 |
---|---|
Codility 32번 CountNonDivisible (0) | 2019.10.20 |
Codility 29번 Peaks (0) | 2019.09.30 |
Codility 28번 MinPerimeterRectangle (0) | 2019.09.30 |
Codility 26번 MaxDoubleSliceSum (0) | 2019.09.25 |