Skip to content

Commit

Permalink
Solution for latest weekly contest
Browse files Browse the repository at this point in the history
  • Loading branch information
Garvit244 committed May 20, 2019
1 parent 7024e98 commit 0934543
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 1000-1100q/1046.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'''
We have a collection of rocks, each rock has a positive integer weight.
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:
If x == y, both stones are totally destroyed;
If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.
At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.)
Example 1:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.
Note:
1 <= stones.length <= 30
1 <= stones[i] <= 1000
'''

class Solution(object):
def lastStoneWeight(self, stones):
"""
:type stones: List[int]
:rtype: int
"""
while len(stones) > 1:
max_x = max(stones)
stones.remove(max_x)
max_y = max(stones)
stones.remove(max_y)

if max_x != max_y:
stones.append(max_x-max_y)
return stones[0] if stones else 0
44 changes: 44 additions & 0 deletions 1000-1100q/1047.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
We repeatedly make duplicate removals on S until we no longer can.
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
Example 1:
Input: "abbaca"
Output: "ca"
Explanation:
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".
Note:
1 <= S.length <= 20000
S consists only of English lowercase letters.
'''

class Solution(object):
def removeDuplicates(self, S):
"""
:type S: str
:rtype: str
"""
stack = []
if not S:
return ""
for char in S:
if not stack:
stack.append(char)
else:
first = stack[-1]
if first == char:
stack.pop()
else:
stack.append(char)
if not stack:
return ""
return ''.join(stack)
45 changes: 45 additions & 0 deletions 1000-1100q/1048.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'''
Given a list of words, each word consists of English lowercase letters.
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".
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.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
'''

class Solution(object):
def longestStrChain(self, words):
"""
:type words: List[str]
:rtype: int
"""
if not words:
return 0
words.sort(key=len)
dp = collections.defaultdict(int)
result = 0
for word in words:
for index in range(len(word)):
char_excluded_string = word[:index] + word[index+1:]
if char_excluded_string in dp:
dp[word] = max(dp[char_excluded_string]+1, dp[word])
else:
dp[word] = max(dp[word], 1)
result = max(dp[word], result)
return result
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
##### [Problems 1000-1100](./1000-1100q/)
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1048|[Longest String Chain](https://leetcode.com/problems/longest-string-chain)|[Python](./1000-1100q/1048.py)|Medium|
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string)|[Python](./1000-1100q/1047.py)|Easy|
|1046|[Last Stone Weight](https://leetcode.com/problems/last-stone-weight)|[Python](./1000-1100q/1046.py)|Easy|
|1044|[Longest Duplicate Substring](https://leetcode.com/problems/longest-duplicate-substring)|[Python](./1000-1100q/1044.py)|Hard|
|1043|[Partition Array for Maximum Sum](https://leetcode.com/problems/partition-array-for-maximum-sum/)|[Python](./1000-1100q/1043.py)|Medium|
|1042|[Flower Planting With No Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent)|[Python](./1000-1100q/1042.py)|Easy|
Expand Down

0 comments on commit 0934543

Please sign in to comment.