164. Max Words Visible on Scrollable Screen

Asked in

Max Words Visible on Scrollable Screen
You are given a list of words that must be written on a scrollable screen.

The screen has a fixed number of visible rows and a fixed number of columns in each row.

Words are written from left to right in the given order.

A word cannot be split across multiple lines.

If the current word does not fit in the current row, it must be written at the beginning of the next row.

Adjacent words on the same row must be separated by exactly one space.

When a new row is created and the number of written rows becomes greater than the number of visible rows, the screen scrolls up by one row and removes the oldest row from the visible screen.

Write an implementation for a method that returns the maximum number of complete words visible on the screen at any time while writing all words.

Method Signature

int maxWordsVisible(List<String> words, int rows, int cols)

Parameters

  • words: A list of words written on the screen in the given order.
  • rows: The number of rows visible on the screen.
  • cols: The maximum number of characters allowed in each row.

Return Value

Return the maximum number of complete words visible on the screen at any time while writing all words.

Rules

  • Words must be processed in the same order as they appear in words.
  • A word cannot be split across multiple rows.
  • Each row may contain one or more words.
  • Adjacent words on the same row must have exactly one space between them.
  • There is no leading or trailing space in any row.
  • If a word does not fit in the current row, it starts a new row.
  • If the number of written rows becomes greater than rows, the oldest visible row is removed due to scrolling.
  • The answer is the largest number of words visible after any word is written.

Constraints

  • 1 ≤ words.size() ≤ 100,000
  • 1 ≤ rows ≤ 10,000
  • 1 ≤ cols ≤ 10,000
  • 1 ≤ words[i].length() ≤ cols
  • words[i] contains only lowercase English letters.
  • No parameter value will be null.

Example 1

maxWordsVisible(words = ["hello", "world", "a", "b", "c"], rows = 2, cols = 10) returns 4.

Explanation:
The rows are formed as:
"hello"
"world a b"
"c"

At one point, the visible screen contains "hello" and "world a b", so 4 words are visible. After "c" is written, the screen scrolls and the visible rows are "world a b" and "c", which also contains 4 words. Therefore, the maximum number of visible words is 4.

Example 2

maxWordsVisible(words = ["a", "bb", "ccc", "dd", "e"], rows = 2, cols = 6) returns 4.

Explanation:
The rows are formed as:
"a bb"
"ccc dd"
"e"

The maximum visible screen is:
"a bb"
"ccc dd"

This contains 4 words.

Example 3

maxWordsVisible(words = ["a", "b", "c", "de", "f"], rows = 1, cols = 5) returns 3.

Explanation:
Since only one row is visible, every new row removes the previous row from the screen.

The rows are:
"a b c"
"de f"

The maximum number of words visible in one row is 3.

Example 4

maxWordsVisible(words = ["abc", "de", "f", "ghij", "k", "lm", "n"], rows = 3, cols = 7) returns 7.

Explanation:
The rows are formed as:
"abc de"
"f ghij"
"k lm n"

All 3 rows are visible together and contain 7 words.




Please use Laptop/Desktop or any other large screen to add/edit code.