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

๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ

(159)
[Programmers] ๋ฌธ์ž์—ด ์••์ถ• ๋ฌธ์ œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ „๋ฌธ๊ฐ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ "์–ดํ”ผ์น˜"๋Š” ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์— ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ๋น„์†์‹ค ์••์ถ• ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ฌธ์ž์—ด์—์„œ ๊ฐ™์€ ๊ฐ’์ด ์—ฐ์†ํ•ด์„œ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๊ทธ ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜์™€ ๋ฐ˜๋ณต๋˜๋Š” ๊ฐ’์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ๋” ์งง์€ ๋ฌธ์ž์—ด๋กœ ์ค„์—ฌ์„œ ํ‘œํ˜„ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์˜ˆ๋กœ "aabbaccc"์˜ ๊ฒฝ์šฐ "2a2ba3c"(๋ฌธ์ž๊ฐ€ ๋ฐ˜๋ณต๋˜์ง€ ์•Š์•„ ํ•œ๋ฒˆ๋งŒ ๋‚˜ํƒ€๋‚œ ๊ฒฝ์šฐ 1์€ ์ƒ๋žตํ•จ)์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์€ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž๊ฐ€ ์ ์€ ๊ฒฝ์šฐ ์••์ถ•๋ฅ ์ด ๋‚ฎ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, "abcabcdede"์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด์€ ์ „ํ˜€ ์••์ถ•๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. "์–ดํ”ผ์น˜"๋Š” ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด์„ 1๊ฐœ ์ด์ƒ์˜ ๋‹จ์œ„๋กœ ์ž˜๋ผ์„œ ..
[BOJ] ์ˆ˜๊ฐ•์‹ ์ฒญ (#13414) ๋ฌธ์ œ ๊ตญ๋ฏผ๋Œ€ํ•™๊ต์—์„œ๋Š” ๋งค ํ•™๊ธฐ ์‹œ์ž‘ ์ „ ์ข…ํ•ฉ์ •๋ณด์‹œ์Šคํ…œ์—์„œ ์ˆ˜๊ฐ•์‹ ์ฒญ์„ ํ•œ๋‹ค. ๋งค ์ˆ˜๊ฐ•์‹ ์ฒญ๋งˆ๋‹ค ์•„์ฃผ ๋งŽ์€ ํ•™์ƒ๋“ค์ด ๋ชฐ๋ ค ์„œ๋ฒ„์— ๋งŽ์€ ๋ถ€ํ•˜๊ฐ€ ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์—, ๊ตญ๋ฏผ๋Œ€ํ•™๊ต์—์„œ๋Š” ์ˆ˜๊ฐ•์‹ ์ฒญ ๋ถ€ํ•˜ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์„ ๋„์ž…ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ•˜์˜€๋‹ค. ์ƒˆ๋กœ์šด ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. ์ˆ˜๊ฐ•์‹ ์ฒญ ๋ฒ„ํŠผ์ด ํ™œ์„ฑํ™” ๋œ ํ›„, ์ˆ˜๊ฐ•์‹ ์ฒญ ๋ฒ„ํŠผ์„ ์กฐ๊ธˆ์ด๋ผ๋„ ๋นจ๋ฆฌ ๋ˆ„๋ฅธ ํ•™์ƒ์ด ๋Œ€๊ธฐ๋ชฉ๋ก์— ๋จผ์ € ๋“ค์–ด๊ฐ„๋‹ค. ์ด๋ฏธ ๋Œ€๊ธฐ์—ด์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์ƒํƒœ์—์„œ ๋‹ค์‹œ ์ˆ˜๊ฐ•์‹ ์ฒญ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๊ฒฝ์šฐ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๋งจ ๋’ค๋กœ ๋ฐ€๋ ค๋‚œ๋‹ค. ์ž ์‹œ ํ›„ ์ˆ˜๊ฐ•์‹ ์ฒญ ๋ฒ„ํŠผ์ด ๋น„ํ™œ์„ฑํ™” ๋˜๋ฉด, ๋Œ€๊ธฐ๋ชฉ๋ก์—์„œ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ํ•™์ƒ๋ถ€ํ„ฐ ์ž๋™์œผ๋กœ ์ˆ˜๊ฐ•์‹ ์ฒญ์ด ์™„๋ฃŒ๋˜๋ฉฐ, ์ˆ˜๊ฐ• ๊ฐ€๋Šฅ ์ธ์›์ด ๊ฝ‰ ์ฐฐ ๊ฒฝ์šฐ ๋‚˜๋จธ์ง€ ๋Œ€๊ธฐ๋ชฉ๋ก์€ ๋ฌด์‹œํ•˜๊ณ  ์ˆ˜๊ฐ•์‹ ์ฒญ์„ ์ข…๋ฃŒํ•œ๋‹ค. ์œ„์˜ ํ‘œ๋Š” ์ตœ๋Œ€ ์ˆ˜๊ฐ• ๊ฐ€๋Šฅ ์ธ์›์ด 3๋ช…์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ..
[BOJ] ๋งˆ์ธํฌ๋ž˜ํ”„ํŠธ (#18111) ๋ฌธ์ œ ํŒ€ ๋ ˆ๋“œ์‹œํ”„ํŠธ๋Š” ๋Œ€ํšŒ ์ค€๋น„๋ฅผ ํ•˜๋‹ค๊ฐ€ ์ง€๋ฃจํ•ด์ ธ์„œ ์ƒŒ๋“œ๋ฐ•์Šค ๊ฒŒ์ž„์ธ ‘๋งˆ์ธํฌ๋ž˜ํ”„ํŠธ’๋ฅผ ์ผฐ๋‹ค. ๋งˆ์ธํฌ๋ž˜ํ”„ํŠธ๋Š” 1 × 1 × 1(์„ธ๋กœ, ๊ฐ€๋กœ, ๋†’์ด) ํฌ๊ธฐ์˜ ๋ธ”๋ก๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ 3์ฐจ์› ์„ธ๊ณ„์—์„œ ์ž์œ ๋กญ๊ฒŒ ๋•…์„ ํŒŒ๊ฑฐ๋‚˜ ์ง‘์„ ์ง€์„ ์ˆ˜ ์žˆ๋Š” ๊ฒŒ์ž„์ด๋‹ค. ๋ชฉ์žฌ๋ฅผ ์ถฉ๋ถ„ํžˆ ๋ชจ์€ lvalue๋Š” ์ง‘์„ ์ง“๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ ๊ณ ๋ฅด์ง€ ์•Š์€ ๋•…์—๋Š” ์ง‘์„ ์ง€์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋•…์˜ ๋†’์ด๋ฅผ ๋ชจ๋‘ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ‘๋•… ๊ณ ๋ฅด๊ธฐ’ ์ž‘์—…์„ ํ•ด์•ผ ํ•œ๋‹ค. lvalue๋Š” ์„ธ๋กœ N, ๊ฐ€๋กœ M ํฌ๊ธฐ์˜ ์ง‘ํ„ฐ๋ฅผ ๊ณจ๋ž๋‹ค. ์ง‘ํ„ฐ ๋งจ ์™ผ์ชฝ ์œ„์˜ ์ขŒํ‘œ๋Š” (0, 0)์ด๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์€ ์ด ์ง‘ํ„ฐ ๋‚ด์˜ ๋•…์˜ ๋†’์ด๋ฅผ ์ผ์ •ํ•˜๊ฒŒ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค. ์šฐ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ์ข…๋ฅ˜์˜ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ขŒํ‘œ (i, j)์˜ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ๋ธ”๋ก์„ ์ œ๊ฑฐํ•˜์—ฌ ์ธ๋ฒคํ† ๋ฆฌ์— ๋„ฃ๋Š”๋‹ค. ์ธ๋ฒค..
[BOJ] ํƒ€๋…ธ์Šค (#20310) ๋ฌธ์ œ ์–ด๋Š ๋‚ , ํƒ€๋…ธ์Šค๋Š” 0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด S$S$๋ฅผ ๋ณด์•˜๋‹ค. ์‹ ๊ธฐํ•˜๊ฒŒ๋„, S$S$๊ฐ€ ํฌํ•จํ•˜๋Š” 0์˜ ๊ฐœ์ˆ˜์™€ S$S$๊ฐ€ ํฌํ•จํ•˜๋Š” 1์˜ ๊ฐœ์ˆ˜๋Š” ๋ชจ๋‘ ์ง์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. ๊ฐ‘์ž๊ธฐ ์‹ฌ์ˆ ์ด ๋‚œ ํƒ€๋…ธ์Šค๋Š” S$S$๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฌธ์ž ์ค‘ ์ ˆ๋ฐ˜์˜ 0๊ณผ ์ ˆ๋ฐ˜์˜ 1์„ ์ œ๊ฑฐํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด S′$S'$๋ฅผ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค. S′$S'$๋กœ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์—ด ์ค‘ ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒƒ์„ ๊ตฌํ•˜์‹œ์˜ค. ํ•ด์„ค ์ฝ”๋“œ num = list(str(input())) # 0 ๊ณผ 1 ์˜ ์ œ๊ฑฐ ํšŸ์ˆ˜ ๊ฐ๊ฐ ๊ตฌํ•ด์ฃผ๊ธฐ cnt = num.count('0')//2 knt = num.count('1')//2 # 0์€ ๋’ค์—์„œ๋ถ€ํ„ฐ 1์€ ์•ž์—์„œ๋ถ€ํ„ฐ ์ œ๊ฑฐํ•˜๊ธฐ for _ in range(cnt): num.pop(-(num[::-1].index('0'))-1) for _..
[Leetcode] Last Moment Before All Ants Fall Out of a Plank (#1503) ๋ฌธ์ œ We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with a speed of 1 unit per second. Some of the ants move to the left, the other move to the right. When two ants moving in two different directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time. When an ant ..
[Leetcode] Validate Stack Sequences (#946) ๋ฌธ์ œ Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise. Example 1: Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] Output: true Explanation: We might do the following sequence: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() ->..
[Leetcode] Create Target Array in the Given Order (#1389) ๋ฌธ์ œ Given two arrays of integers nums and index. Your task is to create target array under the following rules: Initially target array is empty. From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array. Repeat the previous step until there are no elements to read in nums and index. Return the target array. It is guaranteed that the insertion operati..
[Leetcode] Clumsy Factorial (#1006) ๋ฌธ์ œ The factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We make a clumsy factorial using the integers in decreasing order by swapping out the multiply operations for a fixed rotation of operations with multiply '*', divide '/', add '+', and subtract '-' in this order. For exampl..