์ด๋ฒ์๋ Binary Search์ ๋ํ ๋ด์ฉ ์
๋๋ค.
์ฝ๋ฉ ์ธํฐ๋ทฐ ๋ฌธ์ ๋ ํ๋ฌธ์ ์ฉ ๊ฐ์ด ํ์ด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. ์ ์ผ ์๋์ ๊ธฐ์ถ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๊ฐ์ด ํ์ด๋ด์!
์์ด ๊ณต๋ถ๋ ์๋๋ ์์ต๋๋ค. ๋งค์ผ ๊พธ์คํ ๋ฃ๊ณ ๋งํ๊ธฐ ์ฐ์ต ํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ค์ํฉ๋๋ค.
๊ฐ๋ฐ์๋งค์ผ์์ด๋ ๋น๋ถ๊ฐ์ Cracking Coding Interview์ ์ ์๋ก ์ ๋ช
ํ Gayle Laakmann McDowell์ ์์ ๊ฐ์ข๋ฅผ ์ง์์ ์ผ๋ก ๊ณต๋ถํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ ์ฒด ๋ถ๋์ ๋๋ฌด ๊ธธ์ด์ ์ฃผ์๋ถ๋ถ ํ ๋ ๊ตฐ๋ฐ๋ง 1๋ถ ์ดํ๋ก ๋ฐ์ทํ์ฌ mp3ํ์ผ๋ก ๋ง๋ค๊ณ ์์ต๋๋ค.
์ฆ ํ ์ฃผ์ ๋น 1๋ถ ์ดํ ๋ถ๋์ mp3ํ์ผ์ด ํ ๋๊ฐ์ฉ ์ ๊ณต๋๊ฒ ์ต๋๋ค. ๋๋จธ์ง ๋ถ๋ถ์ ๋ฆฌ์ค๋ ์ฐ์ต ํ์๋ฉด ๋๊ฒ ์ต๋๋ค.
์ด๋ฒ์๋ ๋ฐ๋ผํ๊ธฐ ์ฝ๋๋ก ์ต๋ํ ์งง๊ฒ ์๋์ต๋๋ค.
๊ณต๋ถํ๋ 3๋จ๊ณ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ์ต๋๋ค.
1๋ถ ๋ถ๋์ ๋ฒ์ญ
์ ์ฒด ๋ฃ๊ธฐ ๋๋ฒ
๋ฌธ์ฅ ๋ฃ๊ณ ๋ฐ๋ผ ๋งํ๊ธฐ ๋๋ฒ
ํ๊ตญ๋ง๋ก ๋ฃ๊ณ ์์ด๋ก ๋งํ๊ธฐ
ํ๋ฃจ์ ํ์๊ฐ ์ด์ ๋ค์ผ๋ฉด์ ๋งํ๊ธฐ ์ฐ์ตํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
์ฐ์ต mp3 ํ์ผ ๋ค์ด๋ก๋: https://drive.google.com/open?id=1FCSEt_5l-feV3VbFYZ7FO6sXBUDZKmpn
์๋ณธ ๋์์:
--- mp3 script & ๋ฒ์ญ ์ ---
So in an integer array that looks like this, you take some element that you're looking for, like 13, and you compare it to the midpoint, and it has to be a sorted array for this to work, it's very important, and you compare it to the midpoint.
13 is less than this midpoint and so 13, if it's in the array at all, it has to be on the left side.
And then you repeat this process on the left side and say let me look at the midpoint of the left side.
Is 13 before and after that, before or after that, and you just repeat that process until you either find 13 or you know that 13 can't be in it.
So how fast is this is algorithm? Well let's imagine we start off with n elements so we have a search space of n elements.
In a single comparison, just one, we've cut our search space down to n over 2. Then with one more comparison we cut it down to n over 4 and then in half again and half and half and half again.
So how many total operations in the very worst case will we have to run until we figure out if it contains an element or not?
Well the total number of operations we'll have to do is determined by how many times can we divide n by 2 until we get down to just one. So this is what log of n expresses. A log base 2 of n expresses.
So if you pause the video you can study that math for a second and see the relationship between those two things. But this means that binary search is a log n problem.
์ด ๊ฐ์ ์ ์ ๋ฐฐ์ด์์ 13๊ฐ์ ์ฐพ๊ณ ์๋ ์ด๋ค ์์๋ฅผ ๊ฐ์ง๋ ค๊ณ ํ ๋ ์ค๊ฐ๊ฐ๊ณผ ๋น๊ตํ๋ ค๋ฉด ์ค์ํ ๊ฒ์ ์ ๋ ฌ๋ ๋ฐฐ์ด์ ์ฌ์ฉํด์ผํ๋ค๋ ๊ฒ์ด๊ณ , ๊ทธ๋ฆฌ๊ณ ์ค๊ฐ๊ฐ๊ณผ ๋น๊ตํฉ๋๋ค.
13์ ์ค๊ฐ๊ฐ ๋ณด๋ค ์๊ณ , ์ฐ์ธก ๋ฐฐ์ด์ ์๋ค๋ฉด ์ข์ธก์ ์์ ๊ฒ์
๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด ์ฒ๋ฆฌ ๋ฐฉ๋ฒ์ ์ผํธ์ ๋ฐ๋ณตํฉ๋๋ค. 13์ด ์ด์ ์ด๋ ๋ค์ ์๋ค๋ฉด ๊ณ์ํด์ 13์ด ์๋ค๋ ๊ฒ์ ์๊ฑฐ๋ 13์ ์ฐพ์ ๋ ๊น์ง ํฉ๋๋ค.
์ด ์๊ณ ๋ฆฌ์ฆ์ ์ผ๋ง๋ ๋น ๋ฅผ๊น์? N ์์๋ค๊ณผ ์์ํ๋ค๊ณ ์๊ฐํด ๋ด
์๋ค. ์ฐ๋ฆฌ๋ n ์์๋ค๋งํผ์ ๊ฒ์ ๊ณต๊ฐ์ ๊ฐ์ง๊ณ ์์ต๋๋ค.
ํ๋ฒ์ ๋น๊ต์ ์ฐ๋ฆฌ๋ ๊ฒ์ ๊ณต๊ฐ์ ๋ฐ์ผ๋ก ์ค์์ต๋๋ค. ๋ ํ๋ฒ ๋ ๋น๊ตํ๋ฉด ์ฐ๋ฆฌ๋ 1/4๋ก ์๋ฅด๊ณ ๋ ๋ฐ์ผ๋ก ๋ ๋ฐ์ผ๋ก ๋ค์ ์๋ฅผ ์ ์์ต๋๋ค.
์ด ์์๊ฐ ์๋์ง ์๋์ง ์ฐพ๊ธฐ์ํด ์ต์
์ ๊ฒฝ์ฐ์๋ ์ผ๋ง๋ ๋ง์ด ํด์ผํ ๊น์?
์ฐ๋ฆฌ๊ฐ ํด์ผํ๋ ์ด ์ํ ์๋ 1๊ฐ๊ฐ ๋ ๋๊น์ง ์ผ๋ง๋ ๋ง์ด 2๋ก ๋๋๋๊ฐ์ ๊ฒฐ์ ๋ฉ๋๋ค. ์ด๊ฒ์ด log n์
๋๋ค. Log 2์ n์
๋๋ค.
๋์์์ ์ ์งํ๊ณ ์ ๊น ๊ณ์ฐํด ๋ณด๊ณ ๋๊ฐ ๊ฐ์ ๊ด๊ณ๋ฅผ ๋ณด์ธ์. ์ด๊ฒ์ binary search๊ฐ log n ๋ฌธ์ ๋ผ๋๊ฑธ ์๋ฏธํฉ๋๋ค.
----- ์ด์ฃผ์ Interview Question (๊ฐ์ด ํ์ด ๋ด์!) -----
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.
Example :
Input: root = [4,2,6,1,3,null,null]
Output: 1
Explanation:
Note that root is a TreeNode object, not an array.
The given tree [4,2,6,1,3,null,null] is represented by the following diagram:
4
/ \
2 6
/ \
1 3
while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.
Note:
The size of the BST will be between 2 and 100.
The BST is always valid, each node's value is an integer, and each node's value is different.
์ ๋ฌธ์ ๋งํฌ: https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/