728x90
SMALL

C#

 

 

 

 

using System;

using System.Collections.Generic;

public class Solution {
   public int solution(int x, int y, int n)
        {
            int answer = 0;
            int value = 0;
            int tempValue = 0;
            Queue<int> currentValuesQu = new Queue<int>();
            bool[] visited = new bool[y + 1];

            if (x == y)
                return 0;

            currentValuesQu.Enqueue(x);
       
            while (currentValuesQu.Count != 0)
            {
                int size = currentValuesQu.Count;
                for (int iPos = 0; iPos < size; iPos++)
                {
                    value = currentValuesQu.Dequeue();

                    tempValue = value * 3;
                    if (tempValue <= y && visited[tempValue] == false)
                    {
                        currentValuesQu.Enqueue(tempValue);
                        visited[tempValue] = true;
                    }

                    tempValue = value * 2;
                    if (tempValue <= y && visited[tempValue] == false)
                    {
                        currentValuesQu.Enqueue(tempValue);
                        visited[tempValue] = true;
                    }

                    tempValue = value + n;
                    if (tempValue <= y && visited[tempValue] == false)
                    {
                        currentValuesQu.Enqueue(tempValue);
                        visited[tempValue] = true;
                    }
                }

                answer++;
                if (currentValuesQu.Contains(y))
                    return answer;
            }
            return -1;
        }
}

728x90

설정

트랙백

댓글

728x90
SMALL

C#

 

 

 

using System;
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 Max = que.Max(x=>x.Value);
            var deque = que.Dequeue();


            if (deque.Value == Max)
            {
                if (deque.Key == location) return answer + 1;
                else
                {
                    answer++;
                    continue;
                }
            }
            que.Enqueue(deque);
        }
    }
}

 

 

 

// Enqueue가 push 같은 느낌이고

// Dequeue가 pop 같은 느낌이다.

// 큐는 FIFO 처음에 들어간 게 처음에 나간다는 개념이다.

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
            Queue<int> que = new Queue<int>(Enumerable.Range(0, priorities.Length));
            List<int> max = new List<int>(priorities);

            while (que.Count > 0) 
            {
                int document = que.Dequeue();

                if (max.Max() <= priorities[document])
                {
                    answer++;
                    max.Remove(priorities[document]);

                    if (document == location)
                        break;
                }
                else
                    que.Enqueue(document);
            }

            return answer;
    }
}
728x90

설정

트랙백

댓글

728x90
SMALL

자바

 

 

 

 

import java.util.*;

class Solution {
    int[][] map;
    List<Integer> answer = new LinkedList<>();
    public int[] solution(String[] maps) {
        
        map = new int[maps.length][maps[0].length()];
        // 2차원 배열로 정의
        for(int i = 0;i<maps.length;i++)
        {
            char[] mapCharArr = maps[i].toCharArray();
            
            for(int j = 0;j<mapCharArr.length;j++)
            {
                char c = mapCharArr[j];
                
                if(c == 'X' || c == 'x')
                {
                    //X표시면 -1리턴한다
                    map[i][j] = -1;
                }
                else
                {

                    // 문자열 c의 숫자에 문자열 0(아스키코드 48)을 빼준다. 예를 들어 1이면 49 - 48 = 1이 댄다.
                    map[i][j] = c - '0';
                }
            }    
        }
        
        for(int i = 0;i<map.length;i++)
        {
            for(int j = 0;j<map[0].length;j++)
            {
                int islandSum = dfs(i,j);
                if(islandSum > 0)
                {
                    answer.add(islandSum);
                    // 무인도 값 합치기
                }
            }
        }
        
        if(answer.size() == 0) return new int[] {-1};
        // 사이즈가 0, 전부 X면 -1 배열리턴
        Collections.sort(answer);
        // 값이 작은 것부터 나오게 출력
        int[] answerArr = new int[answer.size()];
        
        for(int i = 0;i<answerArr.length;i++) answerArr[i] = answer.get(i);
        // 배열에 집어넣기
        return answerArr;
    }
    
    public int dfs(int i, int j)
    {
        if(i<0 || j<0 || i>=map.length || j>=map[0].length) return 0;
        // 범위를 벗어나면 0리턴
        if(map[i][j] == -1) return 0;
        // 이미 탐색했거나 X표시면 0을 리턴
        int tmp = map[i][j];
        map[i][j] = -1;
        // 탐색했다.
        return tmp + dfs(i-1, j) + dfs(i+1, j) + dfs(i, j-1) + dfs(i, j+1);
        // 주변 무인도 값 탐색하기
    }
}

 

 

 

/// 리스트를 만들어서 add 해주고 X표시는 -1로 해서 if문으로 0보다 큰 거만 수집할 수 있게 한 뒤에 dfs를 활용한다.

