๐ ์ด๋ณด์์ฉ ๊ฐ์ด๋ 'ํ๊ฒ๋ณด๋ค ํฐ ๊ฐ์ฅ ์์ ๋ฌธ์ ์ฐพ๊ธฐ' - Problem 744 (C++, Python, JavaScript)
Source: Dev.to

๋ฌธ์ ์์ฝ
์ฃผ์ด์ง๋ ๊ฒ:
letters๋ผ๋ ์ ๋ ฌ๋ ๋ฌธ์ ๋ฆฌ์คํธ.target์ด๋ผ๋ ํน์ ๋ฌธ์.
๋ชฉํ:
target๋ณด๋ค ์๊ฒฉํ ํฐ ๊ฐ์ฅ ์์ ๋ฌธ์๋ฅผ ๋ฆฌ์คํธ์์ ์ฐพ๋๋ค.- ๊ทธ๋ฐ ๋ฌธ์๊ฐ ์์ผ๋ฉด(์ฆ,
target์ด ๋ชจ๋ ์์๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๊ฒฝ์ฐ) ๋ฆฌ์คํธ์ ์ฒซ ๋ฒ์งธ ๋ฌธ์๋ฅผ ๋ฐํํ๋ค.
Intuition
Because the array is already sorted, we can avoid a linear scan (โฏO(n)โฏ) and instead apply binary search (โฏO(logโฏn)โฏ).
The algorithm follows a โsearch and discardโ approach:
- Examine the middle element.
- If
letters[mid] > targetโ this could be the answer. Record the index and continue searching the left half for an even smaller qualifying character. - If
letters[mid] โค targetโ the middle element and everything to its left are too small. Discard them and search the right half.
If the search finishes without finding a greater character, we โwrap aroundโ and return letters[0].
๋จ๊ณ๋ณ ์ค๋ช : ์์ ์ดํด
์์ โฏ1
letters = ["c", "f", "j"], target = "a"
๋ฌธ์ 'a'๋ณด๋ค ํฐ ์ฒซ ๋ฒ์งธ ๋ฌธ์๋ 'c'์
๋๋ค.
Result: "c"
์์ โฏ2
letters = ["c", "f", "j"], target = "c"
'c'๋ ํ์ฉ๋์ง ์์ต๋๋ค. ์๋ํ๋ฉด ์ฐ๋ฆฌ๋ ์๊ฒฉํ ๋ ํฐ ๋ฌธ์๋ฅผ ํ์๋ก ํ๊ธฐ ๋๋ฌธ์
๋๋ค. ๋ค์ ๋ฌธ์๋ 'f'์
๋๋ค.
Result: "f"
์์ โฏ3
letters = ["x", "x", "y", "y"], target = "z"
'z'๋ณด๋ค ํฐ ๋ฌธ์๊ฐ ์์ต๋๋ค; ์ฐ๋ฆฌ๋ ์ฒ์ ์์๋ก ๋์๊ฐ๋๋ค.
Result: "x"
์ฝ๋ ๊ตฌํ
C++
class Solution {
public:
char nextGreatestLetter(vector& letters, char target) {
int n = letters.size();
int left = 0;
int right = n - 1;
int firstTrueIndex = -1;
while (left target) {
firstTrueIndex = mid;
right = mid - 1; // look for a smaller qualifying character
} else {
left = mid + 1; // discard left half
}
}
return firstTrueIndex == -1 ? letters[0] : letters[firstTrueIndex];
}
};
ํ์ด์ฌ
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
n = len(letters)
left, right = 0, n - 1
first_true_index = -1
while left target:
first_true_index = mid
right = mid - 1
else:
left = mid + 1
return letters[0] if first_true_index == -1 else letters[first_true_index]
์๋ฐ์คํฌ๋ฆฝํธ
/**
* @param {character[]} letters
* @param {character} target
* @return {character}
*/
var nextGreatestLetter = function (letters, target) {
let left = 0;
let right = letters.length - 1;
let firstTrueIndex = -1;
while (left target) {
firstTrueIndex = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return firstTrueIndex === -1 ? letters[0] : letters[firstTrueIndex];
};
ํต์ฌ ์์
- ์ด์ง ํ์ ํจ์จ์ฑ: ๋งค ๋ฐ๋ณต๋ง๋ค ํ์ ๋ฒ์๋ฅผ ์ ๋ฐ์ผ๋ก ์ค์ฌ O(logโฏn) ์๊ฐ ๋ณต์ก๋๋ฅผ ๋ฌ์ฑํ๊ณ , O(n) ๋ณด๋ค ํจ์ฌ ๋น ๋ฆ ๋๋ค.
- Upper Bound ํจํด: ์ด ๋ฌธ์ ๋ ์ ํ์ ์ธ โupper boundโ ํ์์ผ๋ก, ์ฃผ์ด์ง ๊ฐ๋ณด๋ค ์๊ฒฉํ ํฐ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ์ฐพ๋ ๊ฒ์ ๋๋ค.
- WrapโAround ์ฒ๋ฆฌ: sentinel ๊ฐ(์:
firstTrueIndex = -1)์ด๋ ๋ชจ๋๋ฌ ์ฐ์ฐ์ ์ฌ์ฉํ๋ฉด ์ํ ๋ฐฐ์ด ๋ก์ง์ ๊น๋ํ๊ฒ ๊ตฌํํ ์ ์์ต๋๋ค.
์ต์ข ์๊ฐ
์ด ๋ฌธ์ ๋ ๊ฒ์ ๊ธฐ๋ฅ ์ต์ ํ์ ๋ํ ํ๋ฅญํ ์ ๋ฌธ์์ ๋๋ค. ํ์ผ ํ์๊ธฐ๋ ์ฐ๋ฝ์ฒ ๋ชฉ๋ก๊ณผ ๊ฐ์ ์ค์ ์์คํ ์์๋ ์ฌ์ฉ์๊ฐ ๋ฌธ์๋ฅผ ์ ๋ ฅํ ๋ โ๋ค์์ผ๋ก ํฐโ ํญ๋ชฉ์ผ๋ก ์ด๋ํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์ข ์ข ์์ต๋๋ค. ์ด์ง ํ์์ ๋ง์คํฐํ๋ฉด ์ต๊ณ ์ ๊ธฐ์ ๊ธฐ์ ์์์ ๊ธฐ์ ๋ฉด์ ์ค๋น๋ ๋ฌผ๋ก , ์๋ฐฑ๋ง ๊ฐ์ ํญ๋ชฉ๊น์ง ํ์ฅ ๊ฐ๋ฅํ ์ฑ๋ฅ ์ข์ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๊ฒ ๋ฉ๋๋ค.