본문 바로가기

Algorithm118

[백준 알고리즘 / JAVA] 1541번 잃어버린 괄호 문제 정보 문제 링크: https://www.acmicpc.net/problem/1541 문제 번호: 1541번 문제 이름: 잃어버린 괄호 문제 난이도: 실버2 반복 학습 날짜 2024.01.25 완료 1541번: 잃어버린 괄호 첫째 줄에 식이 주어진다. 식은 ‘0’~‘9’, ‘+’, 그리고 ‘-’만으로 이루어져 있고, 가장 처음과 마지막 문자는 숫자이다. 그리고 연속해서 두 개 이상의 연산자가 나타나지 않고, 5자리보다 www.acmicpc.net 코드 import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { // 입력을 받기 위해 BufferedRead.. 2024. 3. 14.
[Algorithm / Programmers] 바탕화면 정리 [Version] ⦁ 2024.03.13 / [Algorithm / Programmers] 바탕화면 정리 class Solution { public int[] solution(String[] wallpaper) { printMap(wallpaper); // 1. 드래그할 구역을 지정하기 위해 변수 선언 int minWidth = Integer.MAX_VALUE; int maxWidth = Integer.MIN_VALUE; int minHeight = Integer.MAX_VALUE; int maxHeight = Integer.MIN_VALUE; // 2. 반복문을 순회하며 드래그할 구역을 찾자 for(int i = 0; i < wallpaper.length; i++) { // 3. 문자열을 문자 단위로 .. 2024. 3. 13.
[Algorithm / Programmers] 행렬의 덧셈 [Version] ⦁ 2024.03.13 / [Algorithm / Programmers] 행렬의 덧셈 class Solution { public int[][] solution(int[][] arr1, int[][] arr2) { int[][] answer = new int[arr1.length][arr1[0].length]; // 1. 반복문을 순회하며, 각 인덱스에 해당하는 값을 더한다. for(int i = 0; i < arr1.length; i++) { for(int j = 0; j < arr1[0].length; j++) { answer[i][j] = arr1[i][j] + arr2[i][j]; } } // 2. 결과 값 반환 return answer; } } 2024. 3. 13.
[Algorithm / Programmers] 모의고사 [Version] ⦁ 2024.03.13 / [Algorithm / Programmers] 모의고사 import java.util.*; class Solution { public int[] solution(int[] answers) { List answer = new ArrayList(); // 1. 수포자가 찍는 방식의 값을 셋팅한다. int[] person1 = {1, 2, 3, 4, 5}; int[] person2 = {2, 1, 2, 3, 2, 4, 2, 5}; int[] person3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; // 2. 각 학생이 맞은 숫자를 카운트 하기 위해 변수 선언 int person1Count = 0; int person2Count = 0; int pe.. 2024. 3. 13.
[Algorithm / Programmers] 가장 큰 수 찾기 [Version] ⦁ 2024.03.13 / [Algorithm / Programmers] 가장 큰 수 찾기 class Solution { public int[] solution(int[] array) { int maxNumber = Integer.MIN_VALUE; int maxNumberIndex = 0; // 1. 반복문을 순회하며 최댓 값을 찾는다. for(int i = 0; i maxNumber) { maxNumber = array[i]; maxNumberIndex = i; } } // 2. 결과 값 셋팅 int[] answer = new int[2]; answer[0] = maxNumber; answer[1] = maxNumbe.. 2024. 3. 13.
[Algorithm / Programmers] 7의 개수 [Version] ⦁ 2024.03.13 / [Algorithm / Programmers] 7의 개수 class Solution { public int solution(int[] array) { int answer = 0; // 1. 반복문을 순회한다. for(int i = 0; i < array.length; i++) { // 2. 문자로 변환한 후 이를 쪼갠다 String convertNumber = Integer.toString(array[i]); char[] input = convertNumber.toCharArray(); // 3. 반복문을 순회하며 해당 문자가 7일경우 answer를 증가한다. for(int j = 0; j < input.length; j++) { if(input[j] == '.. 2024. 3. 13.
[Algorithm / Programmers] 2차원으로 만들기 [Version] ⦁ 2024.03.13 / [Algorithm / Programmers] 2차원으로 만들기 class Solution { public int[][] solution(int[] num_list, int n) { // 1. 2차원 배열의 크기를 구하기 int width = n; int height = num_list.length / n; // 2. 배열 크기 할당 int[][] answer = new int[height][width]; // 3. 반복문을 순회하며 2차원 배열에 값을 넣기 int index = 0; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { answer[i][j] = num_list[index]; .. 2024. 3. 13.
[Algorithm / Programmers] 잘라서 배열로 저장하기 [Version] ⦁ 2024.03.13 / [Algorithm / Programmers] 잘라서 배열로 저장하기 class Solution { public String[] solution(String my_str, int n) { int startIndex = 0; int endIndex = n; // 1. 총 반복 횟수를 구하고 반복문을 순회한다. int round = my_str.length() % n == 0 ? my_str.length() / n : my_str.length() / n + 1; String[] answer = new String[round]; for(int i = 0; i < answer.length; i++) { // 2.문자열을 startIndex부터 endIndex까지 자른.. 2024. 3. 13.
[Algorithm / Programmers] 가위 바위 보 [Version] ⦁ 2024.03.13 / [Algorithm / Programmers] 가위 바위 보 class Solution { public String solution(String rsp) { StringBuilder sb = new StringBuilder(); // 가위: 2, 바위: 0, 보: 5 String[] input = rsp.split(""); // 1. 반복문을 순회하며 각 경우에 이기는 경우를 넣는다. for(String word : input) { switch (word) { case "2": sb.append("0"); break; case "0": sb.append("5"); break; case "5": sb.append("2"); break; } } // 2. 결과 값.. 2024. 3. 13.