[프로그래머스] Lv.2 올바른 괄호
·
Algorithm/프로그래머스 코딩테스트 문제풀이전략
https://school.programmers.co.kr/learn/courses/30/lessons/12909 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.programmers.co.kr 사용 알고리즘스택풀이 import java.util.*;class Solution { boolean solution(String s) { Stack stack = new Stack(); for(char c : s.toCharArray()) { if (!stack.isEmpty()) { if (stack.peek..
N과 M 문제들 다시 풀어보기
·
Algorithm/백준
N과 M(1)1부터 N까지 자연수 중에서 중복 없이 M개를 고른 수열중복되는 수열을 여러 번 출력하면 안되며 -> boolean 배열을 사용해서 방문 체크 N과 M(2)고른 수열은 오름차순이어야 한다.위 조건으로 인해서 idx라는 파라미터가 추가되서 백트래킹을 돌리게 된다. 이전 문제에서 헷갈렸던 부분, i+1로 돌리느냐 idx + 1로 돌리느냐의 차이는 idx + 1로 돌리면, 현재 인덱스를 기준으로 다음 인덱스를 호출하기 때문에 항상 오름차순이지 않다. 하지만, i+1은 현재 요소를 기준으로 다음 요소를 선택하기 때문에 [1,2,3,4] -> 1을 다 순회했다면 다시 1을 사용하지 않아서 모두 오름차순이 된다.dfs(i + 1, depth + 1); N과 M(3)같은 수를 여러 번 골라도 된다.말그대..
[백준] Silver 1. 스타트와 링크
·
Algorithm/백준
https://www.acmicpc.net/problem/14889 사용 알고리즘백트래킹브루트 포스풀이import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { static int n; static int [][] board; static boolean [] visited; static int min = Integer.MAX_VALUE; // 재귀 종료 조건 -> depth가 n/2도달 public static void main(String[] args) throws IOExce..
[백준] Silver II. 부분수열의 합
·
Algorithm/백준
https://www.acmicpc.net/problem/1182 알고리즘백트래킹풀이import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { static int n, s; static int [] arr; static int count = 0; static boolean [] visited; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(ne..
[백준] Gold IV. N-Queen
·
Algorithm/백준
https://www.acmicpc.net/problem/9663 사용 알고리즘백트래킹풀이import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main { static int n; static int [] arr; static int count = 0; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = ..
[백준] Silver.1 연산자 끼워넣기
·
Algorithm/백준
https://www.acmicpc.net/problem/14888 사용 알고리즘백트래킹재귀풀이import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main { static int n; static int [] nums; static int [] cal = new int [4]; static int max = Integer.MIN_VALUE; static int min = Integer.MAX_VALUE; public static void main(String[] args) throws IOException {..
[프로그래머스] Lv.2 요격 시스템
·
Algorithm/프로그래머스
https://school.programmers.co.kr/learn/courses/30/lessons/181188 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.programmers.co.kr 사용 알고리즘그리디 알고리즘정렬풀이import java.util.*;class Solution { public int solution(int[][] targets) { int answer = 0; Arrays.sort(targets, (a,b) -> a[1] - b[1]); int before = 0; for(int [] targ..
[백준] Gold 4 카드 정렬하기
·
Algorithm/백준
https://www.acmicpc.net/problem/1715 사용 알고리즘우선순위 큐 풀이import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); // 10만 , 제한시간 2초 ..
[백준] Silver 1 절댓값 힙
·
Algorithm/백준
https://www.acmicpc.net/problem/11286  import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); PriorityQueue pq = new PriorityQ..
[프로그래머스] Lv.2 디펜스 게임
·
Algorithm/프로그래머스
https://school.programmers.co.kr/learn/courses/30/lessons/142085 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.programmers.co.kr 이전 제출 답안import java.util.*;class Solution { public int solution(int n, int k, int[] enemy) { int answer = 0; // min = 1, max = 5 mid = 3 // 10만 * int [] tmp = Arrays.copyOf(enemy, enem..