This repository was archived by the owner on Jun 19, 2020. It is now read-only.
File tree 4 files changed +163
-0
lines changed
hojin/Algorithm/codility/lesson05
4 files changed +163
-0
lines changed Original file line number Diff line number Diff line change
1
+ - [ CountDiv] ( https:app.codility.com/programmers/lessons/5-prefix_sums/count_div/ )
2
+
3
+ ## 접근 방법
4
+ # 첫번 째 방법
5
+ 1. end와 start사이에 k가 몇번 들어가는지 구하자
6
+ 2. 그리고 start와 end가 k로 나누어 떨어진다면 +1씩 해주자.
7
+
8
+ # 두번 째 방법
9
+ 1. 0부터 end까지 k의 몇배수 까지 들어갈 수 있는지 구하자
10
+ 2. 0부터 start까지 k의 몇배수까지 들어갈 수 있는지 구하자
11
+ 3. start가 k의 배수일때는 +1를 추가한다, 이유 : 문제 제시가 start를 포함한 범위부터 구하기를 제시하기 때문
12
+ 4. (1)-(2)를 해주자
13
+
14
+ ## 소스코드
15
+
16
+ ~~~ java
17
+ public int solution(int A , int B , int K ) {
18
+
19
+ return this . test(A , B , K );
20
+ }
21
+ public int test(int start, int end, int k){
22
+ int temp = end/ k - start/ k;
23
+ if (start% k== 0 ){
24
+ temp+= 1 ;
25
+ }
26
+ return temp;
27
+ }
28
+ ~~~
29
+
30
+ ## 개선사항
31
+
32
+ ## 시간복잡도를 주링기 위해 생각한 점, 반영한 점
33
+
34
+ ## 채점 결과
35
+ | Task Score | Correctness | Performance |
36
+ | ------------ | ------------- | ------------- |
37
+ | 100% | 100% | 100% |
Original file line number Diff line number Diff line change
1
+ - [ GenomicRangeQuery] ( https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/ )
2
+
3
+ ## 접근 방법
4
+
5
+
6
+ # 첫번 째 방법
7
+ 1. 비교를 쉽게 하기 위해서 String[]을 int[]로 변환 한다.
8
+ 2 . 변환시 for문을 최대한 적게 돌기 위하여 탐색에 필요한 부분만 변환한다.
9
+ 3 . p값과 q값에서 최소 값을 찾는다.
10
+ # 두번 째 방법
11
+
12
+ ## 소스코드
13
+
14
+ ~~~ java
15
+ public int [] solution(String s, int [] p, int [] q){
16
+ int intS[] = new int [s. length()];
17
+ int result[] = new int [p. length];
18
+
19
+ for (int i= 0 ;i< p. length;i++ ){
20
+ result[i] = this . getMinOfArray(s, p[i], q[i]);
21
+ // System.out.println(result[i]);
22
+ }
23
+ return result;
24
+ }
25
+
26
+ public int parserStringToInt(char str){
27
+ switch (str){
28
+ case ' A' : return 1 ;
29
+ case ' C' : return 2 ;
30
+ case ' G' : return 3 ;
31
+ case ' T' : return 4 ;
32
+ default : System . out. println(" 잘못된 문자열 값이 들어왔습니다." );
33
+ return - 1 ;
34
+ }
35
+ }
36
+ public int getMinOfArray(String str, int start, int end){
37
+ int min = 5 ;
38
+ int parserInt = 0 ;
39
+ for (int i= start;i<= end;i++ ){
40
+ parserInt = this . parserStringToInt(str. charAt(i));
41
+ if (min > parserInt){
42
+ min = parserInt;
43
+ if (parserInt == 1 ){
44
+ return 1 ;
45
+ }
46
+ }
47
+ }
48
+ return min;
49
+ }
50
+ ~~~
51
+
52
+ ## 개선필요한 사항
53
+ 1 . String[ ] ->int[ ] parser를 미리 하는게 좋을 것 같다. ( 중복되서 parser를 하는 듯 하다. )
54
+ 2 . 시간복잡도가 N* M이므로 줄여야 된다.
55
+
56
+
57
+ ## 시간복잡도를 주링기 위해 생각한 점, 반영한 점
58
+ 1 . 탐색해야되는 길이(M)만큼 for문을 돌면 안되고, 바로 값을 알 수 있는 연산이 필요하다. 즉, 2~ 4라고 가정했을 때 탐색없이 답을 찾아야 한다.
59
+
60
+ ## 채점 결과
61
+ | Task Score | Correctness | Performance |
62
+ | ------------ | ------------- | ------------- |
63
+ | 62% | 100% | 0% |
Original file line number Diff line number Diff line change
1
+ - [ PassingCars] ( https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/ )
2
+
3
+ ## 접근 방법
4
+ # 첫번 째 방법
5
+ 1. 입력 배열값 중 값이 0인 인덱스를 값으로 저장되는 배열을 만든다 int zero[]
6
+ 2. 입력 배열값 중 값이 1인 인덱스를 값으로 저장되는 배열을 만든다 int one[]
7
+ 3. 이슈사항
8
+ 1. (P,Q)를 쌍으로 지정할 때 P < Q라는 조건이 있다
9
+ 2. 이를 해결하기 위해서 (1),(2)를 작업함
10
+ 3. if(zero[i] > one[j]) 일 경우 탐색을 종료하고 i++를 진행 함 (반복문). ( 각 배열의 값은 입력 배열의 인덱스를 저장하고 있다. )
11
+
12
+
13
+ # 두번 째 방법
14
+ 첫번째 방법은 2중 for문이 들어가기 때문에 개선이 필요함
15
+ ` P < Q 해결하기 `
16
+ 1 . 한 쌍의 기준을 (P,Q)로 정한다
17
+ 1. P는 값이 0인 입력 배열의 인덱스
18
+ 2. Q는 값이 1인 입력 배열의 인덱스
19
+ 2 . Q의 개수를 n개로 정한다.
20
+ 1. 현재 P의 인덱스보다 작은 Q값을 다 빼야 된다. ( P < Q 조건 때문 ) --> n = n-1
21
+
22
+ ## 소스코드
23
+
24
+ ~~~ java
25
+ public int solution(int [] A ) {
26
+ // write your code in Java SE 8
27
+ return this . test(A );
28
+ }
29
+
30
+ public int test(int [] a){
31
+ int zeroCnt = 0 ;
32
+ int oneCnt = 0 ;
33
+ int total = 0 ;
34
+ for (int i= 0 ; i< a. length;i++ ){
35
+ if (a[i]== 0 ){
36
+ zeroCnt++ ;
37
+ }else {
38
+ oneCnt++ ;
39
+ }
40
+ }
41
+ for (int i= 0 ;i< a. length;i++ ){
42
+ if (total > 1000000000 ){
43
+ return - 1 ;
44
+ }
45
+ if (a[i]== 0 ){
46
+ total += oneCnt;
47
+ }else {
48
+ oneCnt-- ;
49
+ }
50
+ }
51
+ // System.out.println(total);
52
+ return total;
53
+ }
54
+ ~~~
55
+
56
+ ## 개선사항
57
+
58
+ ## 시간복잡도를 주링기 위해 생각한 점, 반영한 점
59
+
60
+ ## 채점 결과
61
+ | Task Score | Correctness | Performance |
62
+ | ------------ | ------------- | ------------- |
63
+ | 100% | 100% | 100% |
You can’t perform that action at this time.
0 commit comments