Skip to content

Commit 0934543

Browse files
committed
Solution for latest weekly contest
1 parent 7024e98 commit 0934543

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

1000-1100q/1046.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
We have a collection of rocks, each rock has a positive integer weight.
3+
4+
Each turn, we choose the two heaviest rocks and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is:
5+
6+
If x == y, both stones are totally destroyed;
7+
If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.
8+
At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.)
9+
10+
11+
12+
Example 1:
13+
14+
Input: [2,7,4,1,8,1]
15+
Output: 1
16+
Explanation:
17+
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
18+
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
19+
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
20+
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.
21+
22+
23+
Note:
24+
25+
1 <= stones.length <= 30
26+
1 <= stones[i] <= 1000
27+
'''
28+
29+
class Solution(object):
30+
def lastStoneWeight(self, stones):
31+
"""
32+
:type stones: List[int]
33+
:rtype: int
34+
"""
35+
while len(stones) > 1:
36+
max_x = max(stones)
37+
stones.remove(max_x)
38+
max_y = max(stones)
39+
stones.remove(max_y)
40+
41+
if max_x != max_y:
42+
stones.append(max_x-max_y)
43+
return stones[0] if stones else 0

1000-1100q/1047.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
3+
4+
We repeatedly make duplicate removals on S until we no longer can.
5+
6+
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
7+
8+
9+
10+
Example 1:
11+
12+
Input: "abbaca"
13+
Output: "ca"
14+
Explanation:
15+
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
16+
17+
18+
Note:
19+
20+
1 <= S.length <= 20000
21+
S consists only of English lowercase letters.
22+
'''
23+
24+
class Solution(object):
25+
def removeDuplicates(self, S):
26+
"""
27+
:type S: str
28+
:rtype: str
29+
"""
30+
stack = []
31+
if not S:
32+
return ""
33+
for char in S:
34+
if not stack:
35+
stack.append(char)
36+
else:
37+
first = stack[-1]
38+
if first == char:
39+
stack.pop()
40+
else:
41+
stack.append(char)
42+
if not stack:
43+
return ""
44+
return ''.join(stack)

1000-1100q/1048.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
Given a list of words, each word consists of English lowercase letters.
3+
4+
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
5+
6+
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
7+
8+
Return the longest possible length of a word chain with words chosen from the given list of words.
9+
10+
11+
12+
Example 1:
13+
14+
Input: ["a","b","ba","bca","bda","bdca"]
15+
Output: 4
16+
Explanation: one of the longest word chain is "a","ba","bda","bdca".
17+
18+
19+
Note:
20+
21+
1 <= words.length <= 1000
22+
1 <= words[i].length <= 16
23+
words[i] only consists of English lowercase letters.
24+
'''
25+
26+
class Solution(object):
27+
def longestStrChain(self, words):
28+
"""
29+
:type words: List[str]
30+
:rtype: int
31+
"""
32+
if not words:
33+
return 0
34+
words.sort(key=len)
35+
dp = collections.defaultdict(int)
36+
result = 0
37+
for word in words:
38+
for index in range(len(word)):
39+
char_excluded_string = word[:index] + word[index+1:]
40+
if char_excluded_string in dp:
41+
dp[word] = max(dp[char_excluded_string]+1, dp[word])
42+
else:
43+
dp[word] = max(dp[word], 1)
44+
result = max(dp[word], result)
45+
return result

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1515
##### [Problems 1000-1100](./1000-1100q/)
1616
| # | Title | Solution | Difficulty |
1717
|---| ----- | -------- | ---------- |
18+
|1048|[Longest String Chain](https://leetcode.com/problems/longest-string-chain)|[Python](./1000-1100q/1048.py)|Medium|
19+
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string)|[Python](./1000-1100q/1047.py)|Easy|
20+
|1046|[Last Stone Weight](https://leetcode.com/problems/last-stone-weight)|[Python](./1000-1100q/1046.py)|Easy|
1821
|1044|[Longest Duplicate Substring](https://leetcode.com/problems/longest-duplicate-substring)|[Python](./1000-1100q/1044.py)|Hard|
1922
|1043|[Partition Array for Maximum Sum](https://leetcode.com/problems/partition-array-for-maximum-sum/)|[Python](./1000-1100q/1043.py)|Medium|
2023
|1042|[Flower Planting With No Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent)|[Python](./1000-1100q/1042.py)|Easy|

0 commit comments

Comments
 (0)