프로그래머스 코딩테스트 26번 제일 작은 수 제거하기
2022. 9. 29. 23:56 - DoosanBaek
728x90
제목 : 제일 작은 수 제거하기
문제 번호 26번
문제 설명
정수를 저장한 배열, 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] |
Solution.Java
class Solution {
public int[] solution(int[] arr) {
if(arr.length == 1) return new int[] {-1};
int min = Integer.MAX_VALUE;
for(int a : arr) {
if(min > a) min = a;
}
int[] newArr = new int[arr.length-1];
int index = 0;
for(int a : arr) {
if(min != a) newArr[index++] = a;
}
return newArr;
}
}
실행 메소드 추가
class Solution26 {
public int[] solution26(int[] arr) {
if(arr.length == 1) return new int[] {-1};
int min = Integer.MAX_VALUE;
for(int a : arr) {
if(min > a) min = a;
}
int[] newArr = new int[arr.length-1];
int index = 0;
for(int a : arr) {
if(min != a) newArr[index++] = a;
}
return newArr;
}
public static void main(String[] args) {
Solution26 solution26 = new Solution26();
int []arr = {1,2,3};
System.out.println(solution26.solution26(arr));
}
}
728x90
'알고리즘' 카테고리의 다른 글
| [백준] 1330번 : 두 수 비교하기 - JAVA (0) | 2024.11.13 |
|---|---|
| 프로그래머스 코딩테스트 3진법 뒤집기 (0) | 2022.10.02 |
| 프로그래머스 코딩테스트 문제 28번 하샤드 수 (1) | 2022.09.30 |
| 프로그래머스 코딩테스트 27번 콜라츠 추측 (0) | 2022.09.30 |
| 프로그래머스 코딩테스트25번 정수 제곱근 판별 (0) | 2022.09.29 |
| 프로그래머스 코딩테스트 문제24번 정수 내림차순으로 배치하기 (0) | 2022.09.29 |
| 프로그래밍 문제 23번 자연수 뒤집어 배열로 만들기 (0) | 2022.09.29 |
| 프로그래밍 문제 22번 자릿수 더하기 (0) | 2022.09.28 |