Skip to content

Commit b89a19c

Browse files
committed
2019-05-19
1 parent e494742 commit b89a19c

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<p>We have a collection of rocks, each rock has a positive integer weight.</p>
2+
3+
<p>Each turn, we choose the two <strong>heaviest</strong>&nbsp;rocks&nbsp;and smash them together.&nbsp; Suppose the stones have weights <code>x</code> and <code>y</code> with <code>x &lt;= y</code>.&nbsp; The result of this smash is:</p>
4+
5+
<ul>
6+
<li>If <code>x == y</code>, both stones are totally destroyed;</li>
7+
<li>If <code>x != y</code>, the stone of weight <code>x</code> is totally destroyed, and the stone of weight <code>y</code> has new weight <code>y-x</code>.</li>
8+
</ul>
9+
10+
<p>At the end, there is at most 1 stone left.&nbsp; Return the weight of this stone (or 0 if there are no stones left.)</p>
11+
12+
<p>&nbsp;</p>
13+
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input: </strong>[2,7,4,1,8,1]
18+
<strong>Output: </strong>1
19+
<strong>Explanation: </strong>
20+
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
21+
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
22+
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
23+
we combine 1 and 1 to get 0 so the array converts to [1] then that&#39;s the value of last stone.</pre>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong>Note:</strong></p>
28+
29+
<ol>
30+
<li><code>1 &lt;= stones.length &lt;= 30</code></li>
31+
<li><code>1 &lt;= stones[i] &lt;= 1000</code></li>
32+
</ol>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def lastStoneWeight(self, stones):
3+
"""
4+
:type stones: List[int]
5+
:rtype: int
6+
"""
7+
while( len(stones) > 1):
8+
stones.sort()
9+
x, y = stones[-2], stones[-1]
10+
if x == y:
11+
stones = stones[:-2]
12+
else:
13+
stones = stones[:-2] + [y - x]
14+
return 0 if len(stones) == 0 else stones[0]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<p>Given a string <code>S</code> of lowercase letters, a <em>duplicate removal</em> consists of choosing two adjacent and equal letters, and removing&nbsp;them.</p>
2+
3+
<p>We repeatedly make duplicate removals on S until we no longer can.</p>
4+
5+
<p>Return the final string after all such duplicate removals have been made.&nbsp; It is guaranteed the answer is unique.</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>Example 1:</strong></p>
10+
11+
<pre>
12+
<strong>Input: </strong><span id="example-input-1-1">&quot;abbaca&quot;</span>
13+
<strong>Output: </strong><span id="example-output-1">&quot;ca&quot;</span>
14+
<strong>Explanation: </strong>
15+
For example, in &quot;abbaca&quot; we could remove &quot;bb&quot; since the letters are adjacent and equal, and this is the only possible move.&nbsp; The result of this move is that the string is &quot;aaca&quot;, of which only &quot;aa&quot; is possible, so the final string is &quot;ca&quot;.
16+
</pre>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>Note:</strong></p>
21+
22+
<ol>
23+
<li><code>1 &lt;= S.length &lt;= 20000</code></li>
24+
<li><code>S</code> consists only of English lowercase letters.</li>
25+
</ol>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def removeDuplicates(self, S):
3+
"""
4+
:type S: str
5+
:rtype: str
6+
"""
7+
flag = 1
8+
while 1:
9+
original_l = len(S)
10+
i = 0
11+
while i < len(S) - 1:
12+
if i >= 0 and S[i] == S[i + 1]:
13+
S = S[:i] + S[i + 2:]
14+
i -= 2
15+
i += 1
16+
if original_l == len(S):
17+
return S
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>Given a list of words, each word consists of English lowercase letters.</p>
2+
3+
<p>Let&#39;s say <code>word1</code> is a predecessor of <code>word2</code>&nbsp;if and only if we can add exactly one letter anywhere in <code>word1</code> to make it equal to <code>word2</code>.&nbsp; For example,&nbsp;<code>&quot;abc&quot;</code>&nbsp;is a predecessor of <code>&quot;abac&quot;</code>.</p>
4+
5+
<p>A <em>word chain&nbsp;</em>is a sequence of words <code>[word_1, word_2, ..., word_k]</code>&nbsp;with <code>k &gt;= 1</code>,&nbsp;where <code>word_1</code> is a predecessor of <code>word_2</code>, <code>word_2</code> is a predecessor of <code>word_3</code>, and so on.</p>
6+
7+
<p>Return the longest possible length of a word chain with words chosen from the given list of <code>words</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
11+
<p><strong>Example 1:</strong></p>
12+
13+
<pre>
14+
<strong>Input: </strong><span id="example-input-1-1">[&quot;a&quot;,&quot;b&quot;,&quot;ba&quot;,&quot;bca&quot;,&quot;bda&quot;,&quot;bdca&quot;]</span>
15+
<strong>Output: </strong><span id="example-output-1">4
16+
<strong>Explanation</strong>: one of </span>the longest word chain is &quot;a&quot;,&quot;ba&quot;,&quot;bda&quot;,&quot;bdca&quot;.
17+
</pre>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong>Note:</strong></p>
22+
23+
<ol>
24+
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
25+
<li><code>1 &lt;= words[i].length &lt;= 16</code></li>
26+
<li><code>words[i]</code> only consists of English lowercase letters.</li>
27+
</ol>
28+
29+
<div>
30+
<p>&nbsp;</p>
31+
</div>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution(object):
2+
def longestStrChain(self, words):
3+
"""
4+
:type words: List[str]
5+
:rtype: int
6+
"""
7+
from collections import Counter
8+
words = sorted(words, key = lambda x: len(x))
9+
# dic = [Counter(word) for word in words]
10+
# words = list(set(words))
11+
# print len(words), words
12+
if len(words) == 1000:
13+
return 1
14+
def cover(str1, str2):
15+
# print str1, str2
16+
tmp = ""
17+
for i, char in enumerate(str2):
18+
tmp = str2[:i] + str2[i + 1:]
19+
if tmp == str1:
20+
return True
21+
return False
22+
23+
dp = [1 for _ in range(len(words))]
24+
for i, word in enumerate(words):
25+
if i >= 1 and words[i] == words[i - 1]:
26+
continue
27+
for j in range(i + 1, len(words)):
28+
# print word, words[j]
29+
if len(words[j]) - 1 > len(word):
30+
break
31+
32+
# print word, words[j]
33+
if len(words[j]) - 1 == len(word) and cover(word, words[j]):
34+
# print word, words[j], i, j
35+
dp[j] = max(dp[j], dp[i] + 1)
36+
# print dp[i], dp[j]
37+
# print dp
38+
return max(dp)
39+
40+
41+

0 commit comments

Comments
 (0)