codliity 13

[codility] 풀어보기 - PassingCars

Lesson 5-2 - PassingCars내용 : Count the number of passing cars on the road. 시간 복잡도 : O(n)정답 코드 :1234567891011121314function solution(A) { // write your code in JavaScript (Node.js 6.4.0) let rt=0; let eastcnt=0; for(i in A) { if(A[i] == 0) { eastcnt ++; } else { rt += eastcnt; } if(rt > 1000000000) { rt = -1; break; } } return rt;Colored by Color Scriptercs 동쪽과 서쪽 짝을 이루어야 되므로 구분해서 계산쉽지 않은 문제이런 방법..

알고리즘 2017.10.27

[codility] 풀어보기 - CountDiv

Lesson 5-2 - CountDiv내용 : Compute number of integers divisible by k in range [a..b]. 시간 복잡도 : O(1)정답 코드 : 1234567891011int solution(int A, int B, int K) { double a = A; double b = B; int ret = floor(b/K) - floor((a - 1)/K); return ret; } Colored by Color Scriptercs 출처 : http://ccomsi.tistory.com/56 이번엔 수학 이론을 알아야 가능 했던 문제 인 듯...수포자는 ㅠㅠ 수학 이론은https://namu.wiki/w/%ED%95%A9%EB%8F%99%EC%8B%9D여기에서 확인..

알고리즘 2017.10.24

[codility] 풀어보기 - MaxCounters

Lesson 4-4 - MaxCounters내용 : Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.문제 설명은 여기를 참고 http://hojak99.tistory.com/316 시간 복잡도 : O(N+M)80% 코드 :function solution(N, A) { // write your code in JavaScript (Node.js 6.4.0) let rt_arr = new Array(N); let max_counter = 0; let now_idx=0; rt_arr.fill(0); for..

알고리즘 2017.10.18

[codility] 풀어보기 - PermMissingElem

Lesson 3-2 - PermMissingElem내용 :Find the missing element in a given permutation시간 복잡도 : O(N) 성공 했지만 민망해서 공개 안하는 코드 : function isZero(element) { return element == 1; } function solution(A) { // write your code in JavaScript (Node.js 6.4.0) let rt_arr=new Array(A.length+1); rt_arr.fill(1); for(i in A) { rt_arr[A[i]-1]=0; } //console.log(rt_arr.findIndex(isZero)+1); return rt_arr.findIndex(isZero)+..

알고리즘 2017.09.26

[codility] 풀어보기 - FrogJmp

Lesson 3-1 - FrogJmp내용 :Count minimal number of jumps from position X to Y.시간 복잡도 : O(1) 성공한 코드 :function solution(X, Y, D) { // write your code in JavaScript (Node.js 6.4.0) let rt_val = (Y-X)/D; return Math.ceil(rt_val); } 결론 : 요것도 좀 쉬운듯쉬워도 댓글 보면서 다른 방법에 대한 고민 필요댓글 살펴보다 든 생각:과연 잘 짠 소스랑 무엇일까?rt_val = (Y-X)/D; return Math.ceil(rt_val);return (Y-X) % D > 0 ? (Y-X)/D+1 : (Y-X)/D;Y = JD + X, JD = -..

알고리즘 2017.09.21