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

๋ฐ˜์‘ํ˜•

java

[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.. ๋”๋ณด๊ธฐ
[Algorithm/Java][LeetCode] 70. Climbing Stairs [LeetCode] 70. Climbinh Stairs https://leetcode.com/problems/climbing-stairs/ ๋ฌธ์ œ์ ‘๊ทผ ์ฒ˜์Œ์—๋Š” ์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ณด๋ ค๊ณ  ํ•ด์„œ ์ œ์ถœ์„ ํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์™”๋‹ค. ๊ทธ๋ž˜์„œ DP ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด๋ณด์•˜๋‹ค. DP ๋ฐฉ์‹์œผ๋กœ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ ํ™”์‹?์„ ๋ฐœ๊ฒฌํ•ด์•ผ ํ•œ๋‹ค. ๋ญ”๊ฐ€ ๊ต‰์žฅํžˆ ์–ด๋ ค์›Œ ๋ณด์ด์ง€๋งŒ ๊ฐ‘์ž๊ธฐ ๊ฐ์ด ์žกํžŒ๋‹ค.(๊ทœ์น™์„ ์ฐพ๋Š” ๋Š๋‚Œ?) ๊ณ„๋‹จ์€ ํ•œ๋ฒˆ์— 1์นธ ๋˜๋Š” 2์นธ์„ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด 1๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ฌ๋ผ๊ฐ€๋ ค๋ฉด 0๋ฒˆ์งธ์—์„œ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  2๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ฌ๋ผ๊ฐ€๊ธฐ ์œ„ํ•ด์„œ๋Š” 0๋ฒˆ์งธ๊ฑฐ๋‚˜ 1๋ฒˆ์งธ ๊ณ„๋‹จ์—์„œ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค. ๋˜, 3๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ฌ๋ผ๊ฐ€๊ธฐ ์œ„ํ•ด์„œ๋Š” 1๋ฒˆ์งธ๊ฑฐ๋‚˜ 2๋ฒˆ์งธ ๊ณ„๋‹จ์—์„œ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ n๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๊ธฐ์œ„ํ•ด์„œ๋Š” n-1๋ฒˆ์งธ์™€ n-2๋ฒˆ์งธ .. ๋”๋ณด๊ธฐ
[Algorithm/Java][LeetCode] 53. Maximum subarray [LeetCode] 53. Maximum subarray https://leetcode.com/problems/maximum-subarray/ ๋ฌธ์ œ์ ‘๊ทผ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์—ฐ์†๋˜๋Š” ๊ฐ’์„ ๋”ํ•œ sum์ด ๊ฐ€์žฅ ํฐ subarray์˜ sum์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ฒ˜์Œ์— ์ด ๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ ๊ฒƒ ๊ฐ™์•„์„œ dp๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  dp[i]๋Š” i๋ฒˆ์งธ ์ธ๋ฑ์Šค๊นŒ์ง€ ์ค‘์— ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ํฐ subarray์˜ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ํ’€์—ˆ๋‹ค.(๋ฐฉ๋ฒ•1) ๊ทผ๋ฐ ๋ฐฉ๋ฒ•1๋กœ ํ’€๊ณ ๋ณด๋‹ˆ๊นŒ ๊ตณ์ด dp๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ผ์ผ์ด ๋‹ค ์ €์žฅํ•  ํ•„์š” ์—†์ด ํ˜„์žฌ์ธ๋ฑ์Šค ์ „์˜ subarray๊ฐ’(dp[i-1])๋งŒ ์•Œ๋ฉด ๋  ๊ฑฐ ๊ฐ™์•˜๋‹ค. ์ฆ‰ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  dp[i-1]์„ ์ €์žฅํ•˜๋Š” sum์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ dp[i-1] ์ด์ „ ๊ฐ’(dp[.. ๋”๋ณด๊ธฐ
[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ด์ƒํ•œ ๋ฌธ์ž ๋งŒ๋“ค๊ธฐ [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ด์ƒํ•œ ๋ฌธ์ž ๋งŒ๋“ค๊ธฐ https://programmers.co.kr/learn/courses/30/lessons/12930# ๋ฌธ์ œ์ ‘๊ทผ ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„๊ณ  ๋‚˜๋ˆ ์ง„ ๋‹จ์–ด๋“ค์„ ๊ธฐ์ค€์œผ๋กœ ์ง์ˆ˜๋Š” ๋Œ€๋ฌธ์ž๋กœ, ํ™€์ˆ˜๋Š” ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” ๋ฌธ์ œ์ด๋‹ค. splitํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋‹จ์–ด๋ฅผ ๋‚˜๋ˆ„๊ณ , ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ง์ˆ˜, ํ™€์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ๋Œ€๋ฌธ์ž,์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค. Code class Solution { public String solution(String s) { String[] words = s.split(" "); StringBuilder answer = new StringBuilder(); for(int i = 0; i answer.length()) // ์ž…๋ ฅ ๋ฌธ์ž์—ด ๋’ค์— ๊ณต๋ฐฑ์ด ๋” ์žˆ์„ ๊ฒฝ์šฐ answer... ๋”๋ณด๊ธฐ
[Algorithm/Java][BOJ] 3052๋ฒˆ ๋‚˜๋จธ์ง€ [๋ฐฑ์ค€] ๋ฐฑ์ค€ 3052๋ฒˆ ๋‚˜๋จธ์ง€ https://www.acmicpc.net/problem/3052 ๋ฌธ์ œ์ ‘๊ทผ 10๊ฐœ์˜ ์ž…๋ ฅ๋“ค์„ 42๋กœ ๋‚˜๋ˆ„์–ด์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋‚˜๋จธ์ง€ ๊ฐ’๋“ค์ด ๋ช‡ ๊ฐœ๊ฐ€ ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ๋ณด๊ณ  Set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. Set์€ ๋ฐ์ดํ„ฐ์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์ค‘๋ณต ๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€์•Š๊ณ  ๋‚˜๋จธ์ง€๋ฅผ add ํ•ด์ฃผ๊ณ  Set์˜ ํฌ๊ธฐ๋งŒ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค. Code import java.util.Scanner; import java.util.HashSet; public class Main { public static void main(String[] args) { HashSet hs = new HashSet(10); Scanner sc = n.. ๋”๋ณด๊ธฐ
[Algorithm/Java][BOJ] ๋ฐฑ์ค€ 2908๋ฒˆ ์ƒ์ˆ˜ [๋ฐฑ์ค€] 2908๋ฒˆ ์ƒ์ˆ˜ https://www.acmicpc.net/problem/2908 ๋ฌธ์ œ์ ‘๊ทผ ๋ฌธ์ž์—ด์„ ๋’ค์ง‘๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ StringBuilder์˜ reverse() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๊ณ , ์ž…๋ ฅ์ด ์„ธ์ž๋ฆฌ ์ˆ˜ ๋‘๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— BufferedReader๋ฅผ ์“ฐ์ง€์•Š๊ณ  ๊ทธ๋ƒฅ Scanner๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ์„ ๋ฐ›์•˜๋‹ค. Code import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String[] input = sc.nextLine().split(" "); int max = -1; for(String num: input){ StringBuilder.. ๋”๋ณด๊ธฐ