검색결과 리스트
코딩테스트에 해당되는 글 5건
- 2022.06.26 codility Lesson 풀어보기 2
- 2022.06.19 codility Lesson 풀어보기
글
codility Lesson 풀어보기 2
Coding Test 준비/codility
2022. 6. 26. 17:29
Codiliy Lesson 풀어보기 Day2.
- Day1(06.19)에 Lesson 2까지 전부 풀었으나, 작성한 글이 날아가서 Lesson2 Arrays 먼저 작성 후 Lesson3 Time Complexity 문제 풀이 진행
- 아직은 Easy 위주로 진행.
Codility Lesson 2. Arrays - CyclicRotation
public int[] solution(int[] A, int K) {
LinkedList<Integer> list = new LinkedList<>();
boolean checkValue;
//validation value
checkValue = checkValue(A, K);
if(!checkValue) {
return A;
}
//int[] to LinkedList
for(int a : A){
list.add(a);
}
for(int i=0; i<K; i++) {
list.addFirst(list.pollLast());
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
private boolean checkValue(int[] a, int k) {
if(a.length < 1) {
return false;
}
if(k < 1 ) {
return false;
}
return true;
}
Codility Lesson 2. Arrays - OddOccurencesInArray
public int solution(int[] A) {
// write your code in Java SE 8
Map<Integer, Integer> map = new HashMap<>();
//make map
for(int a : A) {
map.put(a, map.getOrDefault(a, 0) + 1);
}
//find odd num
for(int key : map.keySet()) {
if(map.get(key)%2 == 1) return key;
}
return -1;
}
Codility Lesson 3. Time Complexity - CyclicRotation
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
return (int)Math.ceil((double)(Y-X)/D);
}
https://app.codility.com/demo/results/trainingA5PMWS-AFH/
Codility Lesson 3. Time Complexity - PermMissingElem
public int solution(int[] A) {
// write your code in Java SE 8
int[] matchNum = new int[A.length+2];
for(int i=0; i<A.length; i++){
matchNum[A[i]] = 1;
}
for(int i=1; i<matchNum.length; i++){
if(matchNum[i] == 0) return i;
}
return -1;
}
https://app.codility.com/demo/results/training626AX7-JTG/
'Coding Test 준비 > codility' 카테고리의 다른 글
codility Lesson 풀어보기 6 (0) | 2022.08.20 |
---|---|
codility Lesson 풀어보기 5 (0) | 2022.08.15 |
codility Lesson 풀어보기 4 (0) | 2022.08.06 |
codility Lesson 풀어보기 3 (0) | 2022.06.29 |
codility Lesson 풀어보기 (0) | 2022.06.19 |
설정
트랙백
댓글
글
codility Lesson 풀어보기
Coding Test 준비/codility
2022. 6. 19. 14:30
이미 굳어버린 머리에 산소호흡 시켜보고자 강의 수강외에 코딩 테스트도 추가로 풀기로 함.
첫 시작은 codility Lesson 부터 시작 (06.19)
Codility Lesson 1. binary Gap
// 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 N) {
// write your code in Java SE 8
String binaryCode;
int maxGap;
boolean checkValue;
//get binary code
binaryCode = getBinaryCode(N);
//validate binaryCode
checkValue = validateBinaryCode(binaryCode);
//validation failed string return without calculation
if(!checkValue) return 0;
//get max gap value
maxGap = getMaxGap(binaryCode);
return maxGap;
}
private static int getMaxGap(String binaryCode) {
int gapNum=0, maxGap=0;
for(int i=0; i<binaryCode.length(); i++) {
if (binaryCode.charAt(i) == '0') {
gapNum++;
} else {
if (gapNum > maxGap) {
//update max Gap Num
maxGap = gapNum;
}
//init gapNum count
gapNum = 0;
}
}
return maxGap;
}
private static boolean validateBinaryCode(String binaryCode) {
//0, 1, 00, 01, 10, 10 is no gap
if(binaryCode.length()<2) return false;
//check string has only '1' value
if (binaryCode.indexOf('0')<0) return false;
//check string has only one '1' value
if(binaryCode.indexOf('1') == binaryCode.lastIndexOf('1')) return false;
//add additional validation code
return true;
}
private static String getBinaryCode(int n) {
return Integer.toBinaryString(n);
}
}
결과 :
'Coding Test 준비 > codility' 카테고리의 다른 글
codility Lesson 풀어보기 6 (0) | 2022.08.20 |
---|---|
codility Lesson 풀어보기 5 (0) | 2022.08.15 |
codility Lesson 풀어보기 4 (0) | 2022.08.06 |
codility Lesson 풀어보기 3 (0) | 2022.06.29 |
codility Lesson 풀어보기 2 (0) | 2022.06.26 |