随笔分类 - leetcode
摘要:方法:动态规划 class Solution: def minDistance(self, houses: List[int], k: int) -> int: houses.sort() n = len(houses) cost = [[0]*n for _ in range(n)] for i
阅读全文
摘要:题目描述: 提交: class Solution: def finalPrices(self, prices: List[int]) -> List[int]: res = [] for i in range(len(prices)): for j in range(i,len(prices)):
阅读全文
摘要:方法一:dp+前缀和 O(N) class Solution: def minSumOfLengths(self, arr: List[int], target: int) -> int: n = len(arr) # dp[i]记录从第i个元素(包含第i个)往前找到的和为target最短子数组长度
阅读全文
摘要:题目描述: 解法一:二分法 提交:O(nlogn) class Solution: def findBestValue(self, arr: List[int], target: int) -> int: def helper(num): res = 0 for i in arr: if i > n
阅读全文
摘要:题目描述: class Solution: def minCost(self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int) -> int: fcur = [[-1] * n for _ in range
阅读全文
摘要:题目描述: 提交: class BrowserHistory: def __init__(self, homepage: str): self.list = [homepage] self.cur = 0 def visit(self, url: str) -> None: if self.cur
阅读全文
摘要:题目描述: 提交: class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr = sorted(arr) m = arr[(len(arr) - 1) // 2] res = [] tmp = s
阅读全文
摘要:题目描述: 提交: class Solution: def shuffle(self, nums: List[int], n: int) -> List[int]: nums1 = nums[:n] nums2 = nums[n:] res = [] for i in range(n): res.a
阅读全文
摘要:题目描述: 方法一:O(N) O(N) class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: L = [1] * len(nums) R = [1] * len(nums) for i in range(
阅读全文
摘要:题目描述: 提价: class Solution: def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int: horizontalCuts.sort() vertical
阅读全文
摘要:题目描述: 提交: class Solution: def minReorder(self, n: int, connections: List[List[int]]) -> int: connect = collections.defaultdict(set) c_set = set(tuple(
阅读全文
摘要:题目描述: 提交: class Solution: def hasAllCodes(self, s: str, k: int) -> bool: k_list = set() if k > len(s): return False for i in range(len(s) - k + 1): if
阅读全文
摘要:题目描述: 方法:动态规划 自底向上: class Solution: def cherryPickup(self, A): R, C=len(A), len(A[0]) from functools import lru_cache @lru_cache(None) def dp(r,c1,c2)
阅读全文
摘要:题目描述: 提交:超时 class Solution: def checkIfPrerequisite(self, n: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]: dic = colle
阅读全文
摘要:方法一: 二分法 O(nlogn) O(1) class Solution { public int findDuplicate(int[] nums) { int len = nums.length; int left = 1; int right = len - 1; while(left <
阅读全文
摘要:题目描述: 提交: class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n1,n2 = len(nums1),len(nums2) dp = [[0] * n2 for _ in ra
阅读全文
摘要:题目描述: 提交: class Solution: def pseudoPalindromicPaths (self, root: TreeNode) -> int: def helper(l,node): left,right = 0,0 if node.left == None and node
阅读全文
摘要:提交: class Solution: def maxVowels(self, s: str, k: int) -> int: n = len(s) dp = [0] * (n + 1) for i in range(n): if s[i] in "aeiou": dp[i + 1] = dp[i]
阅读全文
摘要:题目描述: 提交: class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: n = len(searchWord) sentence_l = sentence.split(" ") for i
阅读全文
浙公网安备 33010602011771号