일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- recyclerview
- 안드로이드개발레벨업교과서
- high order function
- 고차함수
- 리사이클러뷰
- LayoutManger
- 구분선
- Fragment에서 Activity의 함수 사용하기
- 생명주기
- Error:Execution failed for task ':app:mergeDebugResources'
- 코틀린
- 재사용
- ui test
- ActivityTestRule
- fragment
- espresso
- binding adapter
- 테마 아이콘
- viewholder
- 코딜리티
- 스와이프
- Fragment 수동 추가
- 안드로이드
- adapter
- 안드로이드스튜디오
- IntentTestRule
- 뷰변경 감지
- searchview
- 안드로이드13
- Android
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