본문 바로가기
프로그래밍/Codility

[c#] Codility - PermMissingElem

by 뽀도 2019. 9. 1.

<문제>

 


 

<풀이>

...더보기
using System;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;

// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");

class Solution {
    public int solution(int[] A) {
          var list = A.ToList();
            var count = list.Count + 1;


            long listSum = 0;

            foreach(var r in list)
            {
                listSum = r + listSum;
            }


            var idx = 0;

            long totalSum = 0;
            long augmenter = 1;

            while (idx < count)
            {
                totalSum = totalSum + (augmenter);
                augmenter++;
                idx++;
            }

            return (int)(totalSum - listSum);
    }
}

 

 

<시간복잡도>

 

<풀이방법>

1. 1 2 3 5 라고 할 때 1 + 2+ 3+ 4+ 5 = 15가 나온다.

15에서 1 + 2 + 3 + 5 하면 '4' 누락된 숫자가 나온다. 

 

 

반응형

댓글