//구글콘솔 광고 추가가
728x90
반응형
덧칠하기 문제

내 코드
using System;

public class Solution {
    public int solution(int n, int m, int[] section) {
        int answer = 1;
        int paint = section[0] + (m-1);
        for (int i = 0; i < section.Length; i++)
        {
            if (paint < section[i])
            {
                answer++;
                paint = section[i] + (m-1);
            }
        }
        return answer;
    }
}
728x90
반응형
728x90
반응형

테스트 케이스 추가 시킴

Parameters  : ["..", "##"]  Return : [1, 0, 2, 2]


바탕화면 정리 문제

내 코드
using System;

public class Solution {
    public int[] solution(string[] wallpaper) {
        int[] answer = new int[4];
        bool isFirst = false;
        for (int i = 0; i < wallpaper.Length; i++)
        {
            for (int j = 0; j < wallpaper[i].Length; j++)
            {
                if (wallpaper[i][j] == '#')
                {
                    if (!isFirst)
                    {
                        answer[0] = i;
                        answer[1] = j;
                        answer[2] = i+1;
                        answer[3] = j + 1;
                        isFirst = true;
                    }
                    if(answer[1] > j)
                    {
                        answer[1] = j;
                    }
                    if (answer[0] < i)
                    {
                        answer[2] = i+1;
                    }
                    if (answer[3] < j+1)
                    {
                        answer[3] = j+1;
                    }
                }
            }
        }
        return answer;
    }
}
728x90
반응형
728x90
반응형
달리기 경주 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public string[] solution(string[] players, string[] callings) {
        List<string> answer = new List<string>();
        Dictionary<string, int> resultDic = new Dictionary<string, int>();
        List<string> list = new List<string>();
        int num = 0;
        foreach(string player in players)
        {
            resultDic.Add(player, num);
            num++;
        }
        foreach(string call in callings)
        {
            int n = resultDic[call];
            string name = players[n - 1];
            players[resultDic[call]-1] = call;
            
            //추월당한 사람 이름
            //var _key = resultDic.FirstOrDefault(x => x.Value == resultDic[call] - 1).Key;
            
            players[resultDic[call]] = name;
            
            resultDic[name]++; //추월 당한 사람 순위 ++
            resultDic[call]--; //추월 하려는 사람 순위 --

        }
        return players;
    }
}
728x90
반응형
728x90
반응형
겹치는 선분의 길이 문제

내 코드
using System;
using System.Linq;
public class Solution {
    public int solution(int[,] lines) {
        int answer = 0;
        int [] a = { lines[0, 0], lines[1, 0], lines[2, 0] }; 
        int [] b = { lines[0, 1], lines[1, 1], lines[2, 1] };
        int aMin = a.Min(); 
        int bMax = b.Max();
        for (int k = aMin; k < bMax; k++) 
        {
            int count = 0;
            if( k >= a[0] && k + 1 <= b[0])
            {
                count++;
            }
            if (k >= a[1] && k + 1 <= b[1])
            {
                count++;
            }
            if (k >= a[2] && k + 1 <= b[2])
            {
                count++;
            }
            if(count >= 2)
            {
                answer++;
            }
        }
        return answer;
    }
}
728x90
반응형
728x90
반응형

이문제는 두 점을 이용해서 기울기를 구하는 방법를 알고 있어야 한다. 당연하게도 평행이 되려면 기울기가 같으면 됨.

기울기가 m , y절편이 n인 직선의 방정식 >> y = mx +n
직선의 두점 (x1, y1), (x2, y2)를 지날때,
기울기 = (y값의 증가량) / (x값의 증가량) == (y1 -y2) / (x1- x2)

평행 문제

내 코드
using System;

public class Solution { //기울기 구하기
    public int solution(int[,] dots) {
        if((double)(dots[0,0] - dots[1,0]) / (dots[0,1] - dots[1,1]) == (double)(dots[2,0] - dots[3,0]) / (dots[2,1] - dots[3,1]))
        {
            return 1;
        }
        if((double)(dots[0,0] - dots[2,0]) / (dots[0,1] - dots[2,1]) == (double)(dots[1,0] - dots[3,0]) / (dots[1,1] - dots[3,1]))
        {
            return 1;
        }
        if((double)(dots[0,0] - dots[3,0]) / (dots[0,1] - dots[3,1]) == (double)(dots[1,0] - dots[2,0]) / (dots[1,1] - dots[2,1]))
        {
            return 1;
        }
        return 0;
    }
}
728x90
반응형
728x90
반응형
옹알이 (1) 문제

내 코드
using System;

public class Solution {
    public int solution(string[] babbling) {
        int answer = 0;
        string[] ableTalk = {"aya", "ye", "woo", "ma"};
        for (int i = 0; i < babbling.Length; i++)
        {
            for (int j = 0; j < ableTalk.Length; j++)
            {
                babbling[i] = babbling[i].Replace(ableTalk[j], "H");
            }

            babbling[i] = babbling[i].Replace('H'.ToString(),"");
            if (babbling[i] == "") answer++;
        }        
        return answer;
    }
}
728x90
반응형
728x90
반응형
무작위로 K개의 수 뽑기 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int[] solution(int[] arr, int k) {
        var answer = new List<int>(arr).Distinct().ToList();
        var answerArr = new List<int>();
        for(int i =0; i < k; i++)
        {
            if(i < answer.Count)
                answerArr.Add(answer[i]);
            else
                answerArr.Add(-1);
        }
        return answerArr.ToArray();
    }
}

 

728x90
반응형
728x90
반응형
전국 대회 선발 고사 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int solution(int[] rank, bool[] attendance) {
        int answer = 0;
        Dictionary<int, int> dic = new Dictionary<int, int>();
        for (int i = 0; i < rank.Length; i++)
        {
            if (attendance[i])
                dic.Add(rank[i], i);
        }
        List<int> list = dic.Keys.ToList();
        list.Sort();
        answer = dic[list[0]] * 10000 + dic[list[1]] * 100 + dic[list[2]];
        return answer;
    }
}
728x90
반응형

+ Recent posts