알고리즘
-
프로그래머스 - 대충 만든 자판알고리즘 2024. 5. 7. 19:08
class Solution { public int[] solution(String[] keymap, String[] targets) { int[] answer = new int[targets.length]; for(int i = 0; i 각 targes에서 각 문자열의 문자를 구하고, keymap에서 배열의 원소마다 비교하여 charAt을 이용해 최소 클릭 횟수를 구하여 sum에 더해 키를 누른 횟수를 구했습니다. 만약 문자가 keymap에 포함되어 있지않다면 -1를 반환해야하므로, boolean 타입을 이용하여 체크해 이를 확인했습니다.
-
프로그래머스 - 둘만의 암호알고리즘 2024. 5. 7. 18:29
class Solution { public String solution(String s, String skip, int index) { StringBuilder sb = new StringBuilder(); for(char c : s.toCharArray()) { int tmp = index; while(tmp > 0) { c++; if(c > 'z') c = 'a'; // z를 넘어가면 a if(skip.indexOf(c) == -1) tmp--; // 현재 문자가 skip에 포함되어 있지 않으면 감소 } ..
-
프로그래머스 - 숫자 짝꿍알고리즘 2024. 5. 6. 17:01
import java.util.*;class Solution { public String solution(String X, String Y) { List listX = new ArrayList(); List listY = new ArrayList(); List couple = new ArrayList(); StringBuilder sb = new StringBuilder(); for(char c : X.toCharArray()) listX.add(c); for(char c : Y.toCharArray()) listY.add(c); for(int i = 0; i 모든 수가 0 ..
-
프로그래머스 - 완주하지 못한 선수알고리즘 2024. 5. 2. 12:08
import java.util.*;class Solution { public String solution(String[] participant, String[] completion) { String answer = ""; Map map = new HashMap(); for (String player : participant) map.put(player, map.getOrDefault(player, 0) + 1); for(String player : completion) { if(map.get(player) == 1) map.remove(player); else { ..
-
프로그래머스 - 로또의 최고 순위와 최저 순위알고리즘 2024. 5. 2. 11:20
import java.util.*;class Solution { public int[] solution(int[] lottos, int[] win_nums) { int[] answer = new int[2]; int zero = 0, cnt = 0; // 0의 갯수, 맞춘 갯수 for(int i = 0; i 0의 갯수와 맞춘 갯수를 구해 최고 순위와 최저 순위를 구하는 문제입니다.하나도 맞추지 못 한거나 하나만 맞췄을 때 6위인 것만 예외로 구하면 어렵지 않은 문제였습니다.
-
프로그래머스 - 다트 게임알고리즘 2024. 5. 1. 15:58
import java.util.*;class Solution { public int solution(String dartResult) { int answer = 0; List scores = new ArrayList(); StringBuilder sb = new StringBuilder(); int score = 0; for(char c : dartResult.toCharArray()) { if(Character.isDigit(c)) { // 숫자일 때 점수 구하기 sb.append(c); } else if (c == 'S' || c == 'D'||..
-
프로그래머스 - 실패율알고리즘 2024. 4. 29. 18:24
import java.util.*;class Solution { public int[] solution(int N, int[] stages) { int[] challenge = new int[N + 1]; // 도전한 사람 수 double[] fail = new double[N + 1]; // 실패율 int num = stages.length; // 총 사용자 수 Map map = new HashMap(); for(int stage : stages) { for(int i = 0; i result = new ArrayList(map.keySet()); // 내림차순 정렬 ..