๐ํฌ ํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ์ด๋
ํฌ ํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ์ ๋ฆฌ์คํธ์ ์์ฐจ์ ์ผ๋ก ์ ๊ทผํด์ผ ํ ๋ 2๊ฐ์ ์ ์ ์์น๋ฅผ ๊ธฐ๋กํ๋ฉด์ ์ฒ๋ฆฌํ๋ ์๊ณ ๋ฆฌ์ฆ์ ์๋ฏธํ๋ค.
์๋ฅผ ๋ค์ด์ 2, 3, 4, 5, 6, 7๋ฒ ํ์์ ๊ฐ๋ฆฌ์ผ์ผ ํ ๋ ์ฐ๋ฆฌ๋ '2๋ฒ๋ถํฐ 7๋ฒ๊น์ง์ ํ์'์ด๋ผ๊ณ ๋ถ๋ฅด๊ณค ํ๋ค.
์ด์ฒ๋ผ ๋ฆฌ์คํธ์ ๋ด๊ธด ๋ฐ์ดํฐ์ ์์ฐจ์ ์ผ๋ก ์ ๊ทผํด์ผ ํ ๋๋ '์์์ ' ๊ณผ '๋ ์ ' 2๊ฐ์ ์ ์ผ๋ก ์ ๊ทผํ ๋ฐ์ดํฐ์ ๋ฒ์๋ฅผ ํํํ ์ ์๋ค.
๊ทธ๋ฅ naive ๋ฐฉ์์ธ ๋ฐ๋ณต๋ฌธ์ ์ฐ๋ค๋ณด๋ฉด ์๊ฐ ์ด๊ณผ๊ฐ ๊ฑธ๋ฆฌ๋ ๊ฒฝ์ฐ์ ํฌ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋ชจ๋ฆฌ์ ์๊ฐ ํจ์จ์ฑ์ ๋์ผ ์ ์๋ค.
ํฌ์ธํฐ๋ ํฌ๊ฒ ๋ ๊ฐ์ง ๋ฐฉ์์ผ๋ก ์ฐ์ธ๋ค.
1. ์์์ ์์ํ๋ ํฌ์ธํฐ์ ๋์์ ์์ํ๋ ํฌ์ธํฐ๊ฐ ๋ง๋๋ ํ์ ๋๋ ์ํ๋ ๊ฐ target์ ์ฐพ์ ๋๊น์ง
2. ๋๋ ๋น ๋ฅธ ํฌ์ธํฐ(fast runner) ๊ฐ ๋๋ฆฐ ํฌ์ธํฐ(slow runner) ๋ณด๋ค ์์๋ ํ์
์ ๋๊ฐ์ง ๋ฐฉ์์ ์์ ๋ฅผ ํตํด ์ดํด๋ณด์.
โ๏ธ ํ์1: ์์ - LeetCode [Two Sum II - Input Array Is Sorted]
๋ฌธ์ : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
Two Sum II - Input Array Is Sorted - LeetCode
Two Sum II - Input Array Is Sorted - Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2
leetcode.com
์ด ๋ฌธ์ ๋ 'ํน์ ํ ํฉ์ ๊ฐ์ง๋ ๋ ๊ฐ์ ์์ ์ฐพ๊ธฐ' ๋ฌธ์ ์ด๋ค.
๋ฌธ์ ๋ฅผ ํธ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
1. ์์์ (i ํฌ์ธํธ)๊ณผ ๋์ (j ํฌ์ธํธ) ์ด ๊ฐ๊ฐ ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค์ ๋ง์ง๋ง ์ธ๋ฑ์ค๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํ๋ค.
2. ๋ ์์ ํฉ์ด target ๋ณด๋ค ์์ผ๋ฉด i ํฌ์ธํฐ๋ฅผ ๋ค๋ก ํ๋ ์ด๋ (i += 1)
3. ๋ ์์ ํฉ์ด target ๋ณด๋ค ํฌ๋ฉด j ํฌ์ธํฐ๋ฅผ ์์ผ๋ก ํ๋ ์ด๋ (j -= 1)
ํ์ด๋ ๋ค์๊ณผ ๊ฐ๋ค.
โ๏ธ ํ์2: ์์ - LeetCode [Remove Duplicates from Sorted Array]
๋ฌธ์ : https://leetcode.com/problems/remove-duplicates-from-sorted-array/
Remove Duplicates from Sorted Array - LeetCode
Remove Duplicates from Sorted Array - Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place [https://en.wikipedia.org/wiki/In-place_algorithm] such that each unique element appears only once. The relative order of the e
leetcode.com
์ด ๋ฌธ์ ๋ '๋ ํฌ์ธํฐ๊ฐ ์์์ ์์ํ๋๋ฐ ์๋๊ฐ ๋ค๋ฅด๊ฒ ์์ง์ด๋ ํ์'์ด๋ค. ์ผ๋ช ํ ๋ผ์ ๊ฑฐ๋ถ์ด ํ์์ด๋ค.
์ด ๋ฌธ์ ๋ฅผ ํธ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
1. slow์ fast๋ ๊ฐ๊ฐ ์ธ๋ฑ์ค 0๊ณผ 1์์ ์์ํ๋ค.
2. fast ๊ฐ 1๋ถํฐ ์์ํด์ ์์ง์ด๋ฉด์ slow ์ ์ซ์๊ฐ ๋ฌ๋ผ์ง๋ฉด slow๊ฐ ์์ผ๋ก ์ด๋ํ๋ฉด์ ๊ทธ ์ซ์๋ฅผ fast ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ์ซ์๋ก ๋ฐ๊ฟ์ค๋ค.
3. ์ด๋ฌํ loop ๋ฅผ ๋ฐ๋ณตํ๋ค ๋ณด๋ฉด ๋ฐฐ์ด์ด nums=[0,0,1,1,1,2,2,3,3,4]์์ nums=[0,1,2,3,4,2,2,3,3,4]๋ก ๋ฐ๋๊ณ
slow +1 ์ด ์ค๋ณต๋ ์ซ์๊ฐ ์๋ ๊ธธ์ด์ด๊ธฐ ๋๋ฌธ์ slicing ํด์ return ํ๋ฉด ๋๋ค.
๋ค๋ฅธ ๋ธ๋ก๊ทธ์์ ์๋ ๊ทธ๋ฆผ๋ณด๊ณ ํ ๋ฒ์ ์ดํดํ๋ค. ์ถ์ฒ๋ ๊ทธ๋ฆผ ๋ฐ์ ๋จ๊ฒจ๋์๋ค.
ํ์ด๋ ๋ค์๊ณผ ๊ฐ๋ค.
โ๏ธ ์์ฉ ์์ - LeetCode[3 Sum]
๋ฌธ์ : https://leetcode.com/problems/3sum/description/
3Sum - LeetCode
3Sum - Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Example 1: Input: nums
leetcode.com
์ด ๋ฌธ์ ๋ [Two Sum II - Input Array Is Sorted] ์ฌ๊ธฐ์์ 3 Sum ์ผ๋ก ํ์ฅ๋ ๋ฌธ์ ์ด๋ค.
์ด ๋ฌธ์ ๋ ๋ค์๊ณผ ๊ฐ์ด ํ์๋ค.
1. ์ด ๋ฌธ์ ๋ ๋จผ์ nums ๊ฐ ์ ๋ ฌ๋์ด ์์ง ์์ผ๋ฏ๋ก ์ ๋ ฌ์ ๋จผ์ ํด์ค๋ค.
2. ํฌ ํฌ์ธํฐ๋ฅผ ์ด์ฉํด์ ํ์ฌ ๊ฐ์์ ์ผ์ชฝ, ์ค๋ฅธ์ชฝ ๊ฐ์ ์ ํด์ค๋ค.
3. ์ผ์ชฝ + ํ์ฌ ๊ฐ + ์ค๋ฅธ์ชฝ ๊ฐ ๋ํด์ 0 ๋ณด๋ค ํฌ๋ฉด ์ค๋ฅธ์ชฝ ์ธ๋ฑ์ค๋ฅผ 1 ๊ฐ์์ํค๊ณ , ์์ผ๋ฉด ์ผ์ชฝ ์ธ๋ฑ์ค๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค.
4. ํฉํด์ 0์ด ๋๋ฉด [์ผ์ชฝ, ํ์ฌ๊ฐ, ์ค๋ฅธ์ชฝ ๊ฐ]์ ๋ฐํ ๋ฆฌ์คํธ์ ์ถ๊ฐํ๋ค.
ํ์ด๋ ๋ค์๊ณผ ๊ฐ๋ค.
ํต๊ณผ๋ ํ์ง๋ง ์๊ฐ๋ณด๋ค Runtime ์ด ๋๋ฌด ์ค๋ ๊ฑธ๋ ธ๋ค.
๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฐพ์๋ณด๋ ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ ์ฝ๋ฉ ์ธํฐ๋ทฐ ์ ๋์จ ํ์ด ๋ฐฉ์์ ๋ค์๊ณผ ๊ฐ์๋ค.
์ฒ์ฒํ ๋ค์ ์ดํดํด ๋ด์ผ๊ฒ ๋ค.
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 167. Two Sum II - Input Array Is Sorted | LIM (0) | 2023.05.04 |
---|---|
[LeetCode] 350. Intersection of Two Arrays II | LIM (0) | 2023.05.03 |
[LeetCode] 441.Arranging Coins | LIM (0) | 2023.05.02 |
Linked List vs Array List | LIM (0) | 2022.10.03 |
ํ๋ก๊ทธ๋๋จธ์ค-์คํฌํธ๋ฆฌ(๋ถ์ : For-else๋ฌธ์ ๋ํ์ฌ) (0) | 2021.01.17 |
๋๊ธ