摘要:
84. 柱状图中最大的矩形 1、双指针 class Solution: def largestRectangleArea(self, heights: List[int]) -> int: n = len(heights) # 左右第一个小于 i 的下标 min_l, min_r = [0] * n 阅读全文
摘要:
[503.下一个更大元素II] 循环问题用 2*n , i % n 的方式 n = len(nums) ans = [-1] * n stack = [] for i in range(2 * n): while len(stack) > 0 and nums[i % n] > nums[stack 阅读全文
摘要:
739. 每日温度 class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: n = len(temperatures) ans = [0] * n # 单调增的栈 stack = [] fo 阅读全文
摘要:
647. 回文子串 1、中心扩散法+双指针 class Solution: def countSubstrings(self, s: str) -> int: res = 0 for i in range(len(s)): # 以 i 为中心 res += self.countPalind(i, i 阅读全文
摘要:
583. 两个字符串的删除操作 class Solution: def minDistance(self, word1: str, word2: str) -> int: n, m = len(word1), len(word2) # dp 数组代表使得 word1 以 i-1 结尾和 word2 阅读全文
摘要:
392.判断子序列 1、双指针 class Solution: def isSubsequence(self, s: str, t: str) -> bool: m, n = len(s), len(t) i, j = 0, 0 while m > i and n > j: if s[i] == t 阅读全文
摘要:
1143.最长公共子序列 class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m, n = len(text1), len(text2) # dp 数组代表 text1 以 i-1 结尾 阅读全文
摘要:
300.最长递增子序列 class Solution: def lengthOfLIS(self, nums: List[int]) -> int: if len(nums) <= 1: return len(nums) # dp 数组代表以下标 i 结尾包含 nums[i] 的最长递增子序列长度为 阅读全文
摘要:
309.最佳买卖股票时机含冷冻期 class Solution: def maxProfit(self, prices: List[int]) -> int: # dp[i][0] 持有股票 # dp[i][1] 卖出股票那一天 # dp[i][2] 冷冻期 # dp[i][3] 保持卖出股票的状态 阅读全文
摘要:
123.买卖股票的最佳时机III class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) <= 1: return 0 if len(prices) == 2: return max(0, price 阅读全文