본문 바로가기

Java70

[Java] 프로그래머스 [자비스앤빌런즈 2022 코딩테스트] 선거 import java.util.HashMap; public class Solution { public static int[] solution(int[][] orders) { int[] answer = {}; int studentCnt = orders.length; // 5 int tempCnt = 0; // 후보의 현재 특표 수를 담을 임시 변수 HashMap nomineeHm; // 후보 번호 : 득표 수 담을 해시맵 HashMap dropHm = new HashMap(); // 탈락한 후보 번호 담을 해시맵 int halfNum = 0; // 반수 answer = new int[2]; // 회차, 후보 번호 if (studentCnt % 2 == 0) { halfNum = studentCnt / 2;.. 2022. 11. 28.
[Java] 프로그래머스 [Level-2] 올바른 괄호 import java.util.LinkedList; import java.util.Queue; public class Solution { // 올바른 괄호 static boolean solution(String s) { boolean answer = true; char tempChar; Queue q = new LinkedList(); for (int i = 0; i < s.length(); i++) { tempChar = s.charAt(i); if (tempChar == '(') { // '('라면 q.offer(tempChar); // 큐에 넣기 } else { // ')'라면 if (q.isEmpty()) { // 이 시점에 큐가 비어있는 상태라면 짝이 맞지 않는 것이므로 answer = fals.. 2022. 11. 28.
[Java] 프로그래머스 [Level-2] 구명보트 import java.util.Arrays; public class Solution { // 구명보트 public static int solution(int[] people, int limit) { int answer = 0; Arrays.sort(people); // 50, 50, 70, 80 int maxWeightIndex = people.length - 1; // 구조되지 않은 사람 중 가장 무거운 무게를 가진 사람의 인덱스 int minWeightIndex = 0; // 구조되지 않은 사람 중 가장 가벼운 무게를 가진 사람의 인덱스 for (int i = maxWeightIndex; i >= 0; i--) { // 구조되지 않은 사람 중 가장 무거운 무게를 가진 사람부터 순서대로 구조 answe.. 2022. 11. 27.
[Java] 프로그래머스 [Level-4] 행렬과 연산 import java.util.ArrayDeque; import java.util.Deque; public class Solution { // 행렬과 연산 // Deque 자료구조에서 // add(A) + peek(B) or remove(B)가 있을 때 // (A)와 (B)는 First 또는 Last // (A)와 (B)가 같다면 스택(Stack)처럼 동작 // (A)와 (B)가 다르다면 큐(Queue)처럼 동작 // addFirst로 쌓고 있는 구조에서 제일 앞에 원소를 추가하고 싶다면 addLast로 추가 // addLast로 쌓고 있는 구조에서 제일 앞에 원소를 추가하고 싶다면 addFirst로 추가 static int r = 0; // rc의 행 수 static int c = 0; // rc의 열.. 2022. 11. 27.
[Java] 프로그래머스 [Level-2] 두 큐 합 같게 만들기 import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; public class Solution { // 두 큐 합 같게 만들기 public static int solution(int[] queue1, int[] queue2) { int answer = 0; int length = queue1.length; long queue1Sum = 0; // queue1의 합계만을 사용할 것 long totalSum = 0; long targetSum = 0; Queue q1 = new LinkedList(); Queue q2 = new LinkedList(); queue1Sum = Arrays.stream(queue1).sum();.. 2022. 11. 27.
[Java] 프로그래머스 [Level-1] 성격 유형 검사하기 import java.util.*; public class Solution { //성격 유형 검사하기 static String[] typeArr = {"RT", "CF", "JM", "AN"}; public static String typeCheck(HashMap hm) { StringBuilder sb = new StringBuilder(); char firstChar; char secondChar; char typeChar; for (String s : typeArr) { // "RT", "CF", "JM", "AN" firstChar = s.charAt(0); // 'R', 'C', 'J', 'A' secondChar = s.charAt(1);// 'T', 'F', 'M', 'N' typeChar .. 2022. 11. 27.
[Java] 프로그래머스 [Level-4] 도둑질 두 번째 시도(시간 초과) public class Solution { // 도둑질 // 두 번째 시도(시간 초과) public static int getMaxMoneyExceptTargetIndex(int[] money, int targetIndex) { int[] dp = new int[money.length - 1]; // 제일 먼저 1을 뺐을 경우 2,3,1,4,2,5에서 개미전사 문제 int num1 = (targetIndex + 1) % (money.length); // 타겟 인덱스의 바로 다음 인덱스 int num2 = (targetIndex + 2) % (money.length); // 타겟 인덱스의 다음 다음 인덱스 int num3 = 0; dp[0] = money[num1]; dp[1] .. 2022. 11. 27.
[Java] 프로그래머스 [Level-4] [3차]파괴되지 않은 건물 public class Solution { // 파괴되지 않은 건물 public static int solution(int[][] board, int[][] skill) { int answer = 0; int[] arr = new int[skill[0].length]; int startRowNum = 0; int startColNum = 0; int endRowNum = 0; int endColNum = 0; int affectPoint = 0; for (int i = 0; i < skill.length; i++) { arr = skill[i]; startRowNum = arr[1]; startColNum = arr[2]; endRowNum = arr[3]; endColNum = arr[4]; affec.. 2022. 11. 27.
[Java] 프로그래머스 [Level-2] 주차 요금 계산 import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; public class Solution { // 주차 요금 계산 public static int calcMin(String time1, String time2) { String[] time1Arr = time1.split(":"); String[] time2Arr = time2.split(":"); int min = (Integer.parseInt(time2Arr[0]) * 60 + Integer.parseInt(time2Arr[1])) - (Integer.parseInt(time1Arr[0]) * 60 + Integer.parseInt(time1Arr[1.. 2022. 11. 27.