๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋ฐ˜์‘ํ˜•

Algorithm

[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋” ๋งต๊ฒŒ [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋” ๋งต๊ฒŒ https://programmers.co.kr/learn/courses/30/lessons/42626 ๋ฌธ์ œ์ ‘๊ทผ ์ฃผ์–ด์ง„ ์Œ์‹ ๋ฐฐ์—ด์—์„œ ์Šค์ฝ”๋นŒ์ง€์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ๋‘ ์Œ์‹์„ ์„ž์–ด์„œ ๊ธฐ์ค€์ธ K๋ฅผ ๋„˜๊ฒจ์•ผ ํ•œ๋‹ค. ๋‘ ์Œ์‹์„ ์„ž์€ ๊ฒฐ๊ณผ๋„ ๋‹ค์‹œ ์Œ์‹ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ด์„œ ์—ฐ์‚ฐ์„ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ณ„์†ํ•ด์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œํž™์„ ์‚ฌ์šฉํ–ˆ๋‹ค. Code import java.util.PriorityQueue; class Solution { public int solution(int[] scoville, int K) { PriorityQueue minHeap = new PriorityQueue(); for(int food: scoville) minHeap.add(food); int answer =.. ๋”๋ณด๊ธฐ
[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ธฐ๋Šฅ๊ฐœ๋ฐœ [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ธฐ๋Šฅ๊ฐœ๋ฐœ https://programmers.co.kr/learn/courses/30/lessons/42586 ๋ฌธ์ œ์ ‘๊ทผ ๊ฐ ๊ธฐ๋Šฅ๋งˆ๋‹ค ๋‚จ์€ ์ผ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์ฃผ๊ณ , ์•ž ๊ธฐ๋Šฅ์˜ ๋‚จ์€ ์ผ์ˆ˜๋ณด๋‹ค ์ž‘์€ ์ผ์ˆ˜๋ฅผ ๊ฐ€์ง„ ๊ธฐ๋Šฅ๋งˆ๋‹ค count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์„œ ํ•œ ๋ฐฐํฌ ๋‹จ๊ณ„์— ํฌํ•จ๋˜๋„๋ก ์ ‘๊ทผํ–ˆ๋‹ค. Code import java.util.ArrayList; class Solution { public int[] solution(int[] progresses, int[] speeds) { int[] remainDays = new int[progresses.length]; //๊ฐ ๊ธฐ๋Šฅ์„ ์™„๋ฃŒํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ผ ์ €์žฅ for(int i = 0; i ๋”๋ณด๊ธฐ
[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์นด์นด์˜คํ”„๋ Œ์ฆˆ ์ปฌ๋Ÿฌ๋ง๋ถ [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์นด์นด์˜คํ”„๋ Œ์ฆˆ ์ปฌ๋Ÿฌ๋ง๋ถ https://programmers.co.kr/learn/courses/30/lessons/1829 ๋ฌธ์ œ์ ‘๊ทผ BFS๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ™์€ ์ƒ‰์ƒ์„ ๊ฐ€์ง„ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์„ ํƒ์ƒ‰ํ•˜๊ณ  ์˜์—ญ๊ณผ ์˜์—ญ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ขŒํ‘œ๊ฐ’๊ณผ ํ•ด๋‹น ์ขŒํ‘œ์˜ ์ƒ‰์ƒ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ XYํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ Queue๋ฅผ ์ด์šฉํ•  ๋•Œ ํŽธํ•˜๋„๋ก ํ•˜์˜€๋‹ค. Code import java.util.LinkedList; import java.util.Queue; class Solution { class XY { int x; int y; int value; // ํ•ด๋‹น ์ขŒํ‘œ์˜ ๊ทธ๋ฆผ XY(){}; XY(int x, int y, int value){ this.x = x; this.y = y; this.value = value.. ๋”๋ณด๊ธฐ
[Algorithm/Java][๋ฐฑ์ค€] 1676๋ฒˆ ํŒฉํ† ๋ฆฌ์–ผ 0์˜ ๊ฐœ์ˆ˜ [๋ฐฑ์ค€] 1676๋ฒˆ ํŒฉํ† ๋ฆฌ์–ผ 0์˜ ๊ฐœ์ˆ˜ https://www.acmicpc.net/problem/1676 ๋ฌธ์ œ์ ‘๊ทผ ์ฒ˜์Œ์—๋Š” ๊ทธ๋ƒฅ ํŒฉํ† ๋ฆฌ์–ผ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋‹จ์ˆœํ•˜๊ฒŒ 0์˜ ๊ฐœ์ˆ˜๋ฅผ ์…€๋ ค๊ณ  ํ–ˆ๋‹ค. ๊ทผ๋ฐ ์ž…๋ ฅ์˜ ๋ฒ”์œ„๊ฐ€ 500๊นŒ์ง€ ์˜€๊ณ  500!๋Š” long์œผ๋กœ๋„ ์ปค๋ฒ„๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๋ฒ”์œ„์˜€๋‹ค. ๊ทธ๋ž˜์„œ ๊ฒ€์ƒ‰์„ ํ•ด๋ณธ ๊ฒฐ๊ณผ!@! ๋งจ ๋’ค์—์„œ ์—ฐ์†๋˜๋Š” 0์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ผ๋‹ค๋Š” ๊ฒƒ์€ 2 * 5 ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋ช‡๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€์™€ ๊ฐ™๋‹ค!!๋ผ๋Š” ์—„์ฒญ๋‚œ ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ ๋˜์–ด์„œ ์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค....๋Œ€๋ฐ•... Code import java.util.Scanner; public class BOJ1676 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); .. ๋”๋ณด๊ธฐ
[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 2022 KAKAO BLIND RECRUITMENT ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ https://programmers.co.kr/learn/courses/30/lessons/92334 ๋ฌธ์ œ์ ‘๊ทผ ์ผ๋‹จ ๋ฌธ์ œ๊ฐ€ ๊ณ ๋ คํ•ด์•ผ ํ•  ๊ฒƒ๋“ค์ด ๋งŽ์•„์„œ ์ฒ˜์Œ์—๋Š” ์‹ ๊ณ  ๋‹นํ•œ ํšŸ์ˆ˜, ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ, ์•Œ๋ฆผ ๋ฉ”์ผ์„ ๋ฐ›์„ ํšŸ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ฐ๋กœ map์„ ์ด์šฉํ•ด์„œ 3๊ฐœ์˜ map์„ ๋งŒ๋“ค์–ด์„œ ์ ‘๊ทผํ–ˆ๋Š”๋ฐ ๋„ˆ๋ฌด ๋ณต์žกํ•ด์„œ ๊ทธ๋ƒฅ 3๊ฐœ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ User ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ๋” ์ƒ๊ฐํ•˜๊ธฐ ์‰ฌ์› ๊ณ  ๋‚ด๊ฐ€ ๋Š๋ผ๊ธฐ์—๋Š” ๋ณต์žกํ•จ์ด ๋งŽ์ด ์ค„์–ด๋“ค์—ˆ๋‹ค. Code import java.util.HashMap; import java.util.HashSet; class Solution { class User { HashSet report; // ์‹ ๊ณ ํ•œ .. ๋”๋ณด๊ธฐ
[Algorithm/Java][LeetCode] 121. Best Time to Buy and Sell Stock [LeetCode] 121. Best Time to Buy and Sell Stock https://leetcode.com/problems/best-time-to-buy-and-sell-stock ๋ฌธ์ œ์ ‘๊ทผ ๊ฐ€์žฅ ์‹ธ๊ฒŒ ์‚ฌ์„œ ๊ฐ€์žฅ ๋น„์‹ธ๊ฒŒ ์ƒ€์„ ๋•Œ ์–ป๋Š” ์ด์œค์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ด์šฉํ•ด์„œ dp๋ฐฐ์—ด์— ์ง€๊ธˆ๊นŒ์ง€ ์ค‘์—์„œ ๊ฐ€์žฅ ์‹ผ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ํ˜„์žฌ ๋‚ ์งœ์˜ ๊ฐ€๊ฒฉ prices[i]์™€ ๊ทธ ์ „๋‚ ๊นŒ์ง€ ๊ฐ€์žฅ ์‹ผ ๊ฐ€๊ฒฉ dp[i-1]์„ ๋นผ์„œ ์ด์œค์„ ๊ตฌํ•œ ๋‹ค์Œ max์™€ ๋น„๊ตํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜ค๋Š˜ ๊ฐ€๊ฒฉ์ด dp[i-1]๋ณด๋‹ค ์‹ธ๋‹ค๋ฉด dp[i]๋ฅผ ์˜ค๋Š˜ ๊ฐ€๊ฒฉ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ์•„๋‹ˆ๋ผ๋ฉด dp[i-1]์„ ๋„ฃ์–ด์ค€๋‹ค. Code class Solution { public int maxProfit(int[] prices) { int max.. ๋”๋ณด๊ธฐ
[Algorithm/Java][LeetCode] 101. Symmetric Tree [LeetCode] 101. Symmetric Tree https://leetcode.com/problems/symmetric-tree ๋ฌธ์ œ ์ ‘๊ทผ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๊ฐ€ ๋Œ€์นญ์ธ์ง€๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. BFS์ฒ˜๋Ÿผ ํ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ’€๋ ค๊ณ  ํ–ˆ์œผ๋‚˜, ๋ญ”๊ฐ€ ์ž˜ ์•ˆ๋˜์„œ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ–ˆ๋‹ค. ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋ฅผ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋น„๊ต ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜ 4๊ฐ€์ง€ 1. ์–‘์ชฝ ๋…ธ๋“œ๊ฐ€ ๋ชจ๋‘ null์ธ ๊ฒฝ์šฐ(์•„๋ฌด ๋ฌธ์ œ ์—†์ด ๋๊นŒ์ง€ ํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒ ํ—€์œผ๋ฏ€๋กœ true) 2. ํ•œ์ชฝ ๋…ธ๋“œ๋งŒ null์ธ ๊ฒฝ์šฐ(๋น„๋Œ€์นญ์ด๋ฏ€๋กœ false) 3. ์–‘์ชฝ ๋…ธ๋“œ์˜ ๊ฐ’์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ(๋น„๋Œ€์นญ์ด๋ฏ€๋กœ false) 4. ์–‘์ชฝ ๋…ธ๋“œ์˜ ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ(์ง€๊ธˆ๊นŒ์ง€๋Š” ๋Œ€์นญ์ด๋ฏ€๋กœ ์ถ”๊ฐ€์ ์ธ ํŠธ๋ฆฌ ์ˆœํšŒ - ์žฌ๊ท€ ํ•จ์ˆ˜.. ๋”๋ณด๊ธฐ
[Algorithm/Java][๋ฐฑ์ค€] 2805๋ฒˆ ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ [๋ฐฑ์ค€] 2805๋ฒˆ ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ https://www.acmicpc.net/problem/2805 ๋ฌธ์ œ์ ‘๊ทผ ์ฒ˜์Œ์—๋Š” ๊ฐ์ด ์•ˆ์žกํ˜€์„œ ๋ฐ‘์— ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„๋ฅ˜๊ฐ€ ์ด๋ถ„ ํƒ์ƒ‰์ด๋ผ๊ณ  ์จ์ ธ์žˆ์–ด์„œ, ์˜›๋‚ ์— ๋ฐฐ์šด ๊ธฐ์–ต์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. left, right, mid๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ์ฐพ์•˜๋‹ค. ๋งŒ์•ฝ ๋”ฑ ๋–จ์–ด์ง€๋Š” ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ, ์ ์–ด๋„ m์„ ๋„˜๊ฒจ์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— right๊ฐ’์„ returnํ•˜๋„๋ก ํ–ˆ๋‹ค. Code import java.util.Arrays; import java.util.Scanner; public class BOJ2805 { static int cutHeight(int[] trees, int m){ Arrays.sort(trees); int left = 0; int right = trees[trees.length.. ๋”๋ณด๊ธฐ