//구글콘솔 광고 추가가
대충 만든 자판 문제

 

내 코드
using System;
using System.Collections.Generic;
public class Solution {
    public int[] solution(string[] keymap, string[] targets) 
    {   
        Dictionary<char, int > dic = new Dictionary<char, int>();
        List<int> answer = new List<int>();
        int num = 0;
        //알파벳별 최소순서 정리
        for (int i = 0; i < keymap.Length; i++)
        {
            for (int j = 0; j < keymap[i].Length; j++)
            {
                if (!dic.ContainsKey(keymap[i][j]))
                {
                    dic.Add(keymap[i][j], j+1);
                }
                else
                {
                    if(dic[keymap[i][j]] > j+1)
                    {
                        dic[keymap[i][j]] = j+1;
                    }
                }
            }
        }
        //확인
        for (int i = 0; i < targets.Length; i++)
        {
            for (int j = 0; j < targets[i].Length; j++)
            {
                if(dic.ContainsKey(targets[i][j]))
                {
                    num += dic[targets[i][j]];
                }
                else
                {
                    num = -1;
                    break;
                }    
                
            }
            answer.Add(num);
            num = 0;
        }
        return answer.ToArray();
    }
}
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
반응형

테스트 케이스 추가 시킴

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
반응형
달리기 경주 문제

 

내 코드
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
반응형
겹치는 선분의 길이 문제

내 코드
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
반응형

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

기울기가 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
반응형
옹알이 (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
반응형
무작위로 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
반응형

+ Recent posts