Algorithm

Algorithm/ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ (ํž™) - 11. ์ตœ๋Œ€ํž™

๋‚˜์˜ ํ’€์ด ์ตœ๋Œ€ํž™ heapq์—์„œ๋Š” ์ตœ๋Œ€ ํž™์„ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ€ํ˜ธ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์ตœ๋Œ€ ํž™์„ ๊ตฌํ˜„ํ•œ๋‹ค. ๋ถ€ํ˜ธ๋ฅผ ๋ฐ”๊ฟ”์„œ ์ตœ์†Œ ํž™์— ๋„ฃ์–ด์ค€ ๋’ค์— ์ตœ์†Ÿ๊ฐ’๋ถ€ํ„ฐ pop์„ ํ•ด์ค„ ๋•Œ ๋‹ค์‹œ ๋ถ€ํ˜ธ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋ฉด ์ตœ๋Œ€ ํž™๊ณผ ๋™์ผํ•˜๋‹ค. import heapq heap = [] values = [1,5,3,2,4] # ์•„๋ž˜ for๋ฌธ์„ ์‹คํ–‰์‹œํ‚ค๊ณ  ๋‚˜๋ฉด heap์€ [-5,-4,-3,-1,-2]๊ฐ€ ๋œ๋‹ค. for value in values: heapq.heappush(heap, -value) # ์•„๋ž˜ for๋ฌธ์„ ์‹คํ–‰์‹œํ‚ค๋ฉด 5,4,3,2,1์ด ์ถœ๋ ฅ๋œ๋‹ค. ์ฆ‰, ํฐ ์ˆซ์ž๋ถ€ํ„ฐ ์ถœ๋ ฅ์ด ๋œ๋‹ค. for i in range(5): print(-heapq.heappop(heap))

Algorithm/ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ (ํž™) - 10. ์ตœ์†Œํž™

๋‚˜์˜ ํ’€์ด ์ตœ์†Œ ํž™: ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’์ด ์ž์‹ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’๋ณด๋‹ค ํ•ญ์ƒ ์ž‘์€ ํž™ ์ตœ๋Œ€ ํž™: ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’์ด ์ž์‹ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’๋ณด๋‹ค ํ•ญ์ƒ ํฐ ํž™ ํž™ ์ƒ์„ฑ & ์›์†Œ ์ถ”๊ฐ€ import heapq heap = [] heapq.heappush(heap, 50) ์ด๋ฏธ ์ƒ์„ฑํ•ด๋‘” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด heapify ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ฆ‰๊ฐ์ ์œผ๋กœ ํž™ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. heap2 = [50 ,10, 20] heapq.heapify(heap2) ํž™์—์„œ ์›์†Œ ์‚ญ์ œ heappop ํ•จ์ˆ˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ์›์†Œ๋ฅผ ํž™์—์„œ ์ œ๊ฑฐํ•จ๊ณผ ๋™์‹œ์— ๊ทธ๋ฅผ ๊ฒฐ๊ด๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. result = heapq.heappop(heap) print(result) https://littlefoxdiary.tistory.com/m/3 [Python] ํž™ ์ž๋ฃŒ๊ตฌ์กฐ / ..

Algorithm/ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ (ํ•ด์‰ฌ) - 9. Anagram ์•„๋‚˜๊ทธ๋žจ

๋‚˜์˜ ํ’€์ด ๊ฐ•์˜ํ’€์ด get ๋ฉ”์†Œ๋“œ ์ด์šฉํ•˜๊ธฐ get ๋ฉ”์†Œ๋“œ๋Š” get(key,x) ๋กœ ์‚ฌ์šฉํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” '๋”•์…”๋„ˆ๋ฆฌ์— key๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, x๋ฅผ ๋ฆฌํ„ดํ•ด์ค˜๋ผ' ๋ผ๋Š” ์šฉ๋„์ž…๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๋กœ๋„ ํ’€์–ด๋ณด๊ธฐ

Algorithm/ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ (ํ) - 7. ๊ต์œก๊ณผ์ • ์„ค๊ณ„

๋‚˜์˜ ํ’€์ด #์ฐธ๊ณ  >>> list1 = [1, 2, 3, 4] >>> list2 = list1 >>> print(list1) [1, 2, 3, 4] >>> print(list2) [1, 2, 3, 4] list2๋Š” list1์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ’์„ ๋ณต์‚ฌํ•œ ๊ฒƒ์ด๋‹ค. ์ฆ‰, ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— list1์ด ์ˆ˜์ •๋˜๋ฉด list2๋„ ๋™์‹œ์— ์ˆ˜์ •๋œ๋‹ค. list1์™€ list2์ด ์„œ๋กœ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๋„๋ก ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ์— ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 4๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๊ฐ•์˜ ํ’€์ด

Algorithm/ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ (ํ) - 6. ์‘๊ธ‰์‹ค

๋‚˜์˜ ํ’€์ด ๊ฐ•์˜ ํ’€์ด # any() ํ•จ์ˆ˜ - ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ์ž๋ฃŒํ˜•(iterable)์„ ์ธ์ž๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. - ์ธ์ž๋กœ ๋ฐ›์€ ์š”์†Œ์ค‘ ํ•˜๋‚˜๋ผ๋„ True์ด๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. - False๊ฐ€ ๋ช‡๊ฐœ๋“  ์ƒ๊ด€์—†์ด ๋‹จ ํ•˜๋‚˜๋ผ๋„ True๋ฉด True ๋ฐ˜ํ™˜. - ๋ชจ๋“  ์š”์†Œ๊ฐ€ False ์ธ ๊ฒฝ์šฐ์—๋งŒ False ๋ฐ˜ํ™˜. - ์ธ์ž๋กœ ๋ฐ›์€ ์ž๋ฃŒํ˜•์ด ๋น„์–ด์ด๋Š” ๊ฒฝ์šฐ False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. if any(15>x for x in a): print(“YES”) else: print(“NO”)

Algorithm/ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ

์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ (ํ) - 5. ๊ณต์ฃผ ๊ตฌํ•˜๊ธฐ

๋‚˜์˜ ํ’€์ด ๊ฐ•์˜ ํ’€์ด ํ ์ž๋ฃŒ๊ตฌ์กฐ ํŒŒ์ด์ฌ์—์„œ๋Š” deque ์ด์šฉ

hello_u
'Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (9 Page)