LeetCode Number of Lines To Write String Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

thanks for explaining this one Nick, i could not understand the question lol

sye
Автор

yea good one on this, I didn't quite get it too.

I instead setting the values after, I set it to int[]{1, 0} and then instead of using a variable to store number of lines, I just incremented the first index.

public int[] numberOfLines(int[] widths, String s) {
int width = 0;
int[] result = new int[] {1, 0};
for(char c : s.toCharArray()) {
int char_width = widths[c - 'a'];
if(char_width + width > 100) {
result[0]++;
width = 0;
}
width += char_width;
}
result[1] = width;
return result;
}

BenjaminKane-rs