알고리즘
-
프로그래머스 - 최소직사각형알고리즘 2022. 6. 30.
내 풀이 import java.util.ArrayList; import java.util.Collections; import java.util.List; public class P_33 { public int solution(int[][] sizes) { // 최소직사각형 int a = 0; // 가로 int b = 0; // 세로 List width = new ArrayList(); List height = new ArrayList(); for(int i=0; i max) { max = paramMax; } if (paramMin > min) { min = paramMin; } } return max * min; } } 2차원 배열의 향상된 for문 사용법 : 배열 하나를 선언해서 돌린다. Math의 ..
-
프로그래머스 - 부족한 금액 계산하기알고리즘 2022. 6. 29.
내 풀이 public class P_32 { public long solution(int price, int money, int count) { // 부족한 금액 계산하기 long answer = -1; long cal_price = price; for(int i=2; i= 0) { answer = 0; } return answer; } public static void main(String[] args) { P_32 p = new P_32(); int price = 3; int money = 20; int count = 4; System.out.println(p.solution(price, money, count)); } } 간단한 문제지만 주의할 점 int형으로 계산을 돌리면 테스트19 ~ 22가 오..
-
프로그래머스 - 다트 게임 [2018 KAKAO BLIND RECRUITMENT]알고리즘 2022. 6. 28.
내 풀이 public class P_30 { public int solution(String dartResult) { // dart game int answer = 0; char [] arr = dartResult.toCharArray(); int [] round = new int[3]; // 3개의 라운드 int idx = -1; for(int i=0; i 0) { round[idx-1] *= 2; round[idx] *= 2; } else if(idx
-
프로그래머스 - 가운데 글자 가져오기알고리즘 2022. 6. 26.
내 풀이 public class P_31 { public String solution(String s) { String answer = ""; String[] list = s.split(""); if(list.length % 2 == 1) { answer = list[list.length/2]; } else { answer = list[list.length/2-1] + list[list.length/2]; } return answer; } public static void main(String[] args) { // 가운데 글자 가져오기 P_31 p = new P_31(); String s = "qwer"; System.out.println(p.solution(s)); } } 단순하게 문제에서 요구하는 ..
-
프로그래머스 - 문자열 내 p와 y의 개수알고리즘 2022. 6. 9.
내 풀이 public class P_26 { boolean solution(String s) { boolean answer = false; int p = 0; int y = 0; String [] x = s.split(""); for(int i=0; i 'P'== e).count() == s.chars().filter( e -> 'Y'== e).count(); } } s를 전부 대문자로 만들어서 통일 시킴 (소문자로 해도 됨) chars() 메소드로 전부 char형태로 바꾼후 람다의 filter를 이용해서 매개변수 e가 P와 같다면 카운팅을 함 이 결과값이 P를 구한 과정과 같은 Y를 카운팅한 값과 같다면 true를 반환함.