문자열을 쓸 줄 수

4896 단어 leetcodejavascript
var numberOfLines = function (widths, s) {
  let dummyAlphabet = "abcdefghijklmnopqrstuvwxyz";
  let map = new Map();
  let maxCountInLine = 100;
  let rows = 0;

  for (let i = 0; i < dummyAlphabet.length; i++) {
    map.set(dummyAlphabet[i], widths[i]);
  }

  for (let i = 0; i < s.length; i++) {
    let char = s[i];
    let width = map.get(char);
    maxCountInLine = maxCountInLine - width;
    if (maxCountInLine === 0) {
      rows++;
      maxCountInLine = 100;
    } else if (maxCountInLine < 0) {
      rows++;
      maxCountInLine = 100;
      maxCountInLine = maxCountInLine - width;
    }
  }
  let nextLineUsed = 100 - maxCountInLine;
  let fRows = nextLineUsed > 0 ? rows + 1 : rows;
  return [fRows, nextLineUsed === 0 ? 100 : nextLineUsed];
};


좋은 웹페이지 즐겨찾기