본문 바로가기
C#/프로그래머스

2021.07.06 C# 프로그래머스 제일 작은 수 제거하기

by ian's coding 2021. 7. 6.
728x90
반응형

문제 설명

정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요. 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1]인 경우는 [4,3,2]를 리턴 하고, [10]면 [-1]을 리턴 합니다.

제한 조건

  • arr은 길이 1 이상인 배열입니다.
  • 인덱스 i, j에 대해 i ≠ j이면 arr[i] ≠ arr[j] 입니다.

입출력 예

arrreturn

[4,3,2,1] [4,3,2]
[10] [-1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Solution{
    public int[] solution(int[] arr) {
        int[] answer = new int[] {};
        List<int> list = new List<int>();
        if(arr.Length==1){
            list.Add(-1);
            answer=list.ToArray();}
        else{
            for(int i=0;i<arr.Length;i++){
               list.Add(arr[i]);
            }
            list.Remove(list.Min());
            answer = list.ToArray();
            }
        return answer;
    }
}
cs
728x90
반응형

댓글