2023年9月1日 星期五

9/1 每日一題(根據單字和對應寬度, 每 寬度達100時換行, 計算換了幾行,以及最後一行的寬度)

 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.



class Solution:
    def numberOfLines(self, widths: List[int], s: str) -> List[int]:
        word='abcdefghijklmnopqrstuvwxyz'
        list_s=list(word)
       
        my_dict={}
        for k,v in zip(list_s,widths):
            my_dict[k]=v
       
        total=0
        res=[]
        string=''
        for i in s:
            if total + my_dict[i] > 100:
                total = 0
                res.append(string)
                string=''
            total += my_dict[i]
            string += i
        res.append(string)


        return [len(res),total]
       

-----------------------------------


class Solution:
    def numberOfLines(self, widths: List[int], s: str) -> List[int]:
        lines=1
        count=0
        for char in s:
            if widths[string.ascii_lowercase.index(char)] + count >100:
                lines+=1
                count=widths[string.ascii_lowercase.index(char)]
            else:
                count+=widths[string.ascii_lowercase.index(char)]
        return [lines,count]

-----------------
class Solution:
    def numberOfLines(self, widths: List[int], s: str) -> List[int]:
        line_breaks = 0
        width = 0
        for c in s:
            c_width = widths[ord(c) - ord('a')]
            if width + c_width > 100:
                line_breaks += 1
                width = c_width
            else:
                width += c_width

        return [line_breaks + 1, width]







標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