일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Android
- 스와이프
- 코딜리티
- searchview
- adapter
- 리사이클러뷰
- 안드로이드
- 안드로이드13
- viewholder
- 재사용
- fragment
- Fragment에서 Activity의 함수 사용하기
- recyclerview
- ActivityTestRule
- Fragment 수동 추가
- LayoutManger
- 뷰변경 감지
- 구분선
- 안드로이드개발레벨업교과서
- 생명주기
- IntentTestRule
- espresso
- 테마 아이콘
- 안드로이드스튜디오
- ui test
- Error:Execution failed for task ':app:mergeDebugResources'
- 코틀린
- high order function
- binding adapter
- 고차함수
- Today
- Total
룬아님의 취중코딩
Codility 34번 CommonPrimeDivisors 본문
CommonPrimeDivisors
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 prime D is called a prime divisor of a positive integer P if there exists a positive integer K such that D * K = P. For example, 2 and 5 are prime divisors of 20.
You are given two positive integers N and M. The goal is to check whether the sets of prime divisors of integers N and M are exactly the same.
For example, given:
- N = 15 and M = 75, the prime divisors are the same: {3, 5};
- N = 10 and M = 30, the prime divisors aren't the same: {2, 5} is not equal to {2, 3, 5};
- N = 9 and M = 5, the prime divisors aren't the same: {3} is not equal to {5}.
Write a function:
class Solution { public int solution(int[] A, int[] B); }
that, given two non-empty arrays A and B of Z integers, returns the number of positions K for which the prime divisors of A[K] and B[K] are exactly the same.
For example, given:
A[0] = 15 B[0] = 75 A[1] = 10 B[1] = 30 A[2] = 3 B[2] = 5
the function should return 1, because only one pair (15, 75) has the same set of prime divisors.
Write an efficient algorithm for the following assumptions:
- Z is an integer within the range [1..6,000];
- each element of arrays A, B is an integer within the range [1..2,147,483,647].
Copyright 2009–2019 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
class Solution {
public int solution(int[] A, int[] B) {
int ans = 0;
for(int i=0; i<A.length; i++){
if(A[i] == B[i]){
ans++;
}else{
int gcd = gcd(B[i], A[i]);
if(gcd!=1){
int k;
if(A[i] > B[i]){
k = A[i]/gcd;
}else{
k = B[i]/gcd;
}
if(k>=gcd){
if(k%gcd == 0){
ans++;
}
}else{
if(gcd%k == 0){
ans++;
}
}
}
}
}
return ans;
}
int gcd(int a, int b){
if(b == 0){
return a;
}
return gcd(b, a%b);
}
}
'개발 > 알고리즘' 카테고리의 다른 글
직사각형 나머지 좌표 구하기 (0) | 2020.03.11 |
---|---|
Codility 33번 ChocolatesByNumbers (0) | 2019.10.29 |
Codility 32번 CountNonDivisible (0) | 2019.10.20 |
Codility 31번 CountSemiprimes (0) | 2019.10.19 |
Codility 29번 Peaks (0) | 2019.09.30 |