codility Lesson 풀어보기 5

Coding Test 준비/codility 2022. 8. 15. 20:52

Codiliy Lesson 풀어보기 Day5.

 - 아직은 Easy 위주로 진행.

 - MVC 스프링 2편 완강 후 코딩테스트 진행

 

Codility Lesson 7. Stacks and Queues - Nesting

  - Stack을 사용해서 구현

https://app.codility.com/demo/results/training8U4TPU-2T8/

 

Test results - Codility

A string S consisting of N characters is called properly nested if: S is empty; S has the form "(U)" where U is a properly nested string; S has the form "VW" where V and W are properly nested strings. For example, string "(()(())())" is properly nested but

app.codility.com

 

 

Codility Lesson 7. Stacks and Queues - StoneWall

 - Stack을 사용해서 구현.

 - 문제가 이해가 안가서 인터넷 검색 찬스..☆

https://app.codility.com/demo/results/trainingS8Q4HB-BX3/

 

Test results - Codility

You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by an array H of N positive integer

app.codility.com

 

 

Codility Lesson 8. Leader - Dominator

 - 가장 많이 발생한 숫자의 cnt가 '절반 보다 많이' 발생했을 시 Domiantor로 선정.

https://app.codility.com/demo/results/training77RP5D-XJY/

 

Test results - Codility

An array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A. For example, consider array A such that A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3 The dom

app.codility.com

 

 

Codility Lesson 8. Leader - EquiLeader

 - Leader를 구하는 로직은 dominator에서 사용한 로직을 그대로 사용함.

 - dominator의 발생 횟수를 기준으로 left 발생횟수, right 발생횟수가 각 비교군에서 절반 보다 많이 발생했는지 만족하면 equiLeadr 로 카운트

https://app.codility.com/demo/results/training3W9URK-8GS/

 

Test results - Codility

A non-empty array A consisting of N integers is given. The leader of this array is the value that occurs in more than half of the elements of A. An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A[0], A[1], ..., A[S] and A[S + 1],

app.codility.com

 

 

Codility Lesson 9. Maximum slice problem - MaxProfit

 - 생각없이 for문 2번사용하여 각 원소별로 비교했다가 퍼포먼스를 만족하지 못함

 - 최소값을 지정하여 해당 값하고만 계산하도록 변경 후 퍼포먼스도 만족

https://app.codility.com/demo/results/trainingUNXQUA-R8G/

 

Test results - Codility

An array A consisting of N integers is given. It contains daily prices of a stock share for a period of N consecutive days. If a single share was bought on day P and sold on day Q, where 0 ≤ P ≤ Q < N, then the profit of such transaction is equal to A[

app.codility.com

 

 

Codility Lesson 9. Maximum slice problem - MaxSliceSum

 - maxNum 체크 먼저 수행해서 음수가 maxNum인 경우 해당 값 리턴

 - maxNum이 양수인 경우에만 로직을 수행해서 현재 인덱스까지의 핪(currentSum)과 현재 인덱스까지의 합 중 max 값(maxSliceSum) 중 큰 값을 리턴

https://app.codility.com/demo/results/training9DCYUY-ZT7/

 

Test results - Codility

A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q]. Write a function: class Solution { public int

app.codility.com

 

'Coding Test 준비 > codility' 카테고리의 다른 글

codility Lesson 풀어보기 6  (0) 2022.08.20
codility Lesson 풀어보기 4  (0) 2022.08.06
codility Lesson 풀어보기 3  (0) 2022.06.29
codility Lesson 풀어보기 2  (0) 2022.06.26
codility Lesson 풀어보기  (0) 2022.06.19

codility Lesson 풀어보기 4

Coding Test 준비/codility 2022. 8. 6. 20:21

Codiliy Lesson 풀어보기 Day4.

 - 아직은 Easy 위주로 진행.

 - MVC 스프링 2편 완강 후 코딩테스트 진행

 

Codility Lesson 4. Counting Elements - PermCheck

  - 연달아 숫자가 존재하는지 체크

https://app.codility.com/demo/results/trainingERT3BQ-RR8/

 

Test results - Codility

A non-empty array A consisting of N integers is given. A permutation is a sequence containing each element from 1 to N once, and only once. For example, array A such that: A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2 is a permutation, but array A such that: A[0] =

app.codility.com

 

Codility Lesson 5. Prefix Sums - PassingCars

  -  0 ≤ P < Q < N

  - 뒤에서부터 계산 시 현재까지 계산된 Q의 계산값 + 새로 서쪽으로 판단되는 값을 더하면 된다는 것을 생각하자.

https://app.codility.com/demo/results/trainingYT6WAG-223/

 

Test results - Codility

A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road. Array A contains only 0s and/or 1s: 0 represents a car traveling east, 1 represents a car traveling west. The goal is to count

app.codility.com

 

Codility Lesson 6. Sorting - Distinct

ArrayList result = new ArrayList<>();
for(int i=0; i<A.length; i++)
    if(!result.contains(A[i])){
        result.add(A[i]);
    }
return result.size();

  - Distinct 배열을 만들 시 N이 클 때 퍼포먼스를 통과하지 못함 75%

  - 중복  값만 제외하는 형태로 로직 다시 짜야함

https://app.codility.com/demo/results/trainingHGSF83-423/

 

Test results - Codility

Write a function class Solution { public int solution(int[] A); } that, given an array A consisting of N integers, returns the number of distinct values in array A. For example, given array A consisting of six elements such that: A[0] = 2 A[1] = 1 A[2] = 1

app.codility.com

  - 중복값만 계산해서 A.length - 중복 갯수로 로직 구현하여 퍼포먼스도 통과

 

Codility Lesson 6. Sorting - MaxProductOfThree

  - 3개의 숫자를 곱해야하므로 가장 큰 값은 항상 들어가야하며, 가장 작은 음수 숫자 2개의 곱셈결과와 두번째, 세번째로 큰 값의 곱셈결과 중 큰 값을 구하면 되는 문제

  - 다만 모든 값이 음수인 경우 가장 작은 값 3개의 결과값이 필요함.

https://app.codility.com/demo/results/training6UJ42N-V4Y/

 

Test results - Codility

A non-empty array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N). For example, array A such that: A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6 contains the following exam

