注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
刚结束电面
一开始自我介绍,然后 10 分钟八- /*
- We are building a word processor and we would like to implement word wrapping functionality that is a bit smarter.
- Some users want to have the resulting lines to be as balanced as possible for higher readability. Therefore, our application should divide a given text into a sequence of lines so that every line spans at most some fixed width and the difference between different lines are as little as possible.
- We define the balance score of a collection of strings as follows: find the longest line. For each line, sum the square of
- the length difference between this line and the longest line.
- Given a string of text and a maximum number of characters in a line, return a list of strings which result in the lowest balance score (on a tie, return any of the valid solutions).
- Note: we are using '-' instead of spaces between words to make testing and visual verification of the results easier.
- Examples:
- text1 = "Seven six eight thirteen four five sixteen"
- balancedWrapLines(text1, 15) "balance the lines of text1 wrapping to a max 15 length"=>
- [ "Seven-six-eight", // 0
- "thirteen-four", // 2*2
- "five-sixteen" ] // 3*3 = score of 13
- text2 = "XXXXXX X XXXX X X XXXXX";
- balancedWrapLines(text2, 9) "balance the lines of text2 wrapping to a max 9 length"=>
- Correct answer (each line wrapped to a different length <= 9):
- [ "XXXXXX-X", // 0*0
- "XXXX-X", // 2*2
- "X-XXXXX" ] // 1*1 = score of 5
- Or
- [ "XXXXXX", // 2*2
- "X-XXXX-X", // 0*0
- "X-XXXXX" ] // 1*1 = score of 5
- Wrong answer (wrapping to the same length for each line):
- [ 'XXXXXX-X', // 0
- 'XXXX-X-X', // 0
- 'XXXXX' ] // 3*3 = score of 9
- All Test Cases:
- text, max line length
- balancedWrapLines(text1, 15)
- balancedWrapLines(text2, 9)
- n = number of words OR total characters
- m = length of longest line
- */
复制代码 |