➕문제
https://leetcode.com/problems/number-of-lines-to-write-string/description/
➕풀이
character의 코드를 이용하면 쉽게 풀 수 있다. 시간 복자도는 O(n) 이다.
/**
* @param {number[]} widths
* @param {string} s
* @return {number[]}
*/
var numberOfLines = function (widths, s) {
let base = "a".charCodeAt(0);
let line = 1,
pixels = 0;
for (let i = 0; i < s.length; ++i) {
let width = widths[s.charCodeAt(i) - base];
if (pixels + width <= 100) {
pixels += width;
} else {
++line;
pixels = width;
}
}
return [line, pixels];
};
'🔒Algorithm' 카테고리의 다른 글
LeetCode > 2208 Minimum Operations to Halve Array Sum (0) | 2025.04.05 |
---|---|
LeetCode > 1346. Check If N and Its Double Exist (0) | 2025.04.03 |
LeetCode > 2022. Convert 1D Array Into 2D Array (0) | 2025.03.29 |
LeetCode > 2591. Distribute Money to Maximum Children (0) | 2025.03.28 |
LeetCode [JS] > 392. Is Subsequence (0) | 2025.03.27 |