使用算法:
Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen.
Note:
A word cannot be split into two lines.
The order of words in the sentence must remain unchanged.
Two consecutive words in a line must be separated by a single space.
Total words in the sentence won't exceed 100.
Length of each word is greater than 0 and won't exceed 10.
1 ≤ rows, cols ≤ 20,000.
Example 1:
Input:
rows = 2, cols = 8, sentence = ["hello", "world"]
Output:
1
Explanation:
hello---
world---
The character '-' signifies an empty space on the screen.
Example 2:
Input:
rows = 3, cols = 6, sentence = ["a", "bcd", "e"]
Output:
2
Explanation:
a-bcd-
e-a---
bcd-e-
The character '-' signifies an empty space on the screen.
Example 3:
Input:
rows = 4, cols = 5, sentence = ["I", "had", "apple", "pie"]
Output:
1
Explanation:
I-had
apple
pie-I
had--
The character '-' signifies an empty space on the screen.
这是我的代码:
def words_typing(sentence, rows, cols)
count_words(sentence, rows, cols, cols, 0, 0)
end
def count_words(sentence, rows, cols, remaining_space, row_num, word_idx)
return 0 if row_num == rows #keep going until out of rows, ends the recursion
word_idx = 0 if word_idx == sentence.length #reset the word back to the first
if remaining_space >= sentence[word_idx].length
if remaining_space == sentence[word_idx].length
return 1 + count_words(sentence, rows, cols, remaining_space - sentence[word_idx].length, row_num, word_idx + 1 )
else #greater than 1
return 1 + count_words(sentence, rows, cols, remaining_space - sentence[word_idx].length - 1, row_num, word_idx + 1 )
end
else #move to a new line, reset remaining space
return count_words(sentence, rows, cols, cols, row_num+1, word_idx)
end
end
代码的工作原理如下。 word_idx是句子数组中单词的索引。剩余空间最初是列数。只要有足够的空间放下一个单词,我就会在同一行返回1 +函数调用,并且下一个单词和剩余的空间。如果剩余空间> = 1 +字长,那么我将考虑在两个连续单词之间留一个空格(这就是我有额外条件的原因)。
如果word_idx比句子数组长,它会重置为零。递归函数将继续运行,直到row_num现在大于问题中提供给我们的行数。
然而,这段代码并不起作用。我的输出通常大于正确的答案,但从概念上讲,所有这些似乎都适合我。有人看到我的做法有问题吗?
答案 0 :(得分:1)
这是因为你在计算单词而不是句子。
def words_typing(sentence, rows, cols)
count_words(sentence, rows, cols, cols, 0, 0, 0)
end
def count_words(sentence, rows, cols, remaining_space, row_num, word_idx, number_of_sentences)
nos = number_of_sentences
return nos if row_num == rows #keep going until out of rows, ends the recursion
if word_idx == sentence.length #reset the word back to the first
word_idx = 0
nos = number_of_sentences+1
end
if remaining_space >= sentence[word_idx].length
if remaining_space == sentence[word_idx].length
return count_words(sentence, rows, cols, remaining_space - sentence[word_idx].length, row_num, word_idx + 1, nos )
else #greater than 1
return count_words(sentence, rows, cols, remaining_space - sentence[word_idx].length - 1, row_num, word_idx + 1 , nos)
end
else #move to a new line, reset remaining space
return count_words(sentence, rows, cols, cols, row_num+1, word_idx, nos)
end
end
rows = 3
cols = 6
sentence = ["a", "bcd", "e"]
words_typing(sentence, rows, cols)
rows = 4; cols = 5; sentence = ["I", "had", "apple", "pie"]
words_typing(sentence, rows, cols)
我引入了一个包含多个句子的新变量/参数(最后一个)(在开始0)。当word_idx == sentence.length
表示新句子符合剩余空格时,nos = number_of_sentences+1
最后我们返回nos(句子数)。
答案 1 :(得分:0)
当您发现问题时,我想建议另一种方法来编写该方法。
sentence
我使用了方法Array#cycle。对于上面定义的loopy = sentence.each_with_index.cycle
#=> #<Enumerator: #<Enumerator: ["I", "had", "apple", "pie"]:each_with_index>:cycle>
loopy.first 10
#=> [["I", 0], ["had", 1], ["apple", 2], ["pie", 3],
# ["I", 0], ["had", 1], ["apple", 2], ["pie", 3],
# ["I", 0], ["had", 1]]
,
testFunction