//구글콘솔 광고 추가가
테스트 케이스 추가
Parameters Return
[["00:01", "00:10"], ["00:19", "00:29"]] 2
[["08:00", "08:30"], ["08:00", "13:00"], ["12:30", "13:30"]] 2
[["16:00", "16:10"], ["16:20", "16:30"], ["16:40", "16:50"]] 1
[["09:10", "10:10"], ["10:20", "12:20"], ["12:30", "13:20"]] 1
[["10:00", "10:10"]] 1
호텔 대실

 

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

public class Solution {
    private int StringChangeInt(string str)
    {
        string[] strSplit = str.Split(':');
        return int.Parse(strSplit[0]) * 60 + int.Parse(strSplit[1]);
    }

    public int solution(string[,] book_time) {
        int answer = 0;
        int[] keyArr = new int[book_time.GetLength(0)];
        List<(int, int)> list = new List<(int, int)>();
        List<int> roomCheck = new List<int>();

        for (int i = 0; i < book_time.GetLength(0); i++)
        {
            list.Add((StringChangeInt(book_time[i, 0]), StringChangeInt(book_time[i, 1])));

        }
        var sortlist = list.OrderBy(o => o.Item1);

        foreach((int inRoom, int outRoom) room in sortlist)
        {
            int num = roomCheck.FindIndex(n => n <= room.inRoom - 10);
            if (num == -1)
            {
                roomCheck.Add(room.Item2);
            }
            else
            {
                roomCheck[num] = room.Item2;
            }
        }
        answer = roomCheck.Count;
        return answer;
    }
}
728x90
신고 결과 받기

 

내 코드
using System;
using System.Collections.Generic;
public class Solution {
    public int[] solution(string[] id_list, string[] report, int k) {
        int[] answer = new int[id_list.Length];
        Dictionary<string, List<string>> resultDic = new Dictionary<string, List<string>>();
        
        foreach (string r in report)
        {
            string[] str = r.Split(" ");
            if(!resultDic.ContainsKey(str[1]))
            {
                List<string> list = new List<string>();
                list.Add(str[0]);
                resultDic.Add(str[1],list);
                continue;
            }
            if (!resultDic[str[1]].Contains(str[0]))
            {
                resultDic[str[1]].Add(str[0]);
            }
        }
        for (int i = 0; i < id_list.Length; i++)
        {
            foreach (KeyValuePair<string, List<string>> item in resultDic)
            {
                if (item.Value.Contains(id_list[i]))
                {
                    if (item.Value.Count >= k)
                    {
                        answer[i] ++;
                    }
                }
            }
        }
        return answer;
    }
}
728x90

테스트 케이스 추가

numbers(long[]) Return
[1001, 337, 0, 1, 333, 673, 343, 221, 898, 997, 121, 1015, 665, 779, 891, 421, 222, 256, 512, 128, 100] [1002, 338, 1, 2, 334, 674, 347, 222, 899, 998, 122, 1019, 666, 781, 893, 422, 223, 257, 513, 129, 101]
2개 이하로 다른 비트 문제

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

//+6
public class Solution {
    public long[] solution(long[] numbers) {
        long[] answer = new long[numbers.Length];
        for (int i = 0; i < numbers.Length; i++)
        {
            string numStr = Convert.ToString(numbers[i], 2);
            int startIndex = numStr.IndexOf("1");

            int zeroPoint = numStr.LastIndexOf("0");
            if (zeroPoint == -1)
            {
                string sub = numStr.Substring(startIndex+1);
                numStr = "10" + sub;
            }
            else
            {
                char[] chArr = numStr.ToCharArray();
                chArr[zeroPoint] = '1';
                if(zeroPoint +1 < numStr.Length) 
                    chArr[zeroPoint+1] = '0';
                numStr = new string(chArr);
            }
            answer[i] = Convert.ToInt64(numStr, 2);
        }
        return answer;
    }
}
728x90

딕셔너리와 함수를 만들어서 푼 문제.

+4점

할인 행사 문제

내 코드

 