app.codility.com

 

Codility Lesson 6. Sorting - Triangle

  - triplet (P, Q, R)에 대해 A[P] + A[Q] > A[R] 조건을 만족하는 원소가 있는지만 찾으면 됨

  - MAX_INT Overflow 에 걸렸는데 (long)으로 캐스팅해서 패스함

https://app.codility.com/demo/results/training9ET7YY-UQM/

 

Test results - Codility

An array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: A[P] + A[Q] > A[R], A[Q] + A[R] > A[P], A[R] + A[P] > A[Q]. For example, consider array A such that: A[0] = 10 A[1] = 2 A[2] = 5 A[3] = 1 A[4] = 8 A

app.codility.com

 

Codility Lesson 7. Stacks and Queues - Brackets

  - Stack을 사용해서 구현

https://app.codility.com/demo/results/trainingJZAKU4-CPK/

 

Test results - Codility

A string S consisting of N characters is considered to be properly nested if any of the following conditions is true: S is empty; S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string; S has the form "VW" where V and W are properly nes

app.codility.com

 

Codility Lesson 7. Stacks and Queues - Fish

  - Stack을 사용해서 구현. 하류로 가는 물고기들의 인덱스를 stack에 저장 및 비교

https://app.codility.com/demo/results/training2C3RV4-E2U/

 

Test results - Codility

You are given two non-empty arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish

app.codility.com

 

 

'Coding Test 준비 > codility' 카테고리의 다른 글

codility Lesson 풀어보기 6  (0) 2022.08.20
codility Lesson 풀어보기 5  (0) 2022.08.15
codility Lesson 풀어보기 3  (0) 2022.06.29
codility Lesson 풀어보기 2  (0) 2022.06.26
codility Lesson 풀어보기  (0) 2022.06.19

codility Lesson 풀어보기 3

Coding Test 준비/codility 2022. 6. 29. 21:30

Codiliy Lesson 풀어보기 Day3.

 - 아직은 Easy 위주로 진행.

 

Codility Lesson 3. Time Complexity - TapeEquilibrium

public int solution(int[] A) {
    // write your code in Java SE 8
    int minNum = Integer.MAX_VALUE;
    int totalSum = 0, leftSum=0;

    for(int i : A)
        totalSum += i;

    for(int i=0; i<A.length-1; i++){
        leftSum += A[i];
        minNum = Math.min(minNum, Math.abs(leftSum - (totalSum-leftSum)));
    }

    return minNum;
}

 - for문 내에서 합계를 매번 구하도록 하면 타임아웃이 난다.

Detected time complexity :O(N)

 

https://app.codility.com/demo/results/trainingA9H573-HVU/

 

Test results - Codility

A non-empty array A consisting of N integers is given. Array A represents numbers on a tape. Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. The difference betw

app.codility.com

 

Codility Lesson 4. Counting Elements - FrogRiverOne

public int solution(int X, int[] A) {
    // write your code in Java SE 8
    int[] chkList = new int[X+1];
    for(int i=0, cnt=0; i<A.length; i++) {
        if(chkList[A[i]] != 1 && A[i]<= X){
            chkList[A[i]] = 1;
            cnt++;
        }
        if(cnt == X) return i;
    }
    return -1;
}

 - 모든 배열을 비교할 필요는 없고, X 까지 값 채워지는 순간까지만 비교하면 OK

 

https://app.codility.com/demo/results/trainingFU38QE-6B6/

'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 풀어보기 2  (0) 2022.06.26
codility Lesson 풀어보기  (0) 2022.06.19