
@오답코드
- 단순하게 DFS, 백트래킹으로 가능한 경우의 수를 검사했다.
기본 테스트 케이스는 모두 통과했지만, 일부 케이스에서 시간 초과가 발생했다.
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
|
public class Solution
{
private int minValue = int.MaxValue;
public long solution(int n, int[] works)
{
DFS(n, works);
//남은 작업량이 없어서 최소값 갱신이 안된 경우의 예외처리
return minValue == int.MaxValue ? 0 : minValue;
}
public void DFS(int workingHours, int[] works)
{
//다 일했으면 야근지수를 검사하고 최소값 갱신
if (workingHours == 0)
{
int sum = works.Select(x => x * x).Sum();
minValue = Math.Min(sum, minValue);
return;
}
//1시간 일했다.
workingHours--;
for(int i = 0; i < works.Length; i++)
{
if (works[i] - 1 < 0)
continue;
works[i]--;
//일한 시간을 뺀 works를 깊은 복사
int[] newWorks = new int[works.Length];
Array.Copy(works, newWorks, works.Length);
DFS(workingHours, newWorks);
//빼기 전 값으로 배열을 원복한다. (백트래킹)
works[i]++;
}
}
}
|
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
using System.Collections.Generic;
public class PriorityQueue<T>
{
private readonly List<T> heap;
private readonly IComparer<T> comparer;
public PriorityQueue(IComparer<T> comparerValue, int length)
{
heap = new List<T>(length);
comparer = comparerValue;
}
public int Count => heap.Count;
public void Enqueue(T item)
{
heap.Add(item);
int index = Count - 1;
while (index > 0)
{
int parentIndex = (index - 1) / 2;
if (comparer.Compare(heap[index], heap[parentIndex]) >= 0)
break;
Swap(index, parentIndex);
index = parentIndex;
}
}
public T Dequeue()
{
T item = heap[0];
heap[0] = heap[Count - 1];
heap.RemoveAt(Count - 1);
int index = 0;
while (true)
{
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
if (leftChildIndex >= Count)
break;
int minChildIndex = (rightChildIndex < Count
&& comparer.Compare(heap[rightChildIndex], heap[leftChildIndex]) < 0) ?
rightChildIndex : leftChildIndex;
if (comparer.Compare(heap[index], heap[minChildIndex]) <= 0)
break;
Swap(index, minChildIndex);
index = minChildIndex;
}
return item;
}
private void Swap(int indexA, int indexB)
{
T temp = heap[indexA];
heap[indexA] = heap[indexB];
heap[indexB] = temp;
}
}
public class Solution
{
public long solution(int n, int[] works)
{
//구현된 최소 우선 순위 큐를 Comparer를 새로 정의해서 최대 우선 순위 큐로 변경
PriorityQueue<int> priorityQueue = new PriorityQueue<int>(Comparer<int>.Create((x, y) => y.CompareTo(x)), works.Length);
//우선 순위 큐 삽입
foreach (var work in works)
priorityQueue.Enqueue(work);
while (n > 0 && priorityQueue.Count > 0)
{
//가장 큰 작업을 뽑고 뺀다.
int maxWork = priorityQueue.Dequeue();
if (maxWork > 0)
{
maxWork--;
n--;
priorityQueue.Enqueue(maxWork);
}
}
long sum = 0;
while (priorityQueue.Count > 0)
{
//제곱 계산
int dequeuedValue = priorityQueue.Dequeue();
sum += (long)dequeuedValue * dequeuedValue;
}
return sum;
}
}
|
cs |
'프로그래머스 - 내 풀이 > 프로그래머스 Lv3' 카테고리의 다른 글
[C#]프로그래머스/기지국 설치 (0) | 2023.06.04 |
---|---|
[C#]프로그래머스/숫자 게임 (4) | 2023.05.28 |
[C#]프로그래머스/N으로 표현/DP (0) | 2023.04.29 |
[C#]프로그래머스/네트워크/그래프,DFS (2) | 2023.04.28 |
[C#]프로그래머스/이중우선순위큐 (0) | 2023.04.28 |