forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters