본문 바로가기

코딩테스트

CodeSignal / Intro / Edge of the Ocean _1

6번 부터는 찌끔 시간이 걸렸습니다. .


Ⅳ. adjacentElementsProduct // 인접요소의 곱

 

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3], the output should be

adjacentElementsProduct(inputArray) = 21.

7 and 3 produce the largest product. // 아무래도 인접한 요소들의 곱 중에서 최댓값을 구하라는 것인듯

Input/Output

[execution time limit] 3 seconds (java)

[input] array.integer inputArray

An array of integers containing at least two elements.

Guaranteed constraints:

2 ≤ inputArray.length ≤ 10,

-1000 ≤ inputArray[i] ≤ 1000.

[output] integer

The largest product of adjacent elements.

》》

int adjacentElementsProduct(int[] inputArray) {

int i=0;

int max=-1000; // 그냥 가능한 최소의 수를 넣었음 max=inputArray[i]; 로 해도 될듯

int x;

for(i=0;i<inputArray.length-1;i++){

x=inputArray[i]*inputArray[i+1]; // 순서대로 인접한 두 수의 곱을 x에 저장

max=max<x?x:max; // max가 x보다 작으면 x를, 아니면 max를 max 변수에 저장 ( 최댓값 구하기 )

}return max;

}

 

Ⅴ. shapeArea

 

Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.

A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.

Example

For n = 2, the output should be

shapeArea(n) = 5;

For n = 3, the output should be

shapeArea(n) = 13.

Input/Output

[execution time limit] 3 seconds (java)

[input] integer n

Guaranteed constraints:

1 ≤ n < 104.

[output] integer

The area of the n-interesting polygon.

》》

int shapeArea(int n) {

return ((2*n*n)-(2*n) +1); // 이거는 그냥 증가하는 패턴보고 공식을 구해서 넣었음

}

 

Ⅵ. Make Array Consecutive 2 ( 연속 배열을 만들어라 )

 

Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.

// 우렐렐레 .. 머 .. 생일에 다른사이즈의 스태츄를 받았는데 작은 것 부터 1씩 커지도록 나열하고 싶어한다.. 그러려면 스태츄들이 몇개 더 필요한데, 몇개가 필요한지 알려달라 ..!

Example

For statues = [6, 2, 3, 8], the output should be

makeArrayConsecutive2(statues) = 3.

Ratiorg needs statues of sizes 4, 5 and 7. // -> [ 2,3,4,5,6,7,8 ] 세 개가 더 있으면 1씩 증가하도록 나열할 수 있구나

Input/Output

[execution time limit] 3 seconds (java)

[input] array.integer statues

An array of distinct non-negative integers.

Guaranteed constraints:

1 ≤ statues.length ≤ 10,

0 ≤ statues[i] ≤ 20.

[output] integer

The minimal number of statues that need to be added to existing statues such that it contains every integer size from an interval [L, R] (for some L, R) and no other sizes.

》》

int makeArrayConsecutive2(int[] statues) {

// for(int i=0;i<statues.length-1;i++){

// if(statues[i]>statues[i+1]){ // 정렬하고 싶어서 해봄

// int temp=statues[i+1];

// statues[i+1]=statues[i];

// statues[i]=temp;

// }

// }

int min=20; // 가능한 최댓값 넣음

int max=0; // 가능한 최솟값 넣음

for(int i=0; i<statues.length;i++){

if(statues[i]<min){

min=statues[i]; // 최솟값 구하기

}

if(statues[i]>max){

max=statues[i]; // 최댓값 구하기

}

}

int n=max-min-1; // 최대-최소-1 = 필요한 스태츄 개수

int count=n-(statues.length-2); // statues.length-2 -> 전체 원소 수에서 최대,최소 값 두개 뺀거

if(statues.length==1){ // 배열 원소가 하나 뿐이면 더 필요한 스태츄 없음

return 0;

}

return count;

}