본문 바로가기

알고리즘/문제 풀이 (출처: 프로그래머스)103

[JAVA] 붕대 감기 https://school.programmers.co.kr/learn/courses/30/lessons/250137 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr class Solution { public int solution(int[] bandage, int health, int[][] attacks) { // 현재 체력 int healthNow = health; // 몬스터의 마지막 공격이 끝나는 시간 int lastTime = attacks[attacks.length-1][0]; // 다음 공격 int nextAttack = 0; // 붕대 감기 .. 2024. 2. 24.
[JAVA/DFS]단어 변환 문제 출처 : https://programmers.co.kr/learn/courses/30/lessons/43163 코딩테스트 연습 - 단어 변환 두 개의 단어 begin, target과 단어의 집합 words가 있습니다. 아래와 같은 규칙을 이용하여 begin에서 target으로 변환하는 가장 짧은 변환 과정을 찾으려고 합니다. 1. 한 번에 한 개의 알파벳만 바꿀 수 programmers.co.kr class Solution { int answer = Integer.MAX_VALUE; boolean[] converted; public int solution(String begin, String target, String[] words) { converted = new boolean[words.leng.. 2024. 2. 24.
[JAVA/DFS] 여행경로 https://school.programmers.co.kr/learn/courses/30/lessons/43164 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr import java.util.*; class Solution { private String SPACE = " "; List travelList = new ArrayList(); boolean[] ticketUsed; int ticketCnt; public String[] solution(String[][] tickets) { ticketCnt = tickets.length; ticketUsed .. 2024. 2. 24.
[JAVA/DFS] 네트워크 https://school.programmers.co.kr/learn/courses/30/lessons/43162 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr class Solution { int answer = 0; boolean[] visited; public int solution(int n, int[][] computers) { visited = new boolean[n]; for(int i=0; i 2024. 2. 23.
[JAVA/BFS] 가장 먼 노드 https://school.programmers.co.kr/learn/courses/30/lessons/49189 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr import java.util.*; class Solution { public int solution(int n, int[][] edge) { int answer = 0; // edge와 방문 기록 저장 Map edgeMap = new HashMap(); boolean[] visited = new boolean[n+1]; // edgeMap 초기화 for(int i=1; i 2024. 2. 23.
[JAVA] 과제 진행하기 https://school.programmers.co.kr/learn/courses/30/lessons/176962 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr import java.util.*; class Solution { public String[] solution(String[][] plans) { String[] answer = new String[plans.length]; // 다루기 편하게 클래스로 변경 Homework[] homeworks = new Homework[plans.length]; for(int i=0; i< plans.leng.. 2024. 2. 23.
[JAVA] 디펜스 게임 https://school.programmers.co.kr/learn/courses/30/lessons/142085 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr import java.util.PriorityQueue; import java.util.Collections; class Solution { public int solution(int n, int k, int[] enemy) { int answer = 0; // 막은 적들을 내림차순으로 저장 PriorityQueue pq = new PriorityQueue(Collections.reverseOr.. 2024. 2. 22.
[JAVA/DP] 연속 펄스 부분 수열의 합 https://school.programmers.co.kr/learn/courses/30/lessons/161988# 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr class Solution { public long solution(int[] sequence) { // 16, 19, 20이 계속 에러가 나서 왜그런가 하고 봤더니 자료형을 int가 아니라 long으로 하면 해결됨 // https://school.programmers.co.kr/questions/45489 // +1을 먼저 곱할때와, -1을 먼저 곱할때의 경우의 수 // 사실 굳이 이렇게할 .. 2024. 2. 22.
[JAVA/BFS] 석유 시추 https://school.programmers.co.kr/learn/courses/30/lessons/250136 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr import java.util.*; class Solution { // 각 열별로 얻을 수 있는 석유의 총양 int[] total; // 탐색 여부 private boolean[][] visited; // 정답 private int max = 0; public int solution(int[][] land) { // 입력된 land의 크기에 따라 필요한 배열 크기 지정 total = new in.. 2024. 2. 22.
[JAVA] 마법의 엘리베이터 https://school.programmers.co.kr/learn/courses/30/lessons/148653# 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr class Solution { public int solution(int storey) { return elevator(storey); } public int elevator(int storey) { if(storey 2023. 4. 23.
[JAVA] 가장 큰 수 https://school.programmers.co.kr/learn/courses/30/lessons/42746 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 문제 설명 0 또는 양의 정수가 주어졌을 때, 정수를 이어 붙여 만들 수 있는 가장 큰 수를 알아내 주세요. 예를 들어, 주어진 정수가 [6, 10, 2]라면 [6102, 6210, 1062, 1026, 2610, 2106]를 만들 수 있고, 이중 가장 큰 수는 6210입니다. 0 또는 양의 정수가 담긴 배열 numbers가 매개변수로 주어질 때, 순서를 재배치하여 만들 수 있는 가장 큰 수를 문.. 2023. 4. 19.
[JAVA] 기지국 설치 https://school.programmers.co.kr/learn/courses/30/lessons/12979 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr class Solution { public int solution(int n, int[] stations, int w) { int answer = 0; // 순번 지정 int location = 1; int stationsLocation = 0; while(location 2023. 4. 19.