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

๋ฐ˜์‘ํ˜•

Algorithm

[Algortihm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜คํ”ˆ์ฑ„ํŒ…๋ฐฉ https://programmers.co.kr/learn/courses/30/lessons/42888 ๋ฌธ์ œ ์ ‘๊ทผ HashMap์„ ์ด์šฉํ•ด์„œ ์œ ์ €์•„์ด๋””์™€ ๋‹‰๋„ค์ž„์„ ์ €์žฅํ•˜๊ณ  list์— ์œ ์ € ์•„์ด๋””์™€ Enter or Leave๋ฅผ ์ €์žฅํ•œ ํ›„์— ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์œ ์ € ์•„์ด๋””์™€ ๋‹‰๋„ค์ž„์„ ๋งค์นญํ•ด์„œ ๋ฉ”์„ธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค. Code import java.util.HashMap; import java.util.ArrayList; class Solution { public String[] solution(String[] records) { String[] answer; HashMap users = new HashMap(); ArrayList messages = new ArrayList(); for(.. ๋”๋ณด๊ธฐ
[Algorithm/Java][๋ฐฑ์ค€] 2263 ํŠธ๋ฆฌ์˜ ์ˆœํšŒ [BOJ] 2263 ํŠธ๋ฆฌ์˜ ์ˆœํšŒ https://www.acmicpc.net/problem/2263 ๋ฌธ์ œ ์ ‘๊ทผ ์ธ์˜ค๋”์™€ ํฌ์ŠคํŠธ์˜ค๋” ์ˆœํšŒ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ํ”„๋ฆฌ์˜ค๋” ์ˆœํšŒ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ํฌ์ŠคํŠธ์˜ค๋”์—์„œ๋Š” ํŠธ๋ฆฌ์™€ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ณ , ์ธ์˜ค๋”์—์„œ๋Š” ํฌ์ŠคํŠธ์˜ค๋”์—์„œ ๊ตฌํ•œ ๋ฃจํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด์„œ ํ”„๋ฆฌ์˜ค๋” ์ˆœํšŒ๋ฅผ ๊ตฌํ•˜์˜€๋‹ค. Code import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BOJ2263 { static int n,idx = 0; static int[] inorder, preorder, postorder; .. ๋”๋ณด๊ธฐ
[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฌธ์ž์—ด ์••์ถ• [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฌธ์ž์—ด ์••์ถ• https://programmers.co.kr/learn/courses/30/lessons/60057 ๋ฌธ์ œ ์ ‘๊ทผ ๊ธด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ค„์ด๊ธฐ์œ„ํ•ด์„œ ๋ฌธ์ž์—ด์„ n๊ฐœ ๋‹จ์œ„๋กœ ์ž˜๋ผ์„œ ๊ฐ™์€ ๋ฌธ์ž์—ด str์ด n๋ฒˆ ๋ฐ˜๋ณต๋˜๋ฉด nstr๋กœ ํ‘œํ˜„ํ•˜์—ฌ ์••์ถ•ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. compressString ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์••์ถ•ํ•˜๋ ค๋Š” ๋ฌธ์ž์—ด s์™€ ์ž๋ฅด๋ ค๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด len์„ ๋ฐ›์•„์„œ s๋ฅผ len๋‹จ์œ„๋กœ ์ž˜๋ผ ์••์ถ•ํ•œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•˜์˜€๋‹ค. Code class Solution { public int solution(String s) { int answer = s.length(); for(int i = 1; is.length()){ cur = s.substring(i,s.length()); } else.. ๋”๋ณด๊ธฐ
[Algorithm/Java][๋ฐฑ์ค€] 1918 ํ›„์œ„ ํ‘œ๊ธฐ์‹ [BOJ] 1918 ํ›„์œ„ ํ‘œ๊ธฐ์‹ https://www.acmicpc.net/problem/1918 ๋ฌธ์ œ ์ ‘๊ทผ ์ž…๋ ฅ๋ฐ›์€ String์„ char ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•œํ›„ ํ•˜๋‚˜์”ฉ ์ ‘๊ทผํ•ด์„œ ์—ฐ์‚ฐ์ž๋Š” Stack์„ ์ด์šฉํ•ด ์ฒ˜๋ฆฌํ•˜๊ณ  StringBuilder์˜ append๋ฅผ ์ด์šฉํ•ด์„œ ์ด์–ด ๋ถ™์—ฌ์ฃผ์—ˆ๋‹ค. ์ค‘์œ„ํ‘œ๊ธฐ์‹์—์„œ ํ›„์œ„ํ‘œ๊ธฐ์‹์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ, Stack์„ ์ด์šฉํ•˜๋Š”๋ฐ ํ˜„์žฌ ์—ฐ์‚ฐ์ž๋ณด๋‹ค stack์— top์— ์žˆ๋Š” ์—ฐ์‚ฐ์ž์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๋‹ค๋ฉด postfix์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. '*'์™€ '/'๋ฅผ 2, '+' ์™€ '-'๋ฅผ 1, '('์„ 0์œผ๋กœ ์„ค์ •ํ•ด์„œ ์œ„์— ๋ฐฉ์‹์„ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ–ˆ๋‹ค. ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ 4๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์˜€๋‹ค. 1.A~Z ์‚ฌ์ด์— ํ”ผ์—ฐ์‚ฐ์ž์ผ ๊ฒฝ์šฐ postfix์— ์ถ”๊ฐ€ํ•œ๋‹ค. 2.'('์ธ ๊ฒฝ์šฐ Stack์— pushํ•œ๋‹ค. 3.').. ๋”๋ณด๊ธฐ
[Algorithm/Java][๋ฐฑ์ค€] 1865 ์›œํ™€ [BOJ] 1865 ์›œํ™€ https://www.acmicpc.net/problem/1865 ๋ฌธ์ œ ์ ‘๊ทผ ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1.์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค. 2.์ตœ๋‹จ ๊ฒฝ๋กœ ํ…Œ์ด๋ธ”(๋ฐฐ์—ด)์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. 3.(์ •์ ์˜ ๊ฐœ์ˆ˜-1)๋ฒˆ๋™์•ˆ ๋ชจ๋“  ๊ฐ„์„ ์„ ํƒ์ƒ‰ํ•˜์—ฌ ์ตœ๋‹จ ๊ฒฝ๋กœ ํ…Œ์ด๋ธ”์„ ๊ฐฑ์‹ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์Œ์˜ ์ˆœํ™˜์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ์•Œ๊ณ ์‹ถ๋‹ค๋ฉด ํ•œ๋ฒˆ ๋” ๋ชจ๋“  ๊ฐ„์„ ์— ๋Œ€ํ•ด์„œ ํƒ์ƒ‰์„ ํ•˜๊ณ  ์ด๋•Œ, ์ตœ๋‹จ ๊ฒฝ๋กœ ํ…Œ์ด๋ธ”์ด ๊ฐฑ์‹ ์ด ๋œ๋‹ค๋ฉด ์Œ์˜ ์ˆœํ™˜์ด ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋‹ค. ์ด ๋ฌธ์ œ๋Š” ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•  ํ•„์š”๊ฐ€ ์—†์ด ์Œ์˜ ์ˆœํ™˜์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๋งŒ ์ฒดํฌํ•˜๋ฉด ๋˜๊ธฐ๋•Œ๋ฌธ์— 1๋ฒˆ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ๋งŒ ๋ฒจ๋งŒ ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ด๋•Œ, ์Œ์˜ ์ˆœํ™˜์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ๊ฒ€์‚ฌํ•˜์˜€๋‹ค. Code import java.io.BufferedReader; .. ๋”๋ณด๊ธฐ
[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํƒ€๊ฒŸ ๋„˜๋ฒ„ [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํƒ€๊ฒŸ ๋„˜๋ฒ„ https://programmers.co.kr/learn/courses/30/lessons/43165 ๋ฌธ์ œ ์ ‘๊ทผ dfs๋ฅผ ์ด์šฉํ•ด์„œ numbers[0] ๋ถ€ํ„ฐ numbers[numbers.length-1]๊นŒ์ง€ ๋ชจ๋“  ์ˆซ์ œ์—๋Œ€ํ•ด์„œ +,-ํ•œ ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•ด์„œ target์ด ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ–ˆ๋‹ค. Code class Solution { int answer; int[] nums; public int solution(int[] numbers, int target) { answer = 0; nums = numbers; dfs(0, 0, target); return answer; } public void dfs(int num, int cnt, int target){ if(cnt == nums... ๋”๋ณด๊ธฐ
[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‹ค์Œ ํฐ ์ˆซ์ž [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‹ค์Œ ํฐ ์ˆซ์ž https://programmers.co.kr/learn/courses/30/lessons/12911 ๋ฌธ์ œ ์ ‘๊ทผ ์ฒ˜์Œ์— ์ ‘๊ทผํ•  ๋•Œ๋Š” Integer.toBinaryString() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ stringํ˜•ํƒœ์˜ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ณ , n+1๋ถ€ํ„ฐ 1์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™์„ ๋•Œ๊นŒ์ง€ while๋ฌธ์„ ๋Œ๋ ธ๋‹ค. ๋‹ค ํ’€๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ๊นŒ Integer.countBit()๋ฅผ ์ด์šฉํ•˜๋ฉด 1์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ์–ด์„œ ๋” ์‰ฝ๊ฒŒ ํ’€์—ˆ๋‹ค. Code Integer.toBinaryString()์ด์šฉํ•œ ํ’€์ด class Solution { public int solution(int n) { String binaryNum = Integer.toBinaryString(n); int .. ๋”๋ณด๊ธฐ
[Algorithm/Java][๋ฐฑ์ค€] 1167 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ [BOJ] 1167 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ https://www.acmicpc.net/problem/1167 ๋ฌธ์ œ ์ ‘๊ทผ ์ด ๋ฌธ์ œ๋Š” ์•„๋ž˜ ๋ฌธ์ œ์™€ ๊ฑฐ์˜ ๋˜‘๊ฐ™์€ ๋ฌธ์ œ์ด๋‹ค. ํŠธ๋ฆฌ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ๋Š” ๋ฐฉ์‹๋งŒ ๋‹ค๋ฅด๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋˜‘๊ฐ™์€ ๋ฌธ์ œ์—ฌ์„œ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. 1967 ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ Code import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class BOJ1167 { static class Node{ int to; int dist; public Node(int to, int dist) { th.. ๋”๋ณด๊ธฐ