[프로그래머스, Java] 타겟 넘버
·
CodingTest/Programmers
⭐ 아이디어💡풀이 1- 재귀 풀이class Solution { public int solution(int[] numbers, int target) { return sumCount(numbers, target, 0, 0); } int sumCount(int[] numbers, int target, int index, int sum) { if(index == numbers.length) { if(sum == target) { return 1; } return 0; } return sumCount(numbers, target, index + 1, sum + ..
[프로그래머스, Java] 단어 변환
·
CodingTest/Programmers
⭐ 아이디어모두를가지고~~ -> dfs (stack)가능한 최소의 경우 -> bfs (queue)begin hittarget cogwords 안에 target이 없으면 return 0(begin, count=0)을 stack에 넣고 시작while(!isEmpty)꺼낸값과 하나만 차이나는 단어 찾기 -> 어떻게 최적화? target의 index로 분기하기 현재 target(i) 와 pop(i)가 같으면 넘어가 다르다면 -> pop(i)를 target(i)로 변경한 전체 문자를 words에서 찾기 -> 찾았다면 해당 단어로 변경하고 count+1 하기 -> target과 같다면 answer 와 count를 크기비교하여 작은 값을 저장하기 ..
[프로그래머스, Java] 게임 맵 최단거리
·
CodingTest/Programmers
💡풀이 1import java.util.ArrayDeque;import java.util.Queue;class Solution { public int solution(int[][] maps) { int sizeX = maps.length; // x 길이 int sizeY = maps[0].length; // y 길이 //sizeX*sizeY 배열 visited 만들기 boolean[][] visited = new boolean[sizeX][sizeY]; //분기 - bfs Queue queue = new ArrayDeque(); queue.offer(new int[]{0, 0}); while(!qu..
[프로그래머스, Java] 네트워크
·
CodingTest/Programmers
💡풀이 1- node 클래스를 만들어서 풀이import java.util.ArrayDeque;import java.util.ArrayList;import java.util.Deque;import java.util.List;class Solution { class Node { String name; List links = new ArrayList(); boolean visited = false; Node(String name) { this.name = name; } private List getLinks() { return this.links; } private..
[프로그래머스, Java] 의상
·
CodingTest/Programmers
⭐풀이 아이디어1. clothes의 각 행은 [의상의 이름, 의상의 종류]- 맵에 종류별로 저장하자2. 예를 들어보자 - 모자1, 바지1, 상의1 로 나올수 있는 경우의 수 모자1 바지1 상의1 모자1 바지1 모자1 상의1 바지1 상의1 모자1 바지1 상의1 총 7가지 왜 7가지가 나왔는가? 1. 각각의 개수의 +1을 해준다 - 모자1을 고른경우, 모자1을 고르지 않은경우 -> 2개 2. +1을 한 모든 값을 곱해준다 2*2*2 = 8 3. 모두 고르지 않은 경우를 고려해 -1 해준다 8-1 = 7💡풀이 1import java.util.*;class Solution { public int solution(String[][] clothes) { Map map = new HashMap(..
[프로그래머스, Java] 달리기 경주
·
CodingTest/Programmers
⭐아이디어배열에서 매번 해당 맴버의 번호를 찾으면 너무 느림1. 미리 map에 해당 맴버의 등수를 저장해둔다2. callings를 분기한다3. 해당 멤버의 위치를 map에서 찾는다4. 해당 멤버의 앞에 위치한 멤버의 위치도 map에서 찾는다.5. 배열에서 서로의 위치를 변경시킨다6. map에 저장된 위치값도 서로 바꾼다.💡풀이 1import java.util.HashMap;import java.util.Map;class Solution { public String[] solution(String[] players, String[] callings) { Map rank = new HashMap(); for(int i = 0; i GitHub - okjunghyeon/P..