-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestMinNumRemove.java
More file actions
43 lines (42 loc) · 1.28 KB
/
Copy pathBestMinNumRemove.java
File metadata and controls
43 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package level1;
/*
* 정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요.
* 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1]인 경우는 [4,3,2]를 리턴 하고, [10]면 [-1]을 리턴 합니다.
제한 조건
arr은 길이 1 이상인 배열입니다.
인덱스 i, j에 대해 i ≠ j이면 arr[i] ≠ arr[j] 입니다.
입출력 예
arr return
[4,3,2,1] [4,3,2]
[10] [-1]
*/
public class BestMinNumRemove {
public static void main(String[] args) {
SolutionBestMinNumRemove su = new SolutionBestMinNumRemove();
int[] arr = {4,3,2,1};
for(int data : su.solution(arr))
System.out.print(data + " ");
}
}
class SolutionBestMinNumRemove {
public int[] solution(int[] arr) {
int[] answer = {};
if(arr.length == 1)
return new int[]{-1};
int m = 0;
for(int i=0; i<arr.length; i++) {
if(arr[m] > arr[i]) {
m = i;
}
}
int count=0;
answer = new int[arr.length-1];
for(int i=0; i<arr.length; i++) {
if(i == m) {
continue;
}
answer[count++] = arr[i];
}
return answer;
}
}