Again Very Concise C++ Solution. Tag should be easy.


  • -1
    class Solution {
    public:
        int wordsTyping(vector<string>& sentence, int rows, int cols) {
            int sz = sentence.size(), i = 0;
            for (int rcount = 0, j, len; rcount < rows; rcount++, i += j) {
                if (i && i % sz == 0) {
                    i = i * (rows / rcount);
                    if ((rcount = rcount * (rows / rcount)) == rows) break;
                }
                for (j = len = 0; len + sentence[(i + j) % sz].size() <= cols; j++) {
                    if (j && j % sz == 0 && cols / len > 1) {
                        j = j * (cols / len) - 1;
                        len = len * (cols / len);
                    }
                    else len += sentence[(i + j) % sz].size() + 1;
                }
            }
            return i / sz;
        }
    };
    

    My solution is not vert fast, only 86ms. I think I just handle the repeated cases in each rows and between rows. Can anyone tell me why is not fast ?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.