ALGORITHM 299

[JAVA] 알고리즘 : Queue- 교육과정설계

import java.util.*; public class Main { public String solution(String need, String plan){ String answer="YES"; Queue Q=new LinkedList(); for(char x : need.toCharArray()) Q.offer(x); // 큐에 필수 과목 세팅 for(char x : plan.toCharArray()){ if(Q.contains(x)){ // 큐에 있으면 필수과목 if(x!=Q.poll()) return "NO"; // 큐에 있는 첫 과목이 아니면 // poll()은 꺼내고 리턴받음 } } if(!Q.isEmpty()) return "NO"; // 큐가 비어있지 않으면 return answer; }..

ALGORITHM 2022.07.14

[JAVA] 알고리즘 : Stack- 후위식 연산(postfix)

import java.util.*; public class Main { public int solution(String str) { int answer=0; Stack stack = new Stack(); for(char x : str.toCharArray()){ if(Character.isDigit(x)) stack.push(x-48); // 숫자라면 스택에 추가 else{ int rt=stack.pop(); int lt=stack.pop(); // 늦게 꺼내진 숫자가 왼쪽에 위치 if(x=='+') stack.push(lt+rt); else if(x=='-') stack.push(lt-rt); else if(x=='*') stack.push(lt*rt); else if(x=='/') stack.pus..

ALGORITHM 2022.07.12

[JAVA] 알고리즘 : Stack- 올바른 괄호

import java.util.*; public class Main { public String solution(String str){ String answer = "YES"; Stack stack = new Stack(); for(char x : str.toCharArray()){ if(x=='(') stack.push(x); else{ if(stack.isEmpty()) return "NO"; // 닫는 괄호가 많은 상황 stack.pop(); } } if(!stack.isEmpty()) return "NO"; // 여는 괄호가 많은 상황 return answer; } public static void main(String[] args) { Main T = new Main(); Scanner sc =..

ALGORITHM 2022.07.11

[JAVA] 백준 13414번- 수강신청

https://www.acmicpc.net/problem/13414 13414번: 수강신청 입력 데이터는 표준 입력을 사용한다. 입력은 1개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 과목의 수강 가능 인원 K(1 ≤ K ≤ 100,000)와 학생들이 버튼을 클릭한 순서를 기록한 대기목 www.acmicpc.net import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); int l = sc.nextInt(); int count = 0; LinkedHashSet set = new LinkedHashSet()..

ALGORITHM 2022.07.07

[JAVA] 백준 16499번- 동일한 단어 그룹화하기

https://www.acmicpc.net/problem/16499 16499번: 동일한 단어 그룹화하기 첫째 줄에 단어의 개수 N이 주어진다. (2 ≤ N ≤ 100) 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 소문자로만 이루어져 있고, 길이는 10을 넘지 않는다. www.acmicpc.net import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); HashSet hs = new HashSet(); for(int i=0; i

ALGORITHM 2022.07.06

[JAVA] 백준 10546번- 배부른 마라토너

https://www.acmicpc.net/problem/10546 10546번: 배부른 마라토너 마라토너라면 국적과 나이를 불문하고 누구나 참가하고 싶어하는 백준 마라톤 대회가 열린다. 42.195km를 달리는 이 마라톤은 모두가 참가하고 싶어했던 만큼 매년 모두가 완주해왔다. 단, 한 명 www.acmicpc.net import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); HashMap map = new HashMap(); for(int i=0; i

ALGORITHM 2022.07.06

[JAVA] 백준 1822번- 차집합

https://www.acmicpc.net/problem/1822 1822번: 차집합 첫째 줄에는 집합 A의 원소의 개수 n(A)와 집합 B의 원소의 개수 n(B)가 빈 칸을 사이에 두고 주어진다. (1 ≤ n(A), n(B) ≤ 500,000)이 주어진다. 둘째 줄에는 집합 A의 원소가, 셋째 줄에는 집합 B의 원소 www.acmicpc.net import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int[] arrA = new int[a]; int[] arrB = new ..

ALGORITHM 2022.07.06