본문 바로가기

알고리즘21

체육복 package test; import java.util.*; class Solution { public int solution(int n, int[] lost, int[] reserve) { int answer = n - lost.length; Arrays.sort(lost); Arrays.sort(reserve); // 여벌 체육복을 가져온 학생이 도난당한 경우 for(int i=0; i 2022. 9. 27.
소수 중 최대값 / 일반수 중 최소값 시험일 때 너무 긴장하지 말자. 머리가 굳어버린다. 특정구간에서 문제가 발생하면 침착하게 그 특정구간 문제에만 집중하자. step by step package test2; import java.util.*; public class Solution2 { public String solution(String[] s) { String answer = ""; ArrayList sosumax = new ArrayList(); ArrayList nososumin = new ArrayList(); int[] a = new int[s.length]; for(int i=0; i 2022. 9. 27.
소수 찾기_프로그래머스 [필요개념] > isprime 함수의 원리와 사용법 아래는 잘한 사람 코드임 class NumOfPrime { int numberOfPrime(int n) { int count = 0; int result = 0; for(int i = 1 ; i 2022. 9. 27.
K번째 수_프로그래머스 [필요개념] > Arrays.copyOfRange import java.util.Arrays; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; for(int i=0; i 2022. 9. 27.
최대공약수와 최소공배수_프로그래머스 [필수개념] > 유클리드 호제법을 알고 있어야함 class Solution { public int[] solution(int n, int m) { int[] answer = new int[2]; int minnum = Math.min(n,m); int maxnum = Math.max(n,m); answer[0] = gcd(maxnum,minnum); answer[1] = maxnum*minnum/answer[0]; return answer; } public int gcd(int a, int b){ if(a%b ==0){ return b; } return gcd(b, a%b); } } public class Hello { public static void main(String[] args) { Soluti.. 2022. 9. 27.
신규아이디 추천_프로그래머스 [필수개념] > 정규화, 꼭 알아야함 class Solution { public String solution(String new_id) { String answer = ""; String temp = new_id.toLowerCase(); //^ : 앞의 문자들 제외 // 제외하고 모두 ""으로 처리하겠다 temp = temp.replaceAll("[^-_.a-z0-9]",""); //2개 이상의 .들을 모두 제외 temp = temp.replaceAll("[.]{2,}","."); //첫자리 끝자리 . 들 모두 제외 temp = temp.replaceAll("^[.]|[.]$",""); //문자열 공백이면 "a"추가 if(temp.equals("")) temp+="a"; //문자열 16이상이면 15까지.. 2022. 9. 27.