Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sentence-screen-fitting.cpp 982 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-10-10 02:13 +08:00 . Update sentence-screen-fitting.cpp
// Time: O(r + n * c)
// Space: O(n)
class Solution {
public:
int wordsTyping(vector<string>& sentence, int rows, int cols) {
vector<int> wc(sentence.size());
for (int i = 0; i < sentence.size(); ++i) {
wc[i] = wordsFit(sentence, i, cols);
}
int words = 0, start = 0;
for (int i = 0; i < rows; ++i) {
words += wc[start];
start = (start + wc[start]) % sentence.size();
}
return words / sentence.size();
}
private:
int wordsFit(const vector<string>& sentence, int start, int cols) {
if (sentence[start].length() > cols) {
return 0;
}
int sum = sentence[start].length(), count = 1;
for (int i = (start + 1) % sentence.size();
sum + 1 + sentence[i].length() <= cols;
i = (i + 1) % sentence.size()) {
sum += 1 + sentence[i].length();
++count;
}
return count;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助