C++ solution


  • 0
    V
    class Solution {
    public:
        int wordsTyping(vector<string>& sentence, int rows, int cols) {
            int rowIndex = 1;
            int remainingCols = cols;
            int index=0;
            int count=0;
            int size = sentence.size();
            vector<int> wlen;
            int wc = 0;
            for(int i=0; i<sentence.size(); i++) {
                wlen.push_back(sentence[i].length());
                wc += sentence[i].length();
                wc += 1;
            }
            while(rowIndex <= rows) {
                if(index == 0 && wc <= remainingCols) {
                    count += (remainingCols) / wc;
                    remainingCols = remainingCols % wc;
                } else {
                    if(wlen[index] <= remainingCols) {
                        if(index == size-1) {
                            count++;
                        }
                        remainingCols = remainingCols - wlen[index];
                        remainingCols -= 1;
                        index = (index + 1)%size;
                    } else {
                        rowIndex++;
                        remainingCols = cols;
                    }
                }
            }
            return(count);
        }
    };
    

Log in to reply
 

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