코딩테스트

CodeSignal / Intro / Edge of the Ocean _2

호댕이다 2020. 2. 19. 15:53

왜 이지인지 모르겠는.. 나한테는 넘 하드 ,,....


Ⅶ. almostIncreasingSequence

 

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

// 계속 증가하는 시퀀스를 만들어라. 원소 하나 제거 가능

Example

For sequence = [1, 3, 2, 1], the output should be

almostIncreasingSequence(sequence) = false.

There is no one element in this array that can be removed in order to get a strictly increasing sequence.

For sequence = [1, 3, 2], the output should be

almostIncreasingSequence(sequence) = true.

You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

Input/Output

[execution time limit] 3 seconds (java)

[input] array.integer sequence

Guaranteed constraints:

2 ≤ sequence.length ≤ 105,

-105 ≤ sequence[i] ≤ 105.

[output] boolean

Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.

》》

boolean almostIncreasingSequence(int[] sequence) {

int i,j;

int count =0;

boolean onechance;

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

if(sequence[i]>=sequence[i+1]){ // 앞에 있는 수가 더 크면 카운트 업

count++;

if(i>0 && (i+1)<sequence.length-1){

onechance=false; // onechance 초기화

// 앞의 원소 제거했을 때 트루거나 뒤의 원소 제거했을 떄 트루면 리턴 트루;

if(sequence[i-1]<sequence[i+1]){ // sequence[i] 값 뺴고 비교했을 때 증가하는 시퀀스면 트루

onechance=true;

}

if(sequence[i]<sequence[i+2]){ // sequence[i+1]값 빼고 비교했을 때 증가하는 시퀀스면 트루

onechance=true;

}

if(onechance==false){return false;} // 한 번 기회를 줬는데도 false면 리턴 폴스

}

}

}

if (count>1){ // 카운트가 한 번 초과하면 false ( 원소를 한 번만 제거할 수 있그든, 그 이상은 실패다 이말이야)

return false;

}else{return true;}

}

Ⅷ. matrixElementsSum

 

After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.

Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0).

// 유령이 있는 방과 유령이 있는 방의 아래에 있는 방을 쓰기 싫어하는 미스터 코드보츠, 코드보츠가 쓸 수 있는 방 값의 합은 ?

Example

For

matrix = [[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]]

the output should be

matrixElementsSum(matrix) = 9.

There are several haunted rooms, so we'll disregard them as well as any rooms beneath them. Thus, the answer is 1 + 5 + 1 + 2 = 9.

For

matrix = [[1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10]]

the output should be

matrixElementsSum(matrix) = 9.

Note that the free room in the final column makes the full column unsuitable for bots (not just the room directly beneath it). Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9.

Input/Output

[execution time limit] 3 seconds (java)

[input] array.array.integer matrix

A 2-dimensional array of integers representing the cost of each room in the building. A value of 0 indicates that the room is haunted.

Guaranteed constraints:

1 ≤ matrix.length ≤ 5,

1 ≤ matrix[i].length ≤ 5,

0 ≤ matrix[i][j] ≤ 10.

[output] integer

The total price of all the rooms that are suitable for the CodeBots to live in.

》》

int matrixElementsSum(int[][] matrix) {

int i,j=0;

int sum=0;

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

for(j=0;j<matrix[i].length;j++){

if(matrix[i][j] ==0){ // 유령의 방( 0 ) 이면

for(int k=i+1;k<matrix.length;k++){ // 그 밑의 방도 0으로 바꾼다

matrix[k][j]=0;

}

 

}

sum=sum+matrix[i][j]; // 모든 방 값의 합

System.out.print(matrix[i][j]);

}

} return sum;

}