[프로그래머스, Java] 추억 점수
·
CodingTest/Programmers
💡풀이- HashMap 활용import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.Map;class Solution { public int[] solution(String[] name, int[] yearning, String[][] photo) { // 이름과 그리움 점수를 매핑하는 맵 생성 Map map = new HashMap(); // 이름 배열을 순회하며 맵에 이름과 그리움 점수 저장 for (int i = 0; i answer = new ArrayList(); // 각 사진에 대해 그리움 점수 계산 f..
[프로그래머스, Java] 비밀지도
·
CodingTest/Programmers
💡풀이import java.util.Arrays;class Solution { public String[] solution(int n, int[] arr1, int[] arr2) { // 두 배열을 각각 2진수 문자열로 변환 String[] secret1 = toSecret(arr1, n); String[] secret2 = toSecret(arr2, n); String[] answer = new String[n]; StringBuilder sb = new StringBuilder(); // 각 행마다 처리 for (int i = 0; i String.format(s, Integer.toString(str, ..
[프로그래머스, Java] 카드 뭉치
·
CodingTest/Programmers
💡풀이import java.util.ArrayDeque;import java.util.List;import java.util.Queue;class Solution { public String solution(String[] cards1, String[] cards2, String[] goal) { Queue q1 = new ArrayDeque(List.of(cards1)); Queue q2 = new ArrayDeque(List.of(cards2)); for (String word : goal) { if (!q1.isEmpty() && q1.peek().equals(word)) { q1.poll(); ..
[프로그래머스, Java] 명예의 전당 (1)
·
CodingTest/Programmers
❌풀이import java.util.ArrayList;import java.util.Collections;import java.util.List;class Solution { public int[] solution(int k, int[] score) { // list를 만들어서 값을 계속 집어넣는다 // index 개수가 k 보다 작다면 그냥 집어넣고 정렬 -> 마지막 숫자 result에 저장 // index 개수가 k 보다 크거나 같다면 정렬 -> 마지막 숫자 자르기 -> k번째 숫자 result에 저장 List list = new ArrayList(); List answer = new ArrayList(); for (in..
[프로그래머스, Java] 문자열 내 마음대로 정렬하기
·
CodingTest/Programmers
💡풀이 1- Comparator 람다식 활용import java.util.*;class Solution { public String[] solution(String[] strings, int n) { Arrays.sort(strings, (s1, s2)->{ int result = Character.compare(s1.charAt(n), s2.charAt(n)); if(result == 0){ result = s1.compareTo(s2); } return result; }); return strings; }}💡풀이 2- Comparator 메서드 ..
[프로그래머스, Java] 콜라 문제
·
CodingTest/Programmers
💡풀이class Solution { public int solution(int a, int b, int n) { // n개의 콜라는 a로 나눈다 20/2 = 10 // n 에는 n%a 만 남긴다. // 나눈 몫과 b를 곱하여 n에 더하고 total에도 더한다. // n 이 a보다 작아질때까지 반복한다. int total = 0; while (n >= a) { int count = n / a; n %= a; int newCoke = count * b; total += newCoke; n += newCoke; } ..