codility Lesson 풀어보기 6

Coding Test 준비/codility 2022. 8. 20. 19:36

Codiliy Lesson 풀어보기 Day6.

 - 아직은 Easy 위주로 진행.

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

 

Codility Lesson 10. Prime And Composite Numbers - CountFactors

 - 단순 비교를 통해 계산 시 시간복잡도 O(N) 으로 수행되고, 입력값 N이 클 때 타임아웃이 발생해서 퍼포먼스를 만족하지 못한다.

 - 비교 및 연산 횟수를 줄일 수 있는 방법을 찾자.

   > n의 약수들 중 sqrt(n) 이하인 값들을 구하면 나머지는 n을 해당 값으로 나누어서 구할 수 있다.

https://app.codility.com/demo/results/trainingWK76H5-MVV/

 

Test results - Codility

A positive integer D is a factor of a positive integer N if there exists an integer M such that N = D * M. For example, 6 is a factor of 24, because M = 4 satisfies the above condition (24 = 6 * 4). Write a function: class Solution { public int solution(in

app.codility.com

 

Codility Lesson 10. Prime And Composite Numbers - MinPerimeterRectangle

 - CountFactors와 비슷하게 진행 (단순 비교 계산시 O(N)으로 타임아웃)

 - 일정 값 이상부터는 반복이므로 i*i<=N으로 range 제한

https://app.codility.com/demo/results/trainingGGEHYS-QAX/

 

Test results - Codility

An integer N is given, representing the area of some rectangle. The area of a rectangle whose sides are of length A and B is A * B, and the perimeter is 2 * (A + B). The goal is to find the minimal perimeter of any rectangle whose area equals N. The sides

app.codility.com

 

Codility Lesson 11. Euclidean algorithm - ChocolatesByNumbers

 - 제목 그대로 유클리드 알고리즘으로 gcd 구현해서 풀이

https://app.codility.com/demo/results/trainingTGFZGV-EMB/

 

Test results - Codility

Two positive integers N and M are given. Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1. You start to eat the chocolates. After eating a chocolate you leave only a wrapper. You begin with eating chocolate num

app.codility.com

 

Codility Lesson 12. Caterpillar method - AbsDistinct

 - hashMap을 이용해서 abs(key) 값에 true 세팅하여 문제 풀이 (arraylist 사용 시 기존에 값이 존재하는지 여부에 대한 체크가 필요하므로 중복을 고려하지 않는 haspMap을 사용)

https://app.codility.com/demo/results/trainingB37FTT-5GB/

 

Test results - Codility

A non-empty array A consisting of N numbers is given. The array is sorted in non-decreasing order. The absolute distinct count of this array is the number of distinct absolute values among the elements of the array. For example, consider array A such that:

app.codility.com

 

Codility Lesson 12. Caterpillar method - CountDistinctSlices

 - 이해가 안간다... 머리 살려내..!

 - 구글링으로 컨닝

https://app.codility.com/demo/results/training9BE75S-SG5/

 

Test results - Codility

An integer M and a non-empty array A consisting of N non-negative integers are given. All integers in array A are less than or equal to M. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The slice consists of the eleme

app.codility.com

 

Codility Lesson 12. Caterpillar method - CountTriangles

 - lesson6에서 했던 triangle 을 응용해서 풀이

https://app.codility.com/demo/results/trainingX6ZFPA-77U/

 

Test results - Codility

An array A consisting of N integers is given. A triplet (P, Q, R) is triangular if it is possible to build a triangle with sides of lengths A[P], A[Q] and A[R]. In other words, triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: A[P] + A[Q] > A[R],

app.codility.com

 

 

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

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

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