From a045303b888a387872350e5ac0bc39ced71282b1 Mon Sep 17 00:00:00 2001 From: eat-swap Date: Tue, 12 Apr 2022 21:22:14 +0800 Subject: [PATCH] add: 220412-CN [cpp] --- cpp/2204/220412-CN.cpp | 27 +++++++++++++++++++++++++++ cpp/2204/CMakeLists.txt | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 cpp/2204/220412-CN.cpp diff --git a/cpp/2204/220412-CN.cpp b/cpp/2204/220412-CN.cpp new file mode 100644 index 0000000..cbb21ce --- /dev/null +++ b/cpp/2204/220412-CN.cpp @@ -0,0 +1,27 @@ +#include +#include + +/** + * 806. Number of Lines To Write String + * You are given a string s of lowercase English letters and an array widths denoting how many pixels wide each lowercase English letter is. Specifically, widths[0] is the width of 'a', widths[1] is the width of 'b', and so on. + * You are trying to write s across several lines, where each line is no longer than 100 pixels. Starting at the beginning of s, write as many letters on the first line such that the total width does not exceed 100 pixels. Then, from where you stopped in s, continue writing as many letters as you can on the second line. Continue this process until you have written all of s. + * Return an array result of length 2 where: + * result[0] is the total number of lines. + * result[1] is the width of the last line in pixels. + */ + +class Solution { +public: + static std::vector numberOfLines(const std::vector& widths, const std::string& s) { + int line = 1, cur = 0; + for (const char ch : s) { + if (widths[ch - 'a'] + cur <= 100) { + cur += widths[ch - 'a']; + } else { + cur = widths[ch - 'a']; + ++line; + } + } + return {line, cur}; + } +}; diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index e69da70..365726c 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220411.cpp) +ADD_EXECUTABLE(2204 220412-CN.cpp)