Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Last active May 23, 2020 02:59
Show Gist options
  • Save DongguemYoo/0ca540e76b42f30e7655824d8b5bf65c to your computer and use it in GitHub Desktop.
Save DongguemYoo/0ca540e76b42f30e7655824d8b5bf65c to your computer and use it in GitHub Desktop.
코딩테스트 연습 프린트
첫 예제는 통과했는데
반례를 통과 못해서 수정함
반례
/////////////////////////////////////////////////////////////////////////////////
반례는 다음과 같습니다.
[1,2,3] , 0 => 3
1이 언제 인쇄되는지 확인해야 합니다.
1을 인쇄하자니, 중요도가 더 큰 2와 3이 있습니다. 그래서 맨 뒤로 보냅니다.
[2,3,1]
2를 인쇄하자니, 중요도가 더 큰 3이 있습니다. 그래서 맨 뒤로 보냅니다.
[3,1,2]
3을 인쇄합니다. 3보다 중요도가 더 큰 인쇄물은 없습니다.
그래서 3은 맨 처음으로 인쇄가 됩니다.
[1,2]
1을 인쇄하자니, 중요도가 더 큰 2가 있습니다. 1을 맨 뒤로 보냅니다.
[2,1]
2를 인쇄합니다. 2보다 중요도가 더 큰 인쇄물은 없습니다.
그래서 2가 두 번째로 인쇄됩니다.
[1]
1을 인쇄합니다. 1보다 중요도가 더 큰 인쇄물은 없습니다.
그래서 1은 세 번째로 인쇄됩니다.
1은 세 번째로 인쇄되므로, 3을 리턴해야 합니다. 하지만 코드 상으로는 2가 리턴됩니다.
/////////////////////////////////////////////////////////////////////////////////
public int solution(int[] priorities, int location)
{
int answer = 0;
Dictionary<int, int> dic = new Dictionary<int, int>();
Queue<int> indexs = new Queue<int>();
for (int i = 0; i < priorities.Length; i++)
{
dic.Add(i, priorities[i]);
indexs.Enqueue(i);
}
int targetvalue = dic[indexs.Peek()];
int count = 0;
int temp = 0;
while (indexs.Count != 0)
{
//가장 큰 숫자라면
//추가된 부분////추가된 부분////추가된 부분////추가된 부분//
if (dic.Count == 1)
{
return ++count;
}
//추가된 부분////추가된 부분////추가된 부분////추가된 부분////추가된 부분//
if (targetvalue >= dic.Where(x => x.Key != indexs.Peek()).Max(x => x.Value))
{
count++;
if (location == indexs.Peek())
{
answer = count;
return count;
}
dic.Remove(indexs.Dequeue());
targetvalue = dic[indexs.Peek()];
}
else
{
temp = indexs.Dequeue();
indexs.Enqueue(temp);
targetvalue = dic[indexs.Peek()];
}
}
return answer;
}
//다른사람의 풀이
//Queue<KeyValuePair<int, int>> que = new Queue<KeyValuePair<int, int>>();
//KeyValuePair라는것을 몰랐다 다음에는
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(int[] priorities, int location)
{
int answer = 0;
Queue<KeyValuePair<int, int>> que = new Queue<KeyValuePair<int, int>>();
for(int i = 0; i < priorities.Length; i++)
{
que.Enqueue(new KeyValuePair<int, int>(i, priorities[i]));
}
while(true)
{
int nMax = que.Max(x => x.Value);
var kv = que.Dequeue();
if (kv.Value == nMax)
{
if (kv.Key == location) return answer + 1;
else
{
answer++;
continue;
}
}
que.Enqueue(kv);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment