Wednesday, December 26, 2018

806. Number of Lines To Write String



73.79 %
class Solution {
    public int[] numberOfLines(int[] widths, String S) {
        if (S == null || S.length() == 0) return new int[]{0, 0};
        int count = 1;
        int curr = 0;
        int len = 100; // can't exceed len for each row
        for (int i = 0; i < S.length(); i++) {
            if (curr + widths[S.charAt(i) - 'a'] > 100) {
                count++;
                curr = 0;
            }
            curr += widths[S.charAt(i) - 'a'];
        }
        return new int[]{count, curr};
    }
}

No comments:

Post a Comment