728x90

설정

트랙백

댓글

728x90
SMALL

C#

 

 

 

using System;
using System.Collections.Generic;

public class Solution {
    public int solution(string s) {
        int answer = 0;
        int sameIndex = 0;
        int diffIndex = 0;
        char word = ' ';
        bool start = true; 
        
        for(int i = 0; i < s.Length; i++)
        {
            if(start == true)
            {
                word = s[i];
                sameIndex++;
                start = false;
                // 첫 번째 글자
            }
            else
            {
                if(s[i] == word)
                {
                    sameIndex++;
                }
                else
                {
                    diffIndex++;
                }
                // 첫 번째 글자 word랑 같으면 same을 플러스하고 아니면 다른 인덱스 플러스
            }
            
            if(sameIndex == diffIndex)
            {
                answer++;
                sameIndex = 0;
                diffIndex = 0;
                start = true;
                /// 첫 글자와 같은 문자의 수 = 다른 문자의 수이면 
                /// 자른 후에 초기화 하기
            }
            
            if(i == s.Length - 1)
            {
                if(start == false)
                {
                    answer++;
                }
            }
            /// 마지막 글자에 +1을 해준다.
        }
        return answer;
    }
}

 

 

//////////////////////

 

 

 

using System;

public class Solution {
    public int solution(string s) {
        int answer = 0;

        if (s == string.Empty)
                return answer;

            char frist = s[0];

            int sameCount = 0;
            int diffCount = 0;

            for (int i = 0; i < s.Length; ++i)
            {
                if(sameCount == 0)
                    frist = s[i];

                if (frist == s[i])
                    sameCount++;
                else
                    diffCount++;

                if (sameCount == diffCount)
                {
                    answer++;

                    sameCount = 0;
                    diffCount = 0;
                }
                else
                {
                    if(i + 1 >= s.Length)
                        answer++;
                }
            }


        return answer;
    }
}

 

 

 

자바

 

 

 

public class Solution {
    public int solution(String s) {
        int answer = 0;
        char init = s.charAt(0);
        int count = 0;
        for (char c : s.toCharArray()) 
        {
            if (count == 0) 
            {
                init = c;
            }
            if (init == c) 
            {
                count++;
            } 
            else 
            {
                count--;
            }
            
            if (count == 0) 
            {
                answer++;
            }
        }

        if(count > 0) 
        {
            answer++;
        }
        return answer;
    }
}
728x90

설정

트랙백

댓글

728x90
SMALL

 

C#

 

 

 

using System;

public class Solution {
    public string solution(string[] cards1, string[] cards2, string[] goal) {
        string answer = "Yes";
        int c1 = 0;
        int c2 = 0;
        
        for(int i = 0;i<goal.Length;i++)
        {
            if(c1 < cards1.Length && goal[i] == cards1[c1])
            {
                c1++;
            }
            else if(c2 < cards2.Length && goal[i] == cards2[c2])
            {
                c2++;
            }
            else
                return "No";      
        }
        
        return answer;
    }
}

 

 

 

////////////////////

 

 

 

자바

 

 

 

import java.io.*;

class Solution {
    public String solution(String[] cards1, String[] cards2, String[] goal) {
        int cardIdx1 = 0;
        int cardIdx2 = 0;

        for(int i=0; i<goal.length; i++)
        {
            String target = goal[i];

            if(cardIdx1 < cards1.length && target.equals(cards1[cardIdx1]))
                cardIdx1++;
            else if (cardIdx2 < cards2.length && target.equals(cards2[cardIdx2]))
                cardIdx2++;
            else
                return "No";
        }

        return "Yes";
    }
}
728x90

설정

트랙백

댓글

728x90
SMALL

C#

 

 

 

using System;
using System.Collections.Generic;
public class Solution {
    public int[] solution(string[] keymap, string[] targets) {
        int[] answer = new int[targets.Length];
        Dictionary<char, int> dic = new Dictionary<char, int>();
        
        for(int i=0;i<keymap.Length;i++)
        {
            for(int j=0;j<keymap[i].Length;j++)
            {
                if(dic.ContainsKey(keymap[i][j]) == false)
                {
                    dic.Add(keymap[i][j], j+1);
                    //딕셔너리에 keymap에 있는 문자와 그에 따른 숫자를 저장해놓는다.
                } 
                else
                {
                    if(dic[keymap[i][j]] > j+1)
                    {
                        dic[keymap[i][j]] = j+1;
                    }
                    // 딕셔너리에 똑같은 게 있는데 숫자가 J+1보다 크면 
                //가장 숫자가 작았던 걸 딕셔너리에 저장한다.
                }
            }
        }

        for(int i=0;i<targets.Length;i++)
        {
            answer[i] = 0;
            
            for(int j=0;j<targets[i].Length;j++)
            {
                if(dic.ContainsKey(targets[i][j]) == true)
                {
                    answer[i] += dic[targets[i][j]];
                } 
                else
                {
                    answer[i] = -1;
                    break;
                }
            }

        }

        return answer;
    }
}

 

 

 

