-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-cost-to-separate-sentence-into-rows.py
101 lines (95 loc) · 3.29 KB
/
minimum-cost-to-separate-sentence-into-rows.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Time: O(s + n * k), n is the number of the word_lens
# Space: O(k)
class Solution(object):
def minimumCost(self, sentence, k):
"""
:type sentence: str
:type k: int
:rtype: int
"""
def lens(sentence):
j = len(sentence)-1
for i in reversed(xrange(-1, len(sentence))):
if i == -1 or sentence[i] == ' ':
yield j-i
j = i-1
word_lens, dp = [], [] # dp[i]: min cost of word_lens[-1-i:]
t = -1
for l in lens(sentence):
word_lens.append(l)
dp.append(float("inf"))
t += l+1
if t <= k:
dp[-1] = 0
continue
total = l
for j in reversed(xrange(len(dp)-1)):
dp[-1] = min(dp[-1], dp[j] + (k-total)**2)
total += (word_lens[j]+1)
if total > k:
word_lens = word_lens[j:] # minimize len(word_lens) s.t. sum(word_lens) > k
dp = dp[j:]
break
return dp[-1] if dp else 0
# Time: O(s + n * k), n is the number of the word_lens
# Space: O(n)
class Solution2(object):
def minimumCost(self, sentence, k):
"""
:type sentence: str
:type k: int
:rtype: int
"""
word_lens = []
j = 0
for i in xrange(len(sentence)+1):
if i != len(sentence) and sentence[i] != ' ':
continue
word_lens.append(i-j)
j = i+1
dp = [float("inf")]*(len(word_lens)) # dp[i]: min cost of word_lens[i:]
i, total = len(word_lens)-1, -1
while i >= 0 and total + (word_lens[i]+1) <= k: # find max i s.t. the length of the last line > k
total += (word_lens[i]+1)
dp[i] = 0
i -= 1
for i in reversed(xrange(i+1)):
total = word_lens[i]
for j in xrange(i+1, len(dp)):
dp[i] = min(dp[i], dp[j] + (k-total)**2)
total += (word_lens[j]+1)
if total > k:
break
return dp[0]
# Time: O(s + n * k), n is the number of the word_lens
# Space: O(n)
class Solution3(object):
def minimumCost(self, sentence, k):
"""
:type sentence: str
:type k: int
:rtype: int
"""
word_lens = []
j = 0
for i in xrange(len(sentence)+1):
if i != len(sentence) and sentence[i] != ' ':
continue
word_lens.append(i-j)
j = i+1
dp = [float("inf")]*(1+(len(word_lens)-1)) # dp[i]: min cost of the first i word_lens where i in [0, len(words)-1]
dp[0] = 0
for i in xrange(1, (len(word_lens)-1)+1):
total = word_lens[i-1]
for j in reversed(xrange(i)):
dp[i] = min(dp[i], dp[j] + (k-total)**2)
if j-1 < 0:
continue
total += (word_lens[j-1]+1)
if total > k:
break
i, total = len(word_lens)-1, -1
while i >= 0 and total + (word_lens[i]+1) <= k: # find max i s.t. the length of the last line > k
total += (word_lens[i]+1)
i -= 1
return min(dp[j] for j in xrange(i+1, len(dp)))