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

설정

트랙백

댓글

728x90
SMALL

C#(다른 분 거를 참조했다)

 

 

 

 

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

public class Solution 
{
    int[,] matrix;
        
    public int solution(int n, int[,] edge) 
    {
        matrix = new int[n + 1, n + 1];
        
        for (int i = 0; i < edge.GetLength(0); i++)
        {
            matrix[edge[i, 0], edge[i, 1]] = 1;
            matrix[edge[i, 1], edge[i, 0]] = 1;
        }        
        
        List<int> counts = new List<int>(BFS(1));
        
        int max = counts.Max();
        int count = counts.Count(val => val == max);

        return count;
    }
    
    public int[] BFS(int start)
    {
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(start);
        
        bool[] isFound = new bool[matrix.GetLength(0)];
        isFound[start] = true;
        
        int[] distance = new int[matrix.GetLength(0)];
        distance[start] = 0;

        while (queue.Count > 0)
        {
            int now = queue.Dequeue();
            
            for (int next = 1; next < matrix.GetLength(0); next++)
            {
                if (matrix[now, next] == 0)
                    continue;
                if (isFound[next])
                    continue;

                queue.Enqueue(next);
                isFound[next] = true;
                
                distance[next] = distance[now] + 1;
            }
        }
        
        return distance;
    }
}

 

 

 

 

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

 

 

 

자바

 

 

 

import java.util.*;

class Solution {
    public int solution(int n, int[][] edge) {
        int[] dist = new int[n+1];
        boolean[][] map = new boolean[n+1][n+1];
        
        for(int i =0; i<edge.length; i++)
        {
            map[edge[i][1]][edge[i][0]] = map[edge[i][0]][edge[i][1]]= true;
        }

        LinkedList<Integer> que = new LinkedList<Integer>();
        que.add(1);

        while(!que.isEmpty())
        {
            int temp = que.pollFirst();
            
            for(int i = 2; i<n+1; i++)
            {
                if(map[temp][i]&&dist[i]==0)
                {
                    que.addLast(i);
                    dist[i] = dist[temp]+1;
                }
            }
        }

        Arrays.sort(dist);
        int i = dist.length-1;
        
        for(; i>0; --i)
        {
            if(dist[i]!=dist[i-1])
            {
                break;
            }
        }

        return dist.length-i;
    }
}

 

 

 

728x90

설정

트랙백

댓글

728x90
SMALL

C#

 

 

 

using System;

public class Solution {
    private int n;
    private int[] queen;
    private int answer = 0;

    public int solution(int n) {
        this.n = n;
        this.queen = new int[n];
        locate(0);
        return answer;
    }

    private void locate(int currentRow)
    {
        if (currentRow == n)
            answer++;
        else
        {
            for (int inx = 0; inx < n; inx++)
            {
                queen[currentRow] = inx;

                if (isSearch(currentRow))
                {      
                    locate(currentRow+1);
                }
            }
        }
    }

    private bool isSearch(int currentRow)
    {
        for (int inx = 0; inx < currentRow; inx++)
        {
            if (queen[inx] == queen[currentRow])
                return false;
                //직선

            if (Math.Abs(inx - currentRow) == Math.Abs(queen[inx] - queen[currentRow]))
                return false;
                //대각선
        }

        return true;
    }
}

 

 

 

 

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

 

 

 

자바

 

 

 

import java.util.*;

class Solution {
    static int[] col;
    public boolean check(int here) {
        
        for(int i = 0; i < here; i++) 
        {
            if(col[here] == col[i]) return false;
            if(Math.abs(col[here] - col[i]) == here - i) return false;
        }
        return true;
    }
    public int dfs(int n, int index) {
        
        if(index == n) 
        {
            return 1;
        }
        int res = 0;
        
        for(int i = 0; i < n; i++) 
        {
            col[index] = i;
            if(check(index)) res += dfs(n, index + 1);
            col[index] = -1;
        }
        return res;
    }
    
    public int solution(int n) 
    {
        int answer = 0;
        col = new int[n];
        for(int i = 0; i < n; col[i++] = -1);
        answer = dfs(n, 0);
        return answer;
    }
}

 

 

728x90

설정

트랙백

댓글

728x90
SMALL

C#

 

 

using System;

public class Solution {
    public int[] solution(int[] prices) {
        int[] answer = new int[prices.Length];
        int[] sample = prices;

        for(int i = 0;i<prices.Length-1;i++)
        {
            for(int j = i+1;j<prices.Length;j++)
            {
                if(prices[i] <= prices[j])
                {
                    answer[i]++;
                }
                else
                {
                    answer[i]++;
                    break;
                }
            }
        }
        
        answer[prices.Length-1] = 0;
        return answer;
    }
}

 

 

 

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

 

 

using System;
using System.Collections.Generic;

public class Solution
{
    public int[] solution(int[] prices)
    {
        Stack<int> stack = new Stack<int>();
        int length = prices.Length;
        List<int> answer = new List<int>(prices);

        for (int i = 0; i < length; i++)
        {
            while (stack.Count != 0 && prices[stack.Peek()] > prices[i])
            {
                int top = stack.Pop();
                answer[top] = i - top;
                // 뒤에 있는 가격이 비교기준이 되는 가격보다 낮아질 경우에 대입하는 방법
            }
            stack.Push(i);
        }

        while (stack.Count != 0)
        {
            int top = stack.Pop();
            answer[top] = length - 1 - top;
            // 가격이 안 떨어졌을 때의 대입방법
        }

        return answer.ToArray();
    }
}

 

 

 

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

 

 

 

자바

 

 

 

import java.util.Stack;

class Solution {
    public int[] solution(int[] prices) {
        Stack<Integer> beginIdxs = new Stack<>();
        int i=0;
        int[] terms = new int[prices.length];
        beginIdxs.push(i);
        
        for (i=1; i<prices.length; i++) 
        {
            while (!beginIdxs.empty() && prices[i] < prices[beginIdxs.peek()]) 
            {
                int beginIdx = beginIdxs.pop();
                terms[beginIdx] = i - beginIdx;
            }
            beginIdxs.push(i);
        }
        
        while (!beginIdxs.empty()) 
        {
            int beginIdx = beginIdxs.pop();
            terms[beginIdx] = i - beginIdx - 1;
        }

        return terms;
    }
}

 

728x90

설정

트랙백

댓글