//구글콘솔 광고 추가가
728x90
반응형
테스트 케이스 추가
Parameters Return
queue1(int[ ]) queue2(int[ ])
[2, 3, 2] [1, 1, 1] 1
[1, 1, 1, 1, 1, 1, 1, 1, 1, 10] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] -1

 

두 큐 합 같게 만들기 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int solution(int[] queue1, int[] queue2) {
        int answer = 0;
        Queue<int> q1 = new Queue<int>();
        Queue<int> q2 = new Queue<int>();
        long sum1 = 0;
        long sum2 = 0;

        foreach(int n in queue1)
        {
            q1.Enqueue(n);
            sum1 += n;
        }
        foreach(int n in queue2)
        {
            q2.Enqueue(n);
            sum2 += n;
        }
        //평균값 구할때 배열.Sum()으로 계산하니까 런타임 걸렸어. 알아둬라
        //long average = (queue1.Sum() + queue2.Sum()) /2;        
        long average = (sum1 + sum2) / 2;
        
        if((sum1 + sum2) % 2 !=0) return -1;
        
        int maxLength = (queue1.Length + queue2.Length)*2;
        while(answer < maxLength)
        {
            if(average == sum1) return answer;
            
            if(average > sum1)
            {
                int dequeueNum = q2.Dequeue();
                q1.Enqueue(dequeueNum);
                sum1 += dequeueNum;
                answer ++;
            }
            else
            {
                int dequeueNum = q1.Dequeue();
                q2.Enqueue(dequeueNum);
                sum1 -= dequeueNum;
                answer ++;
            }
        }
        return -1;
    }
}

 

728x90
반응형
728x90
반응형
성격 유형 검사하기 문제

 

내 코드
using System;
using System.Text;
using System.Collections.Generic;

public class Solution {
    public string solution(string[] survey, int[] choices) {
        string answer = "";
        StringBuilder answerSB = new StringBuilder();
        Dictionary<char,int> surveyDic = new Dictionary<char,int>()
        {
            { 'R', 0 },
            { 'T', 0 },
            { 'C', 0 },
            { 'F', 0 },
            { 'J', 0 },
            { 'M', 0 },
            { 'A', 0 },
            { 'N', 0 },
        };
     
        char[] c = new char[2];
        for (int i = 0; i < survey.Length; i++)
        {
            c = survey[i].ToCharArray();

            if (choices[i] > 4)
            {
                surveyDic[c[1]] += choices[i] - 4;
            }
            else if (choices[i] < 4)
            {
                surveyDic[c[0]] += 4 - choices[i];
            }
        }
        answerSB.Append(surveyDic['R'] >= surveyDic['T'] ? "R" : "T");
        answerSB.Append(surveyDic['C'] >= surveyDic['F'] ? "C" : "F");
        answerSB.Append(surveyDic['J'] >= surveyDic['M'] ? "J" : "M");
        answerSB.Append(surveyDic['A'] >= surveyDic['N'] ? "A" : "N");
        answer = answerSB.ToString();
        return answer;
    }
}
728x90
반응형

+ Recent posts