본문 바로가기

알고리즘71

[codility] CountDiv CountDiv CountDivCompute number of integers divisible by k in range [a..b].Task Score100%Correctness100%Performance100% A가 0이면 그냥 B를 K로 나눈 몫에 1을 더해주면 개수가 나온다.그런데 A가 0이 아니면 B 전에 나오는 K의 배수의 개수에서 A 이전에 나오는 K의 배수들을 빼줘야한다. 원래 (B / K +1) - ((A - 1) / K + 1) 인데 앞 뒤의 1이 서로 상쇄되므로 생략해주었다. x// you can also use imports, for example:// import java.util.*;​// you can write to stdout for debugging purposes, e... 2018. 5. 23.
[codility] MaxCounters MaxCounters MaxCountersCalculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.Task Score100%Correctness100%Performance100% correctness 100%에 performance 0% 결과.. 시간복잡도 제한이 O(N+M) 인데 O(N*M) 이 나와버려서 40대의 점수가 나와버렸다. ㅠㅠ루프 하나에서 모든 값처리를 다 하려고 했던게 요인인 것 같다. x// you can also use imports, for example:import java.util... 2018. 5. 23.
[codility] PermCheck PermCheck PermCheckCheck whether array A is a permutation.Task Score100%Correctness100%Performance100% 순열인지 체크하는 문제이다. 1부터 N까지의 숫자가 다 들어있느냐 확인만 하면 되기 때문에 아주 간단하게 체킹할 수 있다. x// you can also use imports, for example:import java.util.*;​// you can write to stdout for debugging purposes, e.g.// System.out.println("this is a debug message");​class Solution { public int solution(int[] A) { Arrays.sort.. 2018. 5. 23.
[codility] MissingInteger MissingInteger MissingIntegerFind the smallest positive integer that does not occur in a given sequence.Task Score100%Correctness100%Performance100% 이것도 날아가서 의도치 않게 재도전 ^^..!HashMap은 key값이 같으면 2번 저장해도 1개만 입력되기 때문에 중복처리를 해줄 필요 없이 그냥 쭉쭉 전체를 입력해주면 된다. 그리고 1부터 A배열의 크기(N)까지 돌려주면서 빈 값을 찾는다. 만약 1부터 N까지의 값이 다 차있을 경우 반복문을 빠져나오고 N+1이 반환된다. x// you can also use imports, for example:import java.util.*;​// y.. 2018. 5. 23.
[codility] FrogRiverOne FrogRiverOne FrogRiverOneFind the earliest time when a frog can jump to the other side of a river.Task Score100%Correctness100%Performance100% 아 원래 풀어 두었던 코드가 날아가서 재시도 ㅠㅠ.... 원래 풀이가 뭐였는지 전혀 기억이 나지 않아 새로운 마음으로 HashMap클래스를 사용해서 풀었다.X보다 작은 값만 중복되지않게 맵에 넣어주면 1, 2, 3, ..., X가 되는 순간 hashmap의 size도 X와 같게 되기 때문에 그 순간의 key값을 return하도록 했다. xxxxxxxxxx// you can also use imports, for example:import java.uti.. 2018. 5. 23.
[codility] TapeEquilibrium TapeEquilibrium TapeEquilibriumMinimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.Task Score100%Correctness100%Performance100% 먼저 보여줄 소스는 correctness 100%에 performance 0%인 코드.. 이것은 앞으로 많이 보게 될 내모습 ㅎㅎㅎ........ 그런데 원래 91%였는데 같은 소스를 넣었는데 왜 50퍼가 되었을까.. 코딜리티에서 시간복잡도 제한을 바꾼건가..뭐 이러나저러나 50%를 받았고, 일단 설명을 해보겠다.P값을 1부터 N-2까지 왔다갔다하면서 다 비교해봤다.설명을 적고보니까 왜 performance가 0% 나왔는지 알 것 같기도 하고 ^^! ㅋ.. 2018. 5. 22.
[codility] PermMissingElem PermMissingElem PermMissingElem Find the missing element in a given permutation.Task Score100%Correctness100%Performance100% 숫자가 1부터 시작하기 때문에 그냥 앞의 수가 뭔지, 뒤의 수가 뭔지 고려할 필요 없이 key 값과 비교하면 된다. 배열은 순서대로 주어지지않기 때문에 먼저 정렬을 해준 뒤 순차적으로 비교를 해준다.처음 제출했을 때는 정확도가 영 안좋게 나왔었다. 왜인가 했더니 반복문 안에서 당연히 return이 이루어질 거라고 생각하고 맨 밑의 return문을 그냥 return 0;을 했었는데, 반복문을 다 돌아도 값 반환이 이루어지지 않는 경우가 있었다. 바로 1에서 N까지 숫자가 나오는 경우! .. 2018. 5. 22.
[codility] FrogJmp FrogJmp FrogJmpCount minimal number of jumps from position X to Y.Task Score100%Correctness100%Performance100% X에서 Y로 가는데 D씩 이동하면 얼마나걸리냐 이건데, 그냥 쉽게 생각하면 된다. X에서 Y까지의 거리를 D로 나누어주면 해결! 그런데 int는 정수형이라서 나눗셈을 할 경우 실수부가 생겨도 그냥 버림처리를 해서 몫만 반환해버린다. 그래서 D로 거리가 나누어 떨어지지 않는 경우 횟수에 1을 더해주었다. x// you can also use imports, for example:// import java.util.*;​// you can write to stdout for debugging purposes, e.. 2018. 5. 22.
[codility] OddOccurrencesInArray OddOccurrencesInArray OddOccurrencesInArrayFind value that occurs in odd number of elements.Task Score100%Correctness100%Performance100% 같은 수 끼리 xor 연산을 하면 0이 되고, 0과 숫자 n을 xor 연산하면 n이 된다는 것을 이용해서 풀면 쉽게 해결할 수 있다.짝수인 수는 각각 xor 연산을 통해 0이 되고, 홀수인 수는 마지막 남은 숫자 하나가 0과 xor연산을 해 자기자신만 남게 된다. 그러므로 모든 연산을 마친 후 xor연산의 결과를 반환해주면 된다. x// you can also use imports, for example:// import java.util.*;​// you can.. 2018. 5. 22.
[codility] CyclicRotation CyclicRotation CyclicRotationRotate an array to the right by a given number of steps.Task Score100%Correctness100%PerformanceNot assessed 맨 뒤의 숫자를 임시 변수에 저장했다가 나머지 숫자들을 뒤로 한 칸씩 미룬 뒤 맨 앞에 넣어주는 방식을 K번 반복했다. performance 점수는 논외로 정확도만 본다고 했기 때문에 반복횟수를 고려하지않고 단순무식하게 생각해서 풀었다. x// you can also use imports, for example:// import java.util.*;​// you can write to stdout for debugging purposes, e.g.// Syst.. 2018. 5. 22.