This file contains hidden or 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
class Solution(object): | |
def canWinNim(self, n): | |
""" | |
:type n: int | |
:rtype: bool | |
""" | |
return n % 4 | |
''' | |
Runtime: 8 ms, faster than 96.34% of Python online submissions for Nim Game. | |
Memory Usage: 11.5 MB, less than 100.00% of Python online submissions for Nim Game. |
This file contains hidden or 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
class Solution: | |
def maxArea(self, height: List[int]) -> int: | |
lo = 0 | |
hi = len(height)-1 | |
cmax = 0 | |
while(lo < hi): | |
cmax = max(min(height[lo],height[hi]) * (hi - lo), cmax) | |
if (height[lo] > height[hi]): | |
hi -= 1 |
This file contains hidden or 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
class Solution: | |
def maxProfit(self, prices: List[int]) -> int: | |
price = [] | |
cmax = 0 | |
for i in prices[::-1]: | |
cmax = i if i > cmax else cmax | |
price.append(cmax) | |
cmax = 0 |
This file contains hidden or 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
class Solution: | |
def longestPalindrome(self, s: str) -> int: | |
ms = set() | |
count = 0 | |
for i in s: | |
if i in ms: | |
ms.discard(i) | |
count += 2 |
This file contains hidden or 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
class Solution(object): | |
def findComplement(self, num): | |
""" | |
:type num: int | |
:rtype: int | |
""" | |
binary = [int(i) ^ 1 for i in bin(num)[2:]] | |
return int("".join(map(str,binary)),2) | |
''' | |
Runtime: 12 ms, faster than 89.42% of Python online submissions for Number Complement. |
This file contains hidden or 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
class Solution(object): | |
def removeDuplicates(self, S): | |
""" | |
:type S: str | |
:rtype: str | |
""" | |
if not len(S): | |
return S | |
This file contains hidden or 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
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution(object): | |
def bstToGst(self, root): | |
""" |
This file contains hidden or 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
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution(object): | |
def insertIntoBST(self, root, val): | |
""" |
This file contains hidden or 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
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution(object): | |
def searchBST(self, root, val): | |
""" |
This file contains hidden or 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
class Solution(object): | |
def lengthOfLongestSubstring(self, s): | |
""" | |
:type s: str | |
:rtype: int | |
""" | |
''' | |
Intuition: |
NewerOlder