๋ฌธ์
ํ์ด ๊ณผ์
ํฌ ํฌ์ธํฐ ์ ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ ์ ์ฌ์ฉํ๋ ๋ฌธ์ ์
๋๋ค. partition ์ด ํน์ ๋ฌธ์๋ค์ ์งํฉ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์์ ๋
๊ฐ์ฅ ๋ง์ partition ์ผ๋ก ๋๋๊ธฐ ์ํด์๋ ์ฐ์ ๋ค์๊ณผ ๊ฐ์ด ์๊ฐํด๋ณผ ์ ์์ต๋๋ค.
๋จผ์ partition A ์ ๊ฐ์ฅ ์ฒซ ๋ฌธ์๊ฐ a ๋ผ๊ณ ํ๋ค๋ฉด,
ํด๋น partition ์๋ ๋ฌธ์์ด S ์ ์กด์ฌํ๋ ๋ชจ๋ a ๋ฅผ ํฌํจํด์ผํฉ๋๋ค.
๋ฐ๋ผ์ ๋ฌธ์์ด S ์์ ๊ฐ์ฅ ๋ง์ง๋ง์ ์กด์ฌํ๋ a ๋ฅผ ํฌํจํด์ผํฉ๋๋ค.
๊ทธ๋ฌ๋ฏ๋ก partition ์ ๊ธฐ์ค์ด ๋๋ ํฌ์ธํฐ right ์
๊ฐ partition ์ ๋ฌธ์๋ค์ ๊ฒ์ฌํ๊ธฐ ์ํ ํฌ์ธํฐ left ๋ฅผ ์ ์ธํด์ ํ์์ ์ํํ๋ฉด ๋ฉ๋๋ค.
์ฝ๋
/**
* @param {string} S
* @return {number[]}
*/
var partitionLabels = function (S) {
const answer = [];
let left = 0;
while (left < S.length) {
let start = left;
let right = S.lastIndexOf(S[left]);
while (left < right) {
right = Math.max(right, S.lastIndexOf(S[left++]));
}
left++;
answer.push(right - start + 1);
}
return answer;
};๋ฐ์ํ
'๐ algorithm > leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| LeetCode 752 - Open the Lock (Medium) (0) | 2021.03.04 |
|---|---|
| LeetCode 11 - Container With Most Water (Medium) (0) | 2021.03.04 |
| LeetCode 48 - Rotate Image (Medium) (0) | 2021.03.04 |
| LeetCode 236 - Lowest Common Ancestor of a Binary Tree (Medium) (0) | 2021.03.04 |
| LeetCode 494 - Target Sum (Medium) (0) | 2021.03.04 |
๐ฌ ๋๊ธ