본문 바로가기

💡 Algorithm22

[Java] 두 배열 합치기 - Two-Pointers Algorithm public class INF0301 { public ArrayList solution(int n, int m, int[] a, int[] b) { ArrayList answer = new ArrayList(); int p1 = 0, p2 = 0; while(p1 < n && p2 < m) { if(a[p1] < b[p2]) answer.add(a[p1++]); else answer.add(b[p2++]); } while(p1 < n) answer.add(a[p1++]); while(p2 < m) answer.add(b[p2++]); return answer; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); i.. 2023. 10. 28.
[JAVA] 등수구하기 - Array(1, 2차원 배열) public class INF0208 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] score = new int[n]; for (int i = 0; i < n; i++) { score[i] = sc.nextInt(); } INF0208 inf = new INF0208(); int[] rank = inf.solution(score); for (int i : rank) { System.out.print(i + " "); } } public int[] solution(int[] score) { int[] rank = new int[score.length]; int .. 2023. 10. 24.
[JAVA] 임시반장 정하기 - Array(1, 2차원 배열) public class INF0211 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] grid = new int[n+1][6]; for(int i=1; i 2023. 10. 23.
[JAVA] 봉우리 - Array(1, 2차원 배열) public class INF0210 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] grid = new int[n+2][n+2]; //0 채우기 for (int i = 0; i grid[i][j-1] && grid[i][j] > grid[i][j+1]) { answer++; } } } return answer; } } 풀이 정석대로 가생이에 0 채우고 중앙에 값을 입력받아 상하좌우를 비교한 방식. 문제는 없지만 나중에 DFS, BFS 등 난이도 있는 문제를 하다 보면 영상처럼 경곗값 확인을 자주 하게 되는데, 나처럼 if문에 상하좌우 조건을 4개 걸 경우에.. 2023. 10. 22.
[JAVA] 특정 문자 뒤집기 - String(문자열) public class INF0105 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); INF0105 inf = new INF0105(); System.out.println(inf.solution(s)); } public String solution(String s) { String answer = ""; char[] c = s.toCharArray(); int lt = 0, rt = s.length() - 1; // 1 while(lt < rt) { // 2 if(!Character.isAlphabetic(c[lt])) { // 3 lt++; // 3 }else if(!C.. 2023. 10. 21.
[JAVA] 단어 뒤집기 - String(문자열) public class INF0104T2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = sc.next(); } INF0104T2 inf = new INF0104T2(); for(String s : inf.solution(n, arr)) { System.out.println(s); } } public ArrayList solution(int n, String[] arr) { ArrayList answer = new ArrayList(); //직접 뒤집.. 2023. 10. 20.
[JAVA] 백준 #10250 - ACM 호텔 10250번: ACM 호텔 프로그램은 표준 입력에서 입력 데이터를 받는다. 프로그램의 입력은 T 개의 테스트 데이터로 이루어져 있는데 T 는 입력의 맨 첫 줄에 주어진다. 각 테스트 데이터는 한 행으로서 H, W, N, 세 정수 www.acmicpc.net public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int testCnt = sc.nextInt(); for (int i = 0; i < testCnt; i++) { int floor = sc.nextInt(); // 층수 입력 int room = sc.nextInt(); // 방 수 입력 int customer = sc.next.. 2023. 10. 19.
[JAVA] 백준 #1157 - 단어 공부 1157번: 단어 공부 알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다. www.acmicpc.net public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine().toUpperCase(); int[] strCount = new int[26]; //알파벳 개수를 저장할 배열 for(int i=0; i 2023. 10. 18.
[JAVA] 백준 #2920 - 음계 2920번: 음계 다장조는 c d e f g a b C, 총 8개 음으로 이루어져있다. 이 문제에서 8개 음은 다음과 같이 숫자로 바꾸어 표현한다. c는 1로, d는 2로, ..., C를 8로 바꾼다. 1부터 8까지 차례대로 연주한다면 ascending, 8 www.acmicpc.net public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] melody = new int[8]; for (int i=0; i melody[i - 1]){ isDecending = false; } } if (isAscending) { System.out.println("ascending"); } e.. 2023. 10. 17.