https://app.codility.com/programmers/lessons/5-prefix_sums/count_div/
CountDiv coding task - Learn to Code - Codility
Compute number of integers divisible by k in range [a..b].
app.codility.com
내가 처음에 제출한 코드는 아래와 같다.
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
let cnt = 0;
for (let i = A ; i <= B ; i++) {
if (i % K === 0) cnt++;
}
return cnt;
}
스코어는 50을 기록했다. 정확성은 100이었으나, time complexity는 O(B-A)였다.
두 번째로 제출한 코드는,
https://poltman.tistory.com/80
[codility] 풀어보기 - CountDiv
Lesson 5-2 - CountDiv 내용 : Compute number of integers divisible by k in range [a..b]. 시간 복잡도 : O(1) 정답 코드 : 1 2 3 4 5 6 7 8 9 10 11 int solution(int A, int B, int K) { double a = A; doubl..
poltman.tistory.com
이 분의 코드를 참조했다.
function solution(A, B, K) {
// write your code in JavaScript (Node.js 8.9.4)
return Math.floor(B/K) - Math.floor((A - 1)/K);
}
time complexity는 O(1)을 기록했다.
'코딩 테스트 > Codility' 카테고리의 다른 글
Codility GenomicRangeQuery JavaScript 풀이 (0) | 2020.03.30 |
---|---|
Codility PermCheck JavaScript 풀이 (0) | 2020.03.30 |
Codility MissingInteger JavaScript 풀이 (0) | 2020.03.30 |
Codility MaxCounters JavaScript 풀이 (0) | 2020.03.29 |
Codility FrogRiverOne JavaScript 풀이 (0) | 2020.03.29 |