본문 바로가기

공부하자/Codility

[Codility] Lesson4. MissingInteger (C#)

문제: app.codility.com/programmers/lessons/4-counting_elements/missing_integer/

 

MissingInteger coding task - Learn to Code - Codility

Find the smallest positive integer that does not occur in a given sequence.

app.codility.com

 

 

66% - 다시 풀어야 함

 

            int result = 1;
            List<int> ListA = new List<int>(A);
            ListA.Sort();
            int index = ListA.IndexOf(1);

            if (index > 0) // 음수 지우기
                ListA.RemoveRange(0, index - 1);

            if (ListA.Count == 0)
                return result;
            else if (ListA.Count == 1)
            {
                if (ListA[0] == 1)
                    return result + 1;
            }
            else
            {
                for (int i = 0; i < ListA.Count; i++)
                {
                    if (!ListA.Contains(i + 1))
                        return i + 1;
                    else if (i == ListA.Count - 1)
                        return ListA[i] + 1;
                }
            }

            return result;