int n = gas.size(); vector<bool> flag(n + 5, false); vector<int> pos(n + 5, 0); vector<int> vt(n + 5, 0);
for(int i = 0; i < n; ++i) flag[i] = (gas[i] >= cost[i]);
//for(int i = 0; i < n; ++i) cout << i << ' ' << flag[i] << '\n'; for(int i = n - 1; i >= 0; --i) { if(!flag[i]) continue; int sum = gas[i] - cost[i]; int p = i; //cout << i << ' ' << sum << ' ' << p << '\n'; while(true) { p = (p + 1) % n; if(p == i) return i; if(flag[p] && p > i) { sum += vt[p]; p = pos[p]; }
int n = gas.size(); int x = 0; while(x < n) { int y = x, sum = 0, cnt = 0; while(cnt < n) { y = (x + cnt) % n; sum += gas[y] - cost[y]; if(sum >= 0) ++cnt; elsebreak; }
int right = 1, ans = 0; for(int i = n - 1; i >= 0; --i) { if(i < n - 1 && ratings[i] > ratings[i + 1]) ++right; else right = 1; ans += max(left[i], right); }
classSolution { public: vector<string> fullJustify(vector<string>& words, int maxWidth){ vector<vector<string>> tmp(305, vector<string>()); vector<int> sLen(305, 0); vector<string> ans; string s; int n = words.size(); int cnt = 0; int sum = 0; int len = maxWidth; for(int i = 0; i < n; ++i) { if(len >= (int)words[i].size()) { tmp[cnt].push_back(words[i]); sum += words[i].size(); } else { len = maxWidth; tmp[++cnt].push_back(words[i]); sum = words[i].size(); } len -= words[i].size(); --len; sLen[cnt] = sum; }
int wordscnt, wordslen, spaceCnt, spaceLen, less, mod, lessCnt, L; for(int i = 0; i < 305; ++i) { constauto& vt = tmp[i]; if(vt.size() == 1) { s = vt.front(); spaceLen = maxWidth - s.size(); while(spaceLen--) s += " "; ans.push_back(s); if(tmp[i + 1].size() == 0) break; } else { s = ""; spaceLen = maxWidth - sLen[i]; spaceCnt = vt.size() - 1; wordslen = sLen[i]; wordscnt = vt.size(); less = spaceLen / spaceCnt; mod = spaceLen % spaceCnt; lessCnt = spaceCnt - mod; if(tmp[i + 1].size() == 0) { for(int j = 0; j < vt.size(); ++j) { s += vt[j]; if(j != vt.size() - 1) s += " "; } L = s.size(); for(int j = 0; j < maxWidth - L; ++j) s += " "; ans.push_back(s); break; } else { for(int j = 0; j < vt.size(); ++j) { s += vt[j]; if(j == vt.size() - 1) break; if(mod) { for(int k = 0; k <= less; ++k) s += " "; --mod; } elsefor(int k = 0; k < less; ++k) s += " "; } ans.push_back(s); } }