using System;                                                                           // +4
using System.Collections.Generic;
public class Solution {
    public int solution(string[] want, int[] number, string[] discount) {
        int answer = 0;
        Dictionary<string,int> map = new Dictionary<string, int>();
        for (int i = 0; i < want.Length; i++)
        {
            map[want[i]] = number[i];
        }
        //10일연속으로 구매성공해야 되니까 할인되는 날짜에서 -9
        for (int i = 0; i < discount.Length-9; i++)
        {
            answer += CheckStart(map, discount, i);
        }
        return answer;
    }
    //열흘간 원하는 제품 다 구매 가능한지 확인 함수
    public int CheckStart(Dictionary<string, int> dic, string[] str, int index)
    {
        Dictionary<string, int> map = new Dictionary<string, int>(dic);
        for (int i = index; i < index + 10; i++)
        {
            if (map.ContainsKey(str[i]))
            {
                map[str[i]]--;
            }
            else
                return 0;
        }
        bool isZero = false; //원하는 재료 다 구매됬는지 체크 bool 값
        foreach(int n in map.Values)
        {
            if(n == 0)
            {
                isZero = true;
            }
            else
            {
                isZero = false; //한개라도 재료 남아있으면 false
                break;
            }
        }
        if (isZero)
        {
            return 1;
        }
        else
            return 0;
    }
}
728x90
예산 문제

내 코드
using System;

public class Solution {
    public int solution(int[] d, int budget) {
        int answer = 0;
        Array.Sort(d);
        int num = d.Length;
        for(int i =0; i < num; i++)
        {
            if (budget >= 0 && (budget - d[i]) >= 0)
            {
                budget -= d[i];
                answer = i+1;
            }
            else
            {
                answer = i;
                break;
            }       
        }
        return answer;
    }
}
728x90

테스트 케이스 하나 추가

Parameters Return
n(int) 2 words(string[]) ["ac", "ca", "ac", "ac"] [1, 2]
영어 끝말잇기 문제

내 코드
using System;

class Solution
{
    public int[] solution(int n, string[] words)
    {
        int[] answer = new int[2];
        int num = 0;
        for (int i = 0; i < words.Length; i++)
        {
            if (num != 0) break;
            if (i < words.Length - 1)
            {
                if (words[i][words[i].Length - 1] != words[i + 1][0])
                {
                    num = i + 1;
                    break;
                }
                for (int j = 0; j < i; j++)
                {
                    if (words[i+1] == words[j])
                    {
                        num = i+1;
                        break;
                    }
                }
            }
            else
            {
                if (words[i - 1][words[i - 1].Length - 1] != words[i][0])
                {
                    num = i;
                    break;
                }
                for (int j = 0; j < i; j++)
                {
                    if (words[i] == words[j])
                    {
                        num = i;
                        break;
                    }
                }
            }
        }
        if (num != 0)
        {
            answer[0] = num % n + 1;
            answer[1] = num / n + 1;
        }
        else
        {
            answer[0] = 0;
            answer[1] = 0;
        }
        return answer;
    }
}
728x90

쉬운 문제라 별다른 생각 없이 풀었다가 테스트 10이 틀렸다. 고민하던 찰나에 혹시나 해서  result에 숫자도 없고 x값도 그냥 "x" 일 때인가 싶어서 이 조건 하나 추가 해서 통과했다. 질문창에 가보니 나와 같이 Test10이 문제였던 사람들이 많아서 글로 추가 작성 해뒀었다. 그 후로 내 글 보고 통과됐다는 사람들이 있어서 기분 좋았던 문제.

다항식 더하기 문제

내 코드
using System;
using System.Linq;
public class Solution {
    public string solution(string polynomial) {
        string[] answer = polynomial.Split(" ");
        string result = "";
        string str = "";
        int xNum = 0;
        int num = 0;
        for (int i = 0; i < answer.Length; i += 2)
        {
            if (answer[i].Contains("x"))
            {
                str = answer[i].Replace("x", string.Empty);
                if (str == "")
                {
                    str = "1";
                }
                xNum += Convert.ToInt32(str);
            }
            else
                num += Convert.ToInt32(answer[i]);
        }
        if (num == 0)
        {
            if(xNum ==1)
                result = "x";
            else
                result = xNum + "x";
        }
        else
        {
            if(xNum ==0)
                result = num.ToString();
            else if(xNum ==1)
            {
                result = "x " + "+ " + num;
            }
            else
                result = xNum + "x " + "+ " + num;
        }
        return result;
    }
}
728x90

처음에 아무 생각 없이 정렬해서 가장 작은 수를 지워버렸는데 그런 문제가 아니였다. 배열 안에서 가장 작은 수만 제거 하면 된다. 이게 왜 9점이나 줬을까.


제일 작은 수 제거하기 문제

내 코드
using System;
using System.Collections.Generic;
//+9
public class Solution {
    public int[] solution(int[] arr) {
        int min = arr[0];
        List<int> answer = new List<int>(arr);
        if (arr.Length == 1) answer.Add(-1);
        else
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if(min > arr[i]) min = arr[i];
            }
        }
        answer.Remove(min);
        return answer.ToArray();
    }
}

 

728x90

+ Recent posts