Number of Lines To Write String

šŸ  ā¬…ļø āž”ļø

You are given a string s of lowercase English letters and an array widths denoting how many pixels wide each lowercase English letter is. Specifically, widths[0] is the width of 'a', widths[1] is the width of 'b', and so on.

You are trying to write s across several lines, where each line is no longer than 100 pixels. Starting at the beginning of s, write as many letters on the first line such that the total width does not exceed 100 pixels. Then, from where you stopped in s, continue writing as many letters as you can on the second line. Continue this process until you have written all of s.

Return an array result of length 2 where:

  • result[0] is the total number of lines.
  • result[1] is the width of the last line in pixels.

Example 1:

Input: widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], s = "abcdefghijklmnopqrstuvwxyz " Output: [3,60] Explanation: You can write s as follows: abcdefghij // 100 pixels wide klmnopqrst // 100 pixels wide uvwxyz // 60 pixels wide There are a total of 3 lines, and the last line is 60 pixels wide.

Example 2:

Input: widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], s = "bbbcccdddaaa " Output: [2,4] Explanation: You can write s as follows: bbbcccdddaa // 98 pixels wide a // 4 pixels wide There are a total of 2 lines, and the last line is 4 pixels wide.

Constraints:

  • widths.length == 26
  • 2 <= widths[i] <= 10
  • 1 <= s.length <= 1000
  • s contains only lowercase English letters.

Note: This problem is from LeetCode.
āŒ Compiled
āŒ Executed
āŒ Correct
program main
    implicit none

    integer :: i, j, k, n, m, result(2)
    character(len=100) :: s
    integer, dimension(26) :: widths

    ! Example 1
    widths = (/10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10/)
    s = 'abcdefghijklmnopqrstuvwxyz '
    call solve(widths, s, result)
    print *, result

    ! Example 2
    widths = (/4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10/)
    s = 'bbbcccdddaaa '
    call solve(widths, s, result)
    print *, result

contains

    subroutine solve(widths, s, result)
        implicit none
        integer, intent(in) :: widths(:), s(:)
        integer, intent(out) :: result(2)
        integer :: i, j, k, n, m

        n = size(s)
        m = size(widths)

        do i = 1, n
            if (sum(widths(1:i)) > 100) then
                exit
            end if
        end do

        result(1) = i - 1

        do j = i, n
            if (sum(widths(j-i+1:m)) > 100) then
                exit
            end if
        end do

        result(2) = sum(widths(j-i+1:m))

    end subroutine solve

end program main
āœ… Compiled
āŒ Executed
āŒ Correct
program main
      implicit none

      integer, parameter :: MAX_WIDTH = 100
      integer, parameter :: MAX_LETTERS = 26
      integer, parameter :: MAX_STRING_LENGTH = 1000

      integer :: widths(MAX_LETTERS)
      character(len=MAX_STRING_LENGTH) :: s
      integer :: result(2)

      integer :: i, j, k, l
      integer :: line_count, line_width

      read(*,*) widths
      read(*,*) s

      line_count = 1
      line_width = 0

      do i = 1, len_trim(s)
          if (line_width + widths(ichar(s(i:i))) > MAX_WIDTH) then
              line_count = line_count + 1
              line_width = 0
          end if
          line_width = line_width + widths(ichar(s(i:i)))
      end do

      result(1) = line_count
      result(2) = line_width

      write(*,*) result

      end program main
šŸŒ Data from online sources
def numberOfWays(n):
    MOD = 1000000007
    dp = [0] * (n + 1)
    dp[0] = 1
    dp[1] = 1

    for i in range(2, n + 1):
        dp[i] = (dp[i - 1] + dp[i - 2]) % MOD

    return dp[n]
The optimal way to solve this problem is to use dynamic programming. The idea is to maintain a dp array where dp[i] holds the number of ways to tile a 2 x i board. Initialize dp[0] and dp[1] to 1 (base cases). Then, for each i from 2 to n, we calculate dp[i] as the sum of dp[i - 1] and dp[i - 2]. For each i, two cases are possible:
  • Place a 2 x 1 domino tile vertically at position i to fill the 2 x i-1 board (use the last solution at dp[i-1]).
  • Place two 2 x 1 domino tiles horizontally at position i-1 and i (use the second last solution at dp[i-2]).

After iterating through the entire dp array, the result will be in dp[n]. Return this value modulo 109 + 7.

šŸŒ Data from online sources
int numberOfWays(int n) {
    int MOD = 1000000007;
    vector<int> dp(n + 1, 0);
    dp[0] = 1;
    dp[1] = 1;

    for (int i = 2; i <= n; ++i)
        dp[i] = (dp[i - 1] + dp[i - 2]) % MOD;

    return dp[n];
}
The optimal way to solve this problem is to use dynamic programming. The idea is to maintain a dp array where dp[i] holds the number of ways to tile a 2 x i board. Initialize dp[0] and dp[1] to 1 (base cases). Then, for each i from 2 to n, we calculate dp[i] as the sum of dp[i - 1] and dp[i - 2]. For each i, two cases are possible:
  • Place a 2 x 1 domino tile vertically at position i to fill the 2 x i-1 board (use the last solution at dp[i-1]).
  • Place two 2 x 1 domino tiles horizontally at position i-1 and i (use the second last solution at dp[i-2]).

After iterating through the entire dp array, the result will be in dp[n]. Return this value modulo 109 + 7.