[BOJ] Queue, DeQue/ 19๋‹จ๊ณ„
ยท
Hi๐Ÿ–๏ธ/Algorithm
1 18258 ํ 2 ์„ฑ๊ณต 7886 26220 30.958% ํ์˜ ๊ฐœ๋…์„ ์ตํžˆ๊ณ  ์‹ค์Šตํ•˜๋Š” ๋ฌธ์ œ. ์—ฐ์‚ฐ ๋‹น ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(1)์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š”. def push(num) : list.append(num) def pop() : if len(list) != 0 : print(list.popleft()) else : print(-1) def size() : print(len(list)) def empty() : if len(list) != 0 : print(0) else : print(1) def front() : if len(list) != 0 : print(list[0]) else : print(-1) def back() : if len(list) != 0 : print(list[-1]) e..
[BOJ] Stack / 18๋‹จ๊ณ„
ยท
Hi๐Ÿ–๏ธ/Algorithm
1 10828 ์Šคํƒ ์„ฑ๊ณต 41369 108817 38.494% ์Šคํƒ์˜ ๊ฐœ๋…์„ ์ตํžˆ๊ณ  ์‹ค์Šตํ•˜๋Š” ๋ฌธ์ œ def push(num): arrayList.append(num) def pop() : if len(arrayList) == 0 : print(-1) else : print(arrayList.pop()) def size() : print(len(arrayList)) def empty() : if len(arrayList) == 0 : print(1) else : print(0) def top() : if len(arrayList) == 0 : print(-1) else : print(arrayList[-1]) import sys arrayList = [] T = int(input()) for i in ran..
[BOJ] ์ •์ˆ˜๋ก  ๋ฐ ์กฐํ•ฉ๋ก  / 17๋‹จ๊ณ„
ยท
Hi๐Ÿ–๏ธ/Algorithm
์ž๋ฐ”๋กœ๋งŒ ํ’€๋‹ค๊ฐ€ ํŒŒ์ด์ฌ ๊ณต๋ถ€ํ• ๊ฒธ ์ด๋ฒˆ ๋‹จ๊ณ„์—์„œ๋Š” ํŒŒ์ด์ฌ์œผ๋กœ๋งŒ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. 1 5086 ๋ฐฐ์ˆ˜์™€ ์•ฝ์ˆ˜ ์„ฑ๊ณต์ถœ์ฒ˜๋‹ค๊ตญ์–ด๋ถ„๋ฅ˜ 7239 10516 70.288% ๋ฐฐ์ˆ˜์™€ ์•ฝ์ˆ˜๋ฅผ ๋ฐฐ์šฐ๋Š” ๋ฌธ์ œ def test(num01, num02) : if num02 % num01 == 0 : print("factor") elif num01 % num02 == 0: print("multiple") else : print("neither") while True : number01, number02 = map(int, input().split()) if number02 == 0 and number01==0 : break test(number01, number02) 2 1037 ์•ฝ์ˆ˜ ์„ฑ๊ณต๋ถ„๋ฅ˜ 12488 25527 49.747% ์•ฝ์ˆ˜์˜ ์„ฑ์งˆ..
[BOJ] Greedy Algorithm / 16๋‹จ๊ณ„
ยท
Hi๐Ÿ–๏ธ/Algorithm
1 11047 ๋™์ „ 0 ๋™์ „์˜ ์กฐ๊ฑด์ด ํŠน๋ณ„ํ•ด์„œ ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ import java.io.*; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException{ BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer bd = new StringTokenizer(bfr.readLine()); int T = Integer.parseInt(bd.nextToken()); int money = Integer.parseInt(bd.nextT..
[BOJ] dynamic programming / 14๋‹จ๊ณ„
ยท
Hi๐Ÿ–๏ธ/Algorithm
1 1003 ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜ ๋‹จ์ˆœ ์žฌ๊ท€๋กœ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ์™œ ๋Š๋ฆด๊นŒ์š”? ํ•จ์ˆ˜ ํ˜ธ์ถœ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์œผ๋กœ ๋Š˜์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(bfr.readLine()); for(int i=0; i
[BOJ] 15๋‹จ๊ณ„ / Greedy Algorithms
ยท
Hi๐Ÿ–๏ธ/Algorithm
1 11047 ๋™์ „ 0 ๋™์ „์˜ ์กฐ๊ฑด์ด ํŠน๋ณ„ํ•ด์„œ ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ import java.io.*; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException{ BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer bd = new StringTokenizer(bfr.readLine()); int T = Integer.parseInt(bd.nextToken()); int money = Integer.parseInt(bd.nextT..
๋™์ ๊ณ„ํš๋ฒ•(Dynamic Programming) / Memoization
ยท
Hi๐Ÿ–๏ธ/Algorithm
-๋™์ ๊ณ„ํš๋ฒ• (dynamic programming) ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ๊ฐ„๋‹จํ•œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์–ด์„œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•. ๋ถ€๋ถ„๋ฌธ์ œ๋ฐ˜๋ณต๊ณผ ์ตœ์ ๋ถ€๋ถ„๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•๋ณด๋‹ค ๋” ์ ์€ ์‹œ๊ฐ„๋‚ด์— ํ’€ ๋•Œ ์‚ฌ์šฉ. ๋ฌธ์ œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•˜์œ„ ๋ฌธ์ œ(subproblem)๋กœ ๋‚˜๋ˆ„์–ด ํ’ˆ. ์ด ํ•˜์œ„๋ฌธ์ œ๋“ค์„ ๊ฒฐํ•ฉํ•˜์—ฌ ์ตœ์ข…์ ์ธ ๋ฌธ์ œ์— ๋„๋‹ฌํ•˜๋Š” ๊ฒƒ. ๋ฉ”๋ชจ์ด์ œ์ด์…˜(memoization) ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋žจ์ด ๋™์ผํ•œ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•  ๋•Œ, ์ด์ „์— ๊ณ„์‚ฐํ•œ ๊ฐ’์€ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•ด์„œ ๋™์ผํ•œ ๊ณ„์‚ฐ์˜ ๋ฐ˜๋ณต ์ˆ˜ํ–‰์„ ์ œ๊ฑฐํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์†๋„๋ฅผ ๋น ๋ฅด๊ฒŒ ํ•˜๋Š” ๊ธฐ์ˆ . ๋™์  ๊ณ„ํš๋ฒ•์˜ ํ•ต์‹ฌ ๊ธฐ์ˆ . ์ฒ˜์Œ ๋ฐฉ๋ฌธํ•˜๋Š” ์žฌ๊ท€๋ฌธ์ œ๋ฉด ๊ณ„์‚ฐ์„ ํ†ตํ•ด ์–ป์€ ๊ฐ’์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ, ์ด ํ›„ ์žฌ๋ฐฉ๋ฌธ์„ ํ•˜๋ฉด ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ  ์ €์žฅ ๋œ ๊ฐ’ ์‚ฌ์šฉ. ex) import java.util.Sc..
[BOJ] BackTracking /13๋‹จ๊ณ„
ยท
Hi๐Ÿ–๏ธ/Algorithm
13 ๋ฐฑํŠธ๋ž˜ํ‚น ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฐฐ์›Œ ๋ด…์‹œ๋‹ค. 1 15649 N๊ณผ M (1) ๋ฐฑํŠธ๋ž˜ํ‚น ์ž…๋ฌธ ๋ฌธ์ œ 1 import java.util.Scanner; public class Main { public static int[] array; public static boolean[] visit; //๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ํ™•์ธ. default = False public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int M = scan.nextInt(); array = new int[M]; visit = new boolean[N]; dfs(N, M, 0); } public st..
Backtracking
ยท
Hi๐Ÿ–๏ธ/Algorithm
๋ฐฑํŠธ๋ž˜ํ‚น(=ํ‡ด๊ฐ๊ฒ€์ƒ‰) , ์ผ์ข…์˜ ํŠธ๋ฆฌ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜.ํ•ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ์ค‘์— ๋ง‰ํžˆ๋ฉด ๋‹ค์‹œ ์ด์ „์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ํ•ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ธฐ๋ฒ•์œผ๋กœ, ์ตœ์ ํ™” ๋ฌธ์ œ์™€ ๊ฒฐ์ • ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์‚ฌ์šฉ. ๋”๋ณด๊ธฐ- ๊นŠ์ด์šฐ์„ ํƒ์ƒ‰ (Depth First Search, DFS)- ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰ (Breadth First Search, BFS) - ์ตœ์„ ์šฐ์„ ํƒ์ƒ‰ (Best First Search/Heuristic Search) ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฒ€์ƒ‰ > DFS.ํŠธ๋ฆฌ์˜ ๊นŠ์ด๊ฐ€ ๋ฌดํ•œ๋Œ€์ด๋ฉด BFS DFS๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•จ. ์ผ์ผ์ด ํ•˜๋‚˜์”ฉ ์ฒดํฌํ•ด๋ณด๋ฉด์„œ ๋…ธ๊ฐ€๋‹คํ•˜๋Š” ํƒ์ƒ‰์ž„.๋ฐฑํŠธ๋ž˜ํ‚น์€ DFS ๋ฐฉ์‹๊ณผ ๋น„์Šทํ•œ๋ฐ, DFS์ฒ˜๋Ÿผ ํ•ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ์ค‘์—, ๋‹ค์Œ์— ๊ฐˆ ๊ฒฝ๋กœ๋กœ ํ•˜๋ฉด ํ•ด๊ฐ€ ์ ˆ๋Œ€ ์•ˆ๋‚˜์˜ฌ๊ฒƒ ๊ฐ™์œผ๋ฉด ๊ทธ ๊ฒฝ๋กœ๋Š” ๊ฐ€์ง€์•Š๊ณ  ๋˜๋Œ์•„๊ฐ.์ด๋ ‡๊ฒŒ ์กฐ๊ฑด์„ ์„ค์ •ํ•ด์„œ ์•ˆ๊ฐ€๋Š” ๊ฒฝ๋กœ..
Liky
'Hi๐Ÿ–๏ธ/Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก