add: 220412-CN [cpp]

This commit is contained in:
eat-swap 2022-04-12 21:22:14 +08:00
parent b443a444bf
commit a045303b88
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 28 additions and 1 deletions

27
cpp/2204/220412-CN.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <vector>
#include <string>
/**
* 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<int> numberOfLines(const std::vector<int>& 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};
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2204)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220411.cpp)
ADD_EXECUTABLE(2204 220412-CN.cpp)