06 2020 档案

摘要:题目描述: 方法一:原地哈希 class Solution: def firstMissingPositive(self, nums: List[int]) -> int: n = len(nums) for i in range(n): if nums[i] <= 0: nums[i] = n + 阅读全文
posted @ 2020-06-27 21:47 oldby 阅读(146) 评论(0) 推荐(0)
摘要:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeDuplicateN 阅读全文
posted @ 2020-06-26 23:47 oldby 阅读(88) 评论(0) 推荐(0)
摘要:题目描述: 提交: class Solution: from typing import List def avoidFlood(self, rains: List[int]) -> List[int]: import heapq heap = [] res = [-1 if i != 0 else 阅读全文
posted @ 2020-06-26 09:45 oldby 阅读(406) 评论(0) 推荐(0)
摘要:题目描述: 第一次提交: 有错 未找出问题 留坑 class Solution: def getFolderNames(self, names: List[str]) -> List[str]: res = [] dic = collections.Counter() for name in nam 阅读全文
posted @ 2020-06-25 09:29 oldby 阅读(175) 评论(0) 推荐(0)
摘要:方法一: class Solution: def patternMatching(self, pattern: str, value: str) -> bool: count_a = sum(1 for ch in pattern if ch == 'a') count_b = len(patter 阅读全文
posted @ 2020-06-22 23:38 oldby 阅读(201) 评论(0) 推荐(0)
摘要:方法:递归 O(n) O(n) class Solution: def __init__(self): self.maxSum = float("-inf") def maxPathSum(self, root: TreeNode) -> int: def maxGain(node): if not 阅读全文
posted @ 2020-06-22 00:03 oldby 阅读(59) 评论(0) 推荐(0)
摘要:提交: class Solution: def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: count = collections.Counter(arr) order_dic = sorted(count.items 阅读全文
posted @ 2020-06-19 22:37 oldby 阅读(223) 评论(0) 推荐(0)
摘要:方法:倍增法dp ACM经典 class TreeAncestor: def __init__(self, n: int, parent: List[int]): self.cols = 20 # log(50000) < 20 self.dp = [[-1] * self.cols for _ i 阅读全文
posted @ 2020-06-19 22:36 oldby 阅读(281) 评论(0) 推荐(0)
摘要:方法:迭代+栈 O(N) class Solution { public TreeNode recoverFromPreorder(String S) { Deque<TreeNode> path = new LinkedList<>(); int pos = 0; while(pos < S.le 阅读全文
posted @ 2020-06-19 00:07 oldby 阅读(135) 评论(0) 推荐(0)
摘要:解:O(N) class Solution: def maxScoreSightseeingPair(self, A: List[int]) -> int: left, res = A[0], -1 for j in range(1, len(A)): res = max(res, left + A 阅读全文
posted @ 2020-06-17 23:48 oldby 阅读(141) 评论(0) 推荐(0)
摘要:题目描述: 提交:二分 O(nlogk) class Solution: def minDays(self, bloomDay: List[int], m: int, k: int) -> int: minday = min(bloomDay) maxday = max(bloomDay) def 阅读全文
posted @ 2020-06-17 20:20 oldby 阅读(307) 评论(0) 推荐(0)
摘要:方法:动态规划 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 阅读全文
posted @ 2020-06-17 19:33 oldby 阅读(156) 评论(0) 推荐(0)
摘要:题目描述: 提交: class Solution: def finalPrices(self, prices: List[int]) -> List[int]: res = [] for i in range(len(prices)): for j in range(i,len(prices)): 阅读全文
posted @ 2020-06-17 17:46 oldby 阅读(257) 评论(0) 推荐(0)
摘要:提交: class SubrectangleQueries: def __init__(self, rectangle: List[List[int]]): self.list = rectangle def updateSubrectangle(self, row1: int, col1: int 阅读全文
posted @ 2020-06-17 17:44 oldby 阅读(150) 评论(0) 推荐(0)
摘要:题目描述: 阅读全文
posted @ 2020-06-15 23:34 oldby 阅读(213) 评论(0) 推荐(0)
摘要:方法一:dp+前缀和 O(N) class Solution: def minSumOfLengths(self, arr: List[int], target: int) -> int: n = len(arr) # dp[i]记录从第i个元素(包含第i个)往前找到的和为target最短子数组长度 阅读全文
posted @ 2020-06-15 10:42 oldby 阅读(270) 评论(0) 推荐(0)
摘要:题目描述: 解法一:二分法 提交: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 阅读全文
posted @ 2020-06-14 23:50 oldby 阅读(161) 评论(0) 推荐(0)
摘要:题目描述: 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 阅读全文
posted @ 2020-06-11 11:10 oldby 阅读(228) 评论(0) 推荐(0)
摘要:题目描述: 提交: class BrowserHistory: def __init__(self, homepage: str): self.list = [homepage] self.cur = 0 def visit(self, url: str) -> None: if self.cur 阅读全文
posted @ 2020-06-11 10:51 oldby 阅读(180) 评论(0) 推荐(0)
摘要:题目描述: 提交: class Solution: def getStrongest(self, arr: List[int], k: int) -> List[int]: arr = sorted(arr) m = arr[(len(arr) - 1) // 2] res = [] tmp = s 阅读全文
posted @ 2020-06-11 10:46 oldby 阅读(132) 评论(0) 推荐(0)
摘要:题目描述: 提交: 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 阅读全文
posted @ 2020-06-11 10:41 oldby 阅读(158) 评论(0) 推荐(0)
摘要:贪心算法 特征: • 在对问题求解时,总是做出在当前看来是最好的选择 • 基本思路: • 建立数学模型来描述问题。 • 把求解的问题分成若干个子问题。 • 对每一子问题求解,得到子问题的局部最优解。 • 把子问题的解局部最优解合成原来解问题的一个解。 • 贪心策略适用的前提是:局部最优策略能导致产生 阅读全文
posted @ 2020-06-11 10:27 oldby 阅读(410) 评论(0) 推荐(0)
摘要:题目描述: 方法一: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( 阅读全文
posted @ 2020-06-05 00:00 oldby 阅读(123) 评论(0) 推荐(0)
摘要:题目描述: 提价: class Solution: def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int: horizontalCuts.sort() vertical 阅读全文
posted @ 2020-06-01 10:30 oldby 阅读(177) 评论(0) 推荐(0)
摘要:题目描述: 提交: class Solution: def minReorder(self, n: int, connections: List[List[int]]) -> int: connect = collections.defaultdict(set) c_set = set(tuple( 阅读全文
posted @ 2020-06-01 10:27 oldby 阅读(167) 评论(0) 推荐(0)
摘要:题目描述: 提交: 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 阅读全文
posted @ 2020-06-01 10:13 oldby 阅读(124) 评论(0) 推荐(0)
摘要:题目描述: 方法:动态规划 自底向上: 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) 阅读全文
posted @ 2020-06-01 10:06 oldby 阅读(233) 评论(0) 推荐(0)
摘要:题目描述: 提交:超时 class Solution: def checkIfPrerequisite(self, n: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]: dic = colle 阅读全文
posted @ 2020-06-01 09:23 oldby 阅读(183) 评论(0) 推荐(0)