본문 바로가기

Algorithm

(177)
백준 11047 - 동전 0, 문제풀이, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy, baekjoon, acmicpc 그리디 - 채울 값어치를 최대한으로 채울 수 있는 가치 코인을 최대 개수로 채워나간다. // Process // 1. 코인 개수 입력받는다. 만드려는 값어치를 입력받는다. 코인 개수 만큼의 코인들의 값어치들을 입력받는다. (각 값어치는 전부 배수 관계, 오름차순) // 2. 코인값어치 배열이 오름차순이므로, 뒤에서부터 만드려는 값어치가 채워질 때까지 반복한다. // 2.1. 현재 차례의 코인 가치보다 채워야할 값어치가 더 남아있는지 확인해서 // 2.1.1. 더 남아있으면 -> 채울수 있는만큼의 개수 세고, 채워야할 가치를 그만큼 줄인다. // 3. 센 개수를 반환한다. import java.util.Scanner; class CoinZero { public static void main(String[]..
백준 11399 - ATM, 문제풀이, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy, baekjoon, acmicpc // Process // 1. 인출할 인원, 각 인원이 인출에 걸리는 시간을 입력받는다. // 2. 인출에 걸리는 시간을 오름차순으로 정렬한다. // 3. 인원 전체 반복한다. // 3.1. 이전사람이 걸렸던 시간 + 해당 순서 사람이 걸리는 시간 = 추가될 시간 // 3.2. 전체 소요시간에 추가될 시간을 더한다. // 4. 전체 소요시간 반환한다. import java.io.IOException; import java.util.Scanner; import java.util.*; class Main { public static void main(String args[]) throws IOException { Scanner scanner = new Scanner(System.in); // 1. int n..
백준 그리디 - 설탕배달, 문제풀이, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy, baekjoon, acmicpc // Process // 1. Input n // 2. 5씩 배달할 수 있는 최대 횟수를 구한다. // 3. 만족할 때까지 반복한다. // 3.1. 최대횟수로 5씩 배달하고 남은 나머지가 3의 배수인지 확인한다. // 3.1.1. 3의 배수이면 -> 만족하고 끝, 5횟수와 3횟수를 더한 값 리턴 // 3.1.2. 아니면 -> 5의 최대횟수를 줄이고, 나머지에 5를 더한다. // 3.1.2.1. 이 때, 5의 최대횟수가 0보다 작으면 (5, 3으로 해결 할 수 없는 숫자이면) -> -1로 종료한다. import java.io.IOException; import java.util.Scanner; class Main { public static void main(String[] args) throws IOEx..
탐욕법 06 - 단속카메라, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy // Process // 1. Input routes (in, out) array // 2. Make routeList and sort using out position // 3. Iterate all // 3.1. Check if i's in position is before current picked out position // 3.1.1. If not -> change out position to i's out position, and add cameraCount // 4. Return cameraCount import java.util.*; class Solution { public int solution(int[][] routes) { int cameraCount = 0; // 2. List r..
탐욕법 05 - 섬 연결하기, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy 최소신장트리 (Minimum Spanning Tree, MST) 문제였는데, 처음보는거라서 Kruskal, Prim 방식 2가지가 있다고 해서 2가지로 풀어두었다. import java.util.*; // Kruskal mst // Process // 1. Input n, costs // 2. Sort using cost (ascending way) // 3. Iterate costs // 3.1. Check edge is valid or not // 3.1.1. If valid -> add cost to totalCost // 3.1.2. If not -> next // 4. Return totalCost class Solution { public int solution(int n, int[][] co..
탐욕법 04 - 구명보트, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy // Process // 1. Input // 2. Sort people in ascending way // 3. Iterate till front and rear index are met // 3.1. Check if front and rear index value can take the same boat // 3.1.1. If so -> move both indices // 3.1.2. If not -> move rear index // 3.2. ++boatCount // 4. Return boatCount import java.util.*; class Solution { public int solution(int[] people, int limit) { int boatCount = 0; int fr..
탐욕법 03 - 큰수만들기, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy // 문제에서 예를 들 때 전체 조합을 나열하고 가장 큰 것을 골라서 순서가 변해도 상관 없을 것 같으나, // 실제로 답은 순서는 건드리지 않고 찾아나가야함. // Process // 1. Input // 2. 결과 숫자를 만들 때 까지 반복한다. // 2.1. 결과 숫자를 만들 때 필요한 최소 길이를 남기고 서브스트링을 만든다. // 2.2. 서브스트링에서 가장 큰 숫자를 찾아서 더한다. 이 때, 해당 인덱스를 기억해서 다음번에 여기부터 서브스트링을 만든다. // 2.3. 가장 큰 수를 결과에 더해둔다. // 2.4. 인덱스를 바꾼다. // 3. 결과 반환한다. class Solution { public String solution(String number, int k) { StringBuilder ..
완전탐색 03 - 카펫, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, Heap, 힙, 정렬, sort, search # Process # 1. Input brown, yellow numbers # 2. Check the number of edges (brown) # 2.1. If so -> Check the number of inner sector (yellow) # 2.1.1. If so -> append to answer (width, height) # 3. Return valid size import math def solution(brown, yellow): answer = [] # 2. init_brown = math.ceil(brown / 2) end_brown = 3 for width_brown in range(init_brown, end_brown-1, -1): height_brown = (brown -..