일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 재사용
- searchview
- recyclerview
- 안드로이드
- 안드로이드스튜디오
- 뷰변경 감지
- 안드로이드13
- high order function
- Android
- 코틀린
- LayoutManger
- ActivityTestRule
- fragment
- 테마 아이콘
- espresso
- ui test
- Error:Execution failed for task ':app:mergeDebugResources'
- viewholder
- 스와이프
- 코딜리티
- 구분선
- 고차함수
- 안드로이드개발레벨업교과서
- Fragment 수동 추가
- adapter
- binding adapter
- 리사이클러뷰
- Fragment에서 Activity의 함수 사용하기
- IntentTestRule
- 생명주기
Archives
- Today
- Total
룬아님의 취중코딩
codility 14번 CountDiv 본문
CountDiv
Write a function:
class Solution { public int solution(int A, int B, int K); }
that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.:
{ i : A ≤ i ≤ B, i mod K = 0 }
For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10.
Write an efficient algorithm for the following assumptions:
- A and B are integers within the range [0..2,000,000,000];
- K is an integer within the range [1..2,000,000,000];
- A ≤ B.
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 K) {
// write your code in Java SE 8
int cnt = 0;
for(int i=A; i<=B; i++){
if(i%K == 0){
cnt++;
cnt += (B - i) / K;
break;
}
}
return cnt;
}
}
class Solution {
public int solution(int A, int B, int K) {
return B/K - A/K + (A%K == 0 ? 1: 0);
}
}
반응형
'개발 > 알고리즘' 카테고리의 다른 글
Codility 16번 Triangle (0) | 2019.09.01 |
---|---|
Codility 13번 MinAvgTwoSlice (0) | 2019.08.28 |
Codlility 15번 Distinct (0) | 2019.08.26 |
Codility 10번 PassingCars (0) | 2019.08.17 |
Codility 12번 MaxProductOfThree (0) | 2019.08.17 |
Comments