๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜

[Algorithm] ํˆฌ ํฌ์ธํ„ฐ (feat. leetcode Two Sum, Three Sum) | LIM

by forestlim 2023. 2. 19.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“šํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€

ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ฆฌ์ŠคํŠธ์— ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•  ๋•Œ 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 ํ•˜๋ฉด ๋œ๋‹ค. 

 

๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ์—์„œ ์•„๋ž˜ ๊ทธ๋ฆผ๋ณด๊ณ  ํ•œ ๋ฒˆ์— ์ดํ•ดํ–ˆ๋‹ค. ์ถœ์ฒ˜๋Š” ๊ทธ๋ฆผ ๋ฐ‘์— ๋‚จ๊ฒจ๋‘์—ˆ๋‹ค.

https://benn.tistory.com/9

 

ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

 

 

โœ”๏ธ ์‘์šฉ ์˜ˆ์ œ - 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 ์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค. 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๋‹ˆ ํŒŒ์ด์ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋”ฉ ์ธํ„ฐ๋ทฐ ์— ๋‚˜์˜จ ํ’€์ด ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค. 

์ฒœ์ฒœํžˆ ๋‹ค์‹œ ์ดํ•ดํ•ด ๋ด์•ผ๊ฒ ๋‹ค.

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€