강의로 돌아가기
Toa

(C#) 이거 언어에 따라서 시간 복잡도 차이가 너무 큰것 같아요

이렇게 dictionary >> sorteddictionary >> sorteddic + sortedset 계속 몸비틀어 가면서 시간 줄이는데도
효율성에서 시간 초과가 뜨네요

작성중인 코드―solution.cs
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
44
45
46
47
48
using System;
using System.Linq;
using System.Collections.Generic;

public class Solution {
    public int solution(int[] A, int[] B)
{
    int answer = 0;
    SortedDictionary<int, int> dictA = new SortedDictionary<int, int>();
    SortedSet<int> keys = new SortedSet<int>();

    for (int i = 0; i < B.Length; i++)
    {
        if (dictA.ContainsKey(B[i]))
            dictA[B[i]]++;
        else
            dictA.Add(B[i], 1);

        keys.Add(B[i]);
    }

    for (int i = 0; i < A.Length; i++)
    {
        if (dictA.Count == 0) break;

        int key;

        // A[i]보다 큰 값 찾기
        var view = keys.GetViewBetween(A[i] + 1, int.MaxValue);
        if (view.Count > 0)
        {
            key = view.Min;
            answer++;
        }
        else
            key = keys.Min; // A[i]보다 큰 값이 없으면 가장 작은 값 대입

        if (dictA[key] == 1)
        {
            dictA.Remove(key);
            keys.Remove(key);
        }
        else
            dictA[key]--;
    }
    return answer;
}
}
1 개의 답변
낙방여우

keys.GetViewBetween 이거 때문에 매번 keys 전체 중에서 탐색을 하게 되는건데 비효율적이지 않을까요?

답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다.