728x90

설정

트랙백

댓글

728x90
SMALL

C#

 

 

 

using System;

public class Solution {

    public bool[] visit;
    public int answer=0;
    
    public int solution(int k, int[,] dungeons) {
        visit = new bool[dungeons.Length];
        func(k,dungeons,visit,0);
        
        return answer;
    }

    //재귀를 위한 함수
    public int func(int k, int[,] dungeons,bool[]visit,int cnt)
    {
        for(int i=0; i<dungeons.GetLength(0); i++)
        {
            if(k >= dungeons[i,0] && !visit[i]) //현재 피로도가 최소 피로도 보다 많고 방문한적 없는 던전인지 확인
            {
                visit[i] = true;
                //현재 피로도를 해당 던전을 다녀왔으니 빼준다
                //모든 던전을 돌 것이고 
                //방문던전 수 cnt +1해준다. 
                func(k - dungeons[i,1],dungeons,visit,cnt+1);
                visit[i] = false; //재귀를 빠져나오면 새로운 던전 먼저 방문할것이기 때문에 false 
            }
        }
        answer= Math.Max(cnt, answer);
        return answer;
    }
}

728x90

설정

트랙백

댓글

728x90
SMALL

C#

 

 

 

using System;

public class Solution {
          int sign = 0;
        int earn = 0;
        public int[] solution(int[,] users, int[] emoticons)
        {
            int[] answer = new int[2] { 0, 0 };
            int[] array = new int[emoticons.Length];

            ExhaustiveSearch(array, 0, users, emoticons);

            answer[0] = sign;
            answer[1] = earn;

            return answer;
        }

        public void ExhaustiveSearch(int[] arr, int start, int[,] users, int[] emoticons)
        {
            if (start == arr.Length)
            {
                calculoate(arr, users, emoticons);
                return;
            }

            for (int iPos = 10; iPos <= 40; iPos += 10)
            {
                arr[start] = iPos;
                ExhaustiveSearch(arr, start + 1, users, emoticons);
            }
        }

        public void calculoate(int[] arr, int[,] users, int[] emoticons)
        {
            int count = 0;
            int earn_t = 0;

            for (int iPos = 0; iPos < users.GetLength(0); iPos++)
            {
                int discount = users[iPos, 0];
                int price = users[iPos, 1];
                int sum = 0;

                for (int jPos = 0; jPos < arr.Length; jPos++)
                {
                    if (arr[jPos] >= discount)
                        sum += (emoticons[jPos] / 100) * (100 - arr[jPos]);
                }

                if (sum >= price)
                    count++;
                else
                    earn_t += sum;
            }

            if (count > sign)
            {
                sign = count;
                earn = earn_t;
                return;
            }
            else if (count == sign)
            {
                if (earn < earn_t)
                {
                    earn = earn_t;
                }
            }
        }
}

 

 

 

////////////////////

 

 

 

using System;
using System.Collections.Generic;
public class Solution {
    public int[] solution(int[,] users, int[] emoticons) {
        int[] answer = new int[] {0,0};

            Stack<int[]> stack = new Stack<int[]>();
            List<int[]> combination = new List<int[]>();

            int cnt = 0;
            int total = 0;
            int[] temp = new int[emoticons.Length];

            for (int j = 1; j < 5; j++) 
            {
                temp[0] = j * 10;
                tempA(temp, 0, combination);
            }

            int price = 0;
            
            foreach (var c in combination)
            {
                total = 0;
                cnt = 0;

                for (int u =0; u < users.GetLength(0); u++)
                {
                    price = 0;

                    for (int e = 0;e<emoticons.Length;e++)
                    {
                        if(users[u,0] <= c[e])
                        {
                            price += emoticons[e] * (100 - c[e]) / 100;
                        }
                    }

                    if(price >= users[u, 1])
                    {
                        cnt += 1;
                    }
                    else
                    {
                        total += price;
                    }

                }

               if (cnt > answer[0] ||
                    (cnt == answer[0] && total > answer[1]))
                {
                    answer[0] = cnt;
                    answer[1] = total;
                }


            }

            return answer;
    }

     public static void tempA(int[] temp, int now, List<int[]> combi)
        {
            if (now+1 == temp.Length) 
            {
                combi.Add(temp.Clone() as int[]);
                return;
            }

            now = now + 1;
            for (int j = 1; j < 5; j++) 
            {
                temp[now] = j*10;
                tempA(temp, now, combi);
            }
        }
}
728x90

설정

트랙백

댓글