[case of algorithm thousand questions] daily LeetCode punch in - 96. The number of lines required to write a string

Posted by GoodWill on Mon, 27 Dec 2021 22:27:29 +0100

πŸ“’ preface

πŸš€ Algorithm problem πŸš€
  • 🌲 Punching out an algorithm problem every day is not only a learning process, but also a sharing process 😜
  • 🌲 Tip: the problem-solving programming languages in this column are C# and Java
  • 🌲 To maintain a state of learning every day, let's work together to become the great God of algorithm 🧐!
  • 🌲 Today is the 96th day of punching out the force deduction algorithm 🎈!
πŸš€ Algorithm problem πŸš€

🌲 Sample of the original question: the number of lines required to write a string

We should write the given string S on each line from left to right. The maximum width of each line is 100 units. If we make this line more than 100 units when writing a letter, we should write this letter to the next line.

We give an array widths. The array widths[0] represents the units required by 'a', the array widths[1] represents the units required by 'b', and the array widths[25] represents the units required by 'z'.

Now answer two questions: at least how many lines can put down S, and how many units of width does the last line use? Return your answer as a list of integers of length 2.

Example 1:

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]
explain: 
All characters have the same occupation unit 10. So write all 26 letters,
We need 2 whole lines and one line occupying 60 units.

Example 2:

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]
explain: 
Remove letters'a'All characters are the same unit 10, and the string "bbbcccdddaa" 9 will be overwritten * 10 + 2 * 4 = 98 Units.
Last letter 'a' It will be written to the second line because there are only 2 units left in the first line.
So, the answer is 2 lines, and the second line has 4 unit widths.

Tips:

  • The length of string S is in the range of [1, 1000].
  • S contains only lowercase letters.
  • widths is an array with a length of 26.
  • The values of widths[i] range from [2, 10].

🌻 C# method: traversal

Traverse s first, and restart one line when each line is greater than 100

code:

public class Solution {
    public int[] NumberOfLines(int[] widths, string s) {
         int[] res = new int[2];
            int num = 0;
            int n = 1;
            for (int i = 0; i < s.Length; i++)
            {
                if (100-num>= widths[s[i] - 'a'])
                {
                    num += widths[s[i] - 'a'];
                }
                else
                {
                    num = 0;
                    num += widths[s[i] - 'a'];
                    n++;
                }
                
            }
            res[0] = n;
            res[1] = num;
            return res;
    }
}

results of enforcement

adopt
 Execution time: 128 ms,At all C# Beat 90.00% of users in submission
 Memory consumption: 39.4 MB,At all C# Defeated 70.90% of users in submission

🌻 Java method: simple traversal

Train of thought analysis
We traverse each letter in the string S from left to right, and use the lines (width) to count the current answer in real time.

When traversing a letter X, if width + widths[x] < = 100, update the width and keep the lines unchanged; If width + widths[x] > 100, the value of lines is increased by 1 and the width is set to widths[x].

code:

class Solution {
    public int[] numberOfLines(int[] widths, String S) {
        int lines = 1, width = 0;
        for (char c: S.toCharArray()) {
            int w = widths[c - 'a'];
            width += w;
            if (width > 100) {
                lines++;
                width = w;
            }
        }

        return new int[]{lines, width};
    }
}

results of enforcement

adopt
 Execution time: 0 ms,At all Java  Defeated 100 in submission.00%User
 Memory consumption: 36.3 MB,At all Java Beat 75 in submission.50%User

Complexity analysis

Time complexity: O( n )
Space complexity: O(1) 

πŸ’¬ summary

  • Today is the 96th day of punching out the force deduction algorithm!
  • This paper uses C# and Java programming languages to solve problems
  • Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
  • That's the end of today's algorithm sharing. See you tomorrow!

Topics: Algorithm leetcode