greedy(4)
-
[Leetcode] 649. Dota2 Senate
🗓️ Daily LeetCoding Challenge May, Day 4 class Solution: def predictPartyVictory(self, senate: str) -> str: A = collections.deque() people = [0, 0] bans = [0, 0] for person in senate: x = person == 'R' people[x] += 1 A.append(x) while all(people): x = A.popleft() people[x] -= 1 if bans[x]: bans[x] -= 1 else: people[x] +=1 bans[x^1] += 1 A.append(x) return 'Radiant' if people[1] else 'Dire'
2023.05.05 -
[Leetcode] 121. Best Time to Buy and Sell Stock
Time Complexity: O(N) Space Complexity: O(N) (Worst -> len(maxlist) = n/2 class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) 0: minlist.append(prices[n2] - prices[n1]) n1 = i n2 = i + 1 elif prices[i+1] > prices[n2]: n2 = i + 1 if prices[n2]..
2023.05.02 -
[Leetcode 334] 334. Increasing Triplet Subsequence
class Solution: def increasingTriplet(self, nums: List[int]) -> bool: n1 = n2 = float('inf') for n in nums: if n < n1: n1 = n elif n1 < n < n2: n2 = n elif n1 < n2 < n: return True return False
2023.05.02 -
[Leetcode] 11. Container With Most Water
class Solution: def maxArea(self, height: List[int]) -> int: left, right = 0, len(height)-1 result = 0 while left
2023.04.27