LeetCode > 806. Number of Lines To Write String

2025. 4. 2. 23:23·🔒Algorithm

➕문제

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
'🔒Algorithm' 카테고리의 다른 글
  • LeetCode > 2208 Minimum Operations to Halve Array Sum
  • LeetCode > 1346. Check If N and Its Double Exist
  • LeetCode > 2022. Convert 1D Array Into 2D Array
  • LeetCode > 2591. Distribute Money to Maximum Children
devWarrior
devWarrior
  • devWarrior
    devWarrior
    devWarrior
  • 전체
    오늘
    어제
    • 🧩Dev (263)
      • ⭐FE (34)
      • 🔒Algorithm (155)
      • ➕Etc. (11)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
    • 관리
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    nodejs
    실버2
    leetcode
    코테
    react
    자바스크립트
    티스토리챌린지
    javascript
    프론트엔드
    실버3
    그리디
    DFS
    실버1
    BFS
    Algorithm
    실버4
    Lv2
    node.js
    자스
    FE
    백준
    알고리즘
    프로그래머스
    dp
    Easy
    골드5
    오블완
    js
    코딩테스트
    구현
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
devWarrior
LeetCode > 806. Number of Lines To Write String
상단으로

티스토리툴바