검색결과 리스트
프로그래밍에 해당되는 글 335건
- 2023.02.23 프로그래머스 레벨3 가장 먼 노드 C#(JAVA) queue, BFS
- 2023.02.23 프로그래머스 레벨2 N-Queen C#(JAVA) - 완전탐색 private void/bool
- 2023.02.23 프로그래머스 레벨2 주식가격 C#(JAVA)
- 2023.02.23 프로그래머스 레벨2 뒤에 있는 큰 수 찾기 C#(JAVA) peek, pop, push
- 2023.02.11 프로그래머스 레벨2 C# 연속 부분 수열 합의 개수(Hash.Count, HashSet, Skip, Take, JAVA)
- 2023.02.11 프로그래머스 C# 행렬의 곱셈(3중 for문, JAVA)
- 2023.02.11 프로그래머스 C# 예상 대진표(JAVA)
- 2023.02.11 프로그래머스 문자열 내의 p와 y의 개수(JAVA, toLowerCase().split(""), equals)
글
프로그래머스 레벨3 가장 먼 노드 C#(JAVA) queue, BFS
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;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 레벨2 피로도 C#(완전탐색) (0) | 2023.02.24 |
---|---|
프로그래머스 이모티콘 할인행사 C#() (0) | 2023.02.23 |
프로그래머스 레벨2 N-Queen C#(JAVA) - 완전탐색 private void/bool (0) | 2023.02.23 |
프로그래머스 레벨2 주식가격 C#(JAVA) (0) | 2023.02.23 |
프로그래머스 레벨2 뒤에 있는 큰 수 찾기 C#(JAVA) peek, pop, push (0) | 2023.02.23 |
글
프로그래머스 레벨2 N-Queen C#(JAVA) - 완전탐색 private void/bool
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;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 이모티콘 할인행사 C#() (0) | 2023.02.23 |
---|---|
프로그래머스 레벨3 가장 먼 노드 C#(JAVA) queue, BFS (0) | 2023.02.23 |
프로그래머스 레벨2 주식가격 C#(JAVA) (0) | 2023.02.23 |
프로그래머스 레벨2 뒤에 있는 큰 수 찾기 C#(JAVA) peek, pop, push (0) | 2023.02.23 |
프로그래머스 레벨2 C# 연속 부분 수열 합의 개수(Hash.Count, HashSet, Skip, Take, JAVA) (0) | 2023.02.11 |
글
프로그래머스 레벨2 주식가격 C#(JAVA)
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;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 레벨3 가장 먼 노드 C#(JAVA) queue, BFS (0) | 2023.02.23 |
---|---|
프로그래머스 레벨2 N-Queen C#(JAVA) - 완전탐색 private void/bool (0) | 2023.02.23 |
프로그래머스 레벨2 뒤에 있는 큰 수 찾기 C#(JAVA) peek, pop, push (0) | 2023.02.23 |
프로그래머스 레벨2 C# 연속 부분 수열 합의 개수(Hash.Count, HashSet, Skip, Take, JAVA) (0) | 2023.02.11 |
프로그래머스 C# 행렬의 곱셈(3중 for문, JAVA) (0) | 2023.02.11 |
글
프로그래머스 레벨2 뒤에 있는 큰 수 찾기 C#(JAVA) peek, pop, push
Stack을 이용하는 거다.
C#기준으로 stack은 LIFO 개념인데, Last In First Out이다. 마지막에 들어간 게 처음으로 나온다는 의미이다.
push는 스택에 값을 때려박는 거고 pop는 맨 위에 있는 값(마지막에 들어간 값)을 리턴하면서 그걸 삭제하는 개념인 듯하다.
peek는 가장 top에 위치한 값을 리턴하는데 pop이랑은 다르게 삭제하지는 않는 방식인 거 같다.
2중 for문을 방지하기 위해서 stack을 활용한다.
s.Peek < [i]
리셋하기 위해서 s.Pop도 쓴다.
C#
public class Solution {
public int[] solution(int[] numbers) {
int[] answer = new int[numbers.Length];
var s = new System.Collections.Generic.Stack<int>();
for (int i = 0; i < numbers.Length; i++)
{
answer[i] = -1;
while (s.Count > 0)
{
if (numbers[s.Peek()] < numbers[i])
{
answer[s.Peek()] = numbers[i];
s.Pop();
}
else
{
break;
}
}
s.Push(i);
}
return answer;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int[] solution(int[] numbers) {
int[] answer = new int[numbers.Length];
Stack<int> stack = new Stack<int>();
for(int i = numbers.Length-1 ; i>=0 ; i--)
{
if(stack.Count ==0)
{
answer[i]=-1;
stack.Push(numbers[i]);
continue;
}
while(stack.Count>0)
{
if(stack.Peek()>numbers[i])
{
answer[i]=stack.Peek();
stack.Push(numbers[i]);
break;
}
stack.Pop();
}
if(answer[i]==0)
{
answer[i]=-1;
stack.Push(numbers[i]);
}
}
return answer;
}
}
자바
import java.util.*;
class Solution {
public int[] solution(int[] numbers) {
Stack<Integer> stack = new Stack<>();
int[] ret = new int[numbers.length];
for(int i = 0;i<numbers.length;i++)
{
// 하강 직선일 때는 push
if(stack.isEmpty() || numbers[i]<numbers[i-1])
{
stack.push(i);
}
else
{
// 현재값보다 작은 index는 pop하여 현재값으로
while(!stack.isEmpty() && numbers[stack.peek()]<numbers[i])
{
ret[stack.pop()] = numbers[i];
}
stack.push(i);
}
}
// 나머지는 -1
while(!stack.isEmpty())
{
ret[stack.pop()] = -1;
}
return ret;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 레벨2 N-Queen C#(JAVA) - 완전탐색 private void/bool (0) | 2023.02.23 |
---|---|
프로그래머스 레벨2 주식가격 C#(JAVA) (0) | 2023.02.23 |
프로그래머스 레벨2 C# 연속 부분 수열 합의 개수(Hash.Count, HashSet, Skip, Take, JAVA) (0) | 2023.02.11 |
프로그래머스 C# 행렬의 곱셈(3중 for문, JAVA) (0) | 2023.02.11 |
프로그래머스 C# 예상 대진표(JAVA) (0) | 2023.02.11 |
글
프로그래머스 레벨2 C# 연속 부분 수열 합의 개수(Hash.Count, HashSet, Skip, Take, JAVA)
/// 내가 작성한 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(int[] elements) {
int[] doublearray = new int[2 * elements.Length];
/// 2배 길이의 배열을 작성해서 뒤에서 앞으로 합계를 구하는 경우가 가능하게 했다.
HashSet<int> hash = new HashSet<int>(); // 중복 저장이 불가능한 HashSet을 썼다.
for(int i = 0;i<2*elements.Length;i++)
{
doublearray[i] = elements[i%elements.Length];
}
/// 2배 배열을 대입
for(int i = 0;i<elements.Length;i++)
{
for(int j = 0;j<elements.Length;j++)
{
int[] newArray = doublearray.Skip(i).Take(j+1).ToArray();
hash.Add(newArray.Sum());
}
}
return hash.Count;
}
}
// 되기는 되는데 시간이 엄청나게 오래 걸려서 겨우겨우 실행이 됐다. 그래서 다른 분 코드를 참조한 것을 했다.
// skip부터 시작해서 take에 있는 숫자 만큼 취한다.
//
//
//
//
using System;
public class Solution {
public int solution(int[] elements)
{
int answer = 0;
int[] flagedInts = new int[elements.Length * 1000 + 1]; // 100만개까지 배열을 만든다. 길이가 1000이고, 배열에 들어가는 수가 1000까지니까 최대가 1000 * 1000 = 100만개까지 나온다. 전부 0이 들어가 있다.
for(int addingLen = 1; addingLen <= elements.Length; addingLen++)
{
for (int i = 0; i < elements.Length; i++)
{
int idx = 0;
for (int j = 0; j < addingLen; j++)
{
idx += elements[(i + j) % (elements.Length)];
}
// 선택된 배열 인덱스만 1을 집어넣게 해서 if문으로 중복을 걸러내고 answer의 값을 플러스 시켜서 갯수를 구한다.
if (flagedInts[idx] == 0)
{
flagedInts[idx] = 1;
answer++;
}
}
}
return answer;
}
}
// 이게 시간이 훨씬 덜 나온다.
자바
import java.util.*;
class Solution {
public int solution(int[] elements) {
Set<Integer> set = new HashSet<>();
int start = 1;
while(start < elements.length)
{
for (int i = 0; i < elements.length; i++)
{
int value = 0;
for (int j = i; j < i+start; j++)
{
value += elements[j%elements.length];
}
set.add(value);
}
start++;
}
int sum = 0;
for (int i = 0; i < elements.length; i++)
sum += elements[i];
set.add(sum);
return set.size();
}
}
import java.util.*;
class Solution {
public int solution(int[] elements) {
int answer = 0;
Set<Integer> set = new HashSet<>();
int start = 0;
for(int i=0; i<elements.length; i++)
{
int n = 1;
int idx = i;
int sum = 0;
while(n <= elements.length)
{
sum += elements[idx++];
set.add(sum);
if(idx >= elements.length)
idx = 0;
n++;
}
}
answer = set.size();
return answer;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 레벨2 주식가격 C#(JAVA) (0) | 2023.02.23 |
---|---|
프로그래머스 레벨2 뒤에 있는 큰 수 찾기 C#(JAVA) peek, pop, push (0) | 2023.02.23 |
프로그래머스 C# 행렬의 곱셈(3중 for문, JAVA) (0) | 2023.02.11 |
프로그래머스 C# 예상 대진표(JAVA) (0) | 2023.02.11 |
프로그래머스 문자열 내의 p와 y의 개수(JAVA, toLowerCase().split(""), equals) (0) | 2023.02.11 |
글
프로그래머스 C# 행렬의 곱셈(3중 for문, JAVA)
using System;
public class Solution {
public int[,] solution(int[,] arr1, int[,] arr2) {
int[,] answer = new int[arr1.GetLength(0),arr2.GetLength(1)];
for(int i = 0;i<arr1.GetLength(0);i++)
{
for(int j = 0;j<arr2.GetLength(0);j++)
{
for (int k = 0; k < arr2.GetLength(1); k++)
{
answer[i, k] += arr1[i, j] * arr2[j, k];
}
}
}
return answer;
}
}
자바
class ProductMatrix {
public int[][] productMatrix(int[][] A, int[][] B) {
int[][] answer = new int[A.length][B[0].length];
for(int i=0;i<answer.length;i++)
{
for(int j=0;j<answer[0].length;j++)
{
for(int k=0;k<A[0].length;k++)
{
answer[i][j]+=A[i][k]*B[k][j];
}
}
}
return answer;
}
public static void main(String[] args) {
ProductMatrix c = new ProductMatrix();
int[][] a = { { 1, 2 }, { 2, 3 } };
int[][] b = { { 3, 4 }, { 5, 6 } };
// 아래는 테스트로 출력해 보기 위한 코드입니다.
System.out.println("행렬의 곱셈 : " + c.productMatrix(a, b));
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 레벨2 뒤에 있는 큰 수 찾기 C#(JAVA) peek, pop, push (0) | 2023.02.23 |
---|---|
프로그래머스 레벨2 C# 연속 부분 수열 합의 개수(Hash.Count, HashSet, Skip, Take, JAVA) (0) | 2023.02.11 |
프로그래머스 C# 예상 대진표(JAVA) (0) | 2023.02.11 |
프로그래머스 문자열 내의 p와 y의 개수(JAVA, toLowerCase().split(""), equals) (0) | 2023.02.11 |
프로그래머스 JAVA 폰켓몬 (0) | 2023.02.09 |
글
프로그래머스 C# 예상 대진표(JAVA)
using System;
class Solution
{
public int solution(int n, int a, int b)
{
int answer = 0;
int index = 0;
/// a를 2로 나눈 몫+나머지, b를 2로 나눈 몫+나머지
while(a != b)
{
index++;
a = a/2+a%2;
b = b/2+b%2;
}
return index;
}
}
/// 라운드에서 위로 올라갈 때마다 번호가 바뀌는 방법을 응용했다.
자바
class Solution
{
public int solution(int n, int a, int b)
{
int round = 0;
while(a != b)
{
a = a/2 + a%2;
b = b/2 + b%2;
round++;
}
return round;
}
}
class Solution
{
public int solution(int n, int a, int b)
{
return Integer.toBinaryString((a-1)^(b-1)).length();
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 레벨2 C# 연속 부분 수열 합의 개수(Hash.Count, HashSet, Skip, Take, JAVA) (0) | 2023.02.11 |
---|---|
프로그래머스 C# 행렬의 곱셈(3중 for문, JAVA) (0) | 2023.02.11 |
프로그래머스 문자열 내의 p와 y의 개수(JAVA, toLowerCase().split(""), equals) (0) | 2023.02.11 |
프로그래머스 JAVA 폰켓몬 (0) | 2023.02.09 |
프로그래머스 레벨0 안전지대(C#, JAVA) try catch(Exception e) (0) | 2023.02.09 |
글
프로그래머스 문자열 내의 p와 y의 개수(JAVA, toLowerCase().split(""), equals)
class Solution {
boolean solution(String s) {
int pCount = 0, yCount = 0;
String[] array = s.toLowerCase().split(""); //소문자로 바꾸고 잘라 배열에 넣음
for (int i = 0; i < array.length; i++)
{
if ("p".equals(array[i]))
{
pCount++;
}
else if ("y".equals(array[i]))
{
yCount++;
}
}
if (pCount != yCount)
{
return false;
}
return true;
}
}
'프로그래밍' 카테고리의 다른 글
프로그래머스 C# 행렬의 곱셈(3중 for문, JAVA) (0) | 2023.02.11 |
---|---|
프로그래머스 C# 예상 대진표(JAVA) (0) | 2023.02.11 |
프로그래머스 JAVA 폰켓몬 (0) | 2023.02.09 |
프로그래머스 레벨0 안전지대(C#, JAVA) try catch(Exception e) (0) | 2023.02.09 |
다항식 더하기 JAVA (0) | 2023.02.09 |