Algorithm
文章平均质量分 76
Sengo_1993
你多走了许多弯路,却看到了更多的风景。你觉得一切为时已晚,却恰是刚刚好的开始。
展开
-
leetcode刷题笔记-DP2
418.Sentence Screen Fittingclass Solution: def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int: ''' 1. Based on the above observation, in the first for loop we compute the number of words that can be pl..原创 2022-04-14 14:01:31 · 461 阅读 · 1 评论 -
leetcode刷题笔记-DFS/BFS 2
1293. Shortest Path in a Grid with Obstacles Eliminationfrom collections import dequeclass Solution: def shortestPath(self, grid: List[List[int]], k: int) -> int: ''' Solution ExplanationBecause we are trying to find the shortest原创 2021-02-01 15:10:14 · 292 阅读 · 0 评论 -
leetcode刷题笔记-Greedy贪婪算法
330.Patching Arrayclass Solution(object): def minPatches(self, nums, n): i, far, re = 0, 0, 0 nums = sorted(nums) while far < n : if i < len(nums...原创 2018-11-22 00:39:20 · 971 阅读 · 1 评论 -
leetcode刷题笔记-Monotonic queue/Stack
239.Sliding Window Maximumhttps://www.youtube.com/watch?v=2SXqBsTR6a8class Solution(object): def maxSlidingWindow(self, nums, k): """ :type nums: List[int] :type k...原创 2018-10-15 23:06:57 · 752 阅读 · 1 评论 -
leetcode刷题笔记-Parentheses
22. Generate Parentheses dfs stack思路:If you have two stacks, one for n "(", the other for n ")", you generate a binary tree from these two stacks of left/right parentheses to form an output st...原创 2018-10-14 09:32:31 · 329 阅读 · 0 评论 -
leetcode刷题笔记-trie
复习时候做421642.Design Search Autocomplete Systemclass TrieNode(): def __init__(self): self.children = collections.defaultdict(TrieNode) self.isEnd = False self.rank ...原创 2018-10-06 08:07:22 · 617 阅读 · 0 评论 -
leetcode笔记-union find并查集
parents = range(n) rank = [0] * n def find(edge): if parents[edge] != edge: parents[edge] = find(parents[edge]) return parents[edge] ...原创 2018-10-12 08:49:14 · 627 阅读 · 1 评论 -
leetcode刷题笔记-design
588.Design In-Memory File System在trie分类362.Design Hit Counterclass HitCounter(object): # hit: O(1). get_hit: O(300). This solution will scale perfectly! def __init__(self): ...原创 2018-10-03 10:07:24 · 567 阅读 · 0 评论 -
Leetcode刷题笔记-sort
Quick Sort O(nlogn)选最后一个num为pivot,从left遍历到right, 比它小的元素全部放到前面去。然后再把自己也放到前面去,它后面的都比它大,它前面的都比它小。返回它的索引。然后重复 它的左边,和右边按照之前的步骤。def quickSort(arr, l, r): if l < r: idx = partition(arr,......原创 2018-09-11 10:05:22 · 407 阅读 · 1 评论 -
leetcode刷题笔记-难题整理
472.Concatenated Words和这题类似的139.Word Breakhttps://leetcode.com/problems/word-break/description/解法一DPpython超时,但是JAVA解法可以通过。也是139题的解法延伸。class Solution(object): def findAllConcatenate...原创 2018-11-30 05:23:35 · 774 阅读 · 0 评论 -
leetcode刷题笔记-two pointer
713.Subarray Product Less Than KTime is O(N) and space is O(1)class Solution(object): def numSubarrayProductLessThanK(self, nums, k): i = re = product = 0 for j, v in enumera...原创 2019-01-12 01:05:19 · 323 阅读 · 1 评论 -
leetcode刷题笔记-0-1knapsack背包问题
474. Ones and Zeroes DP 01背包问题思路: For dp[i][j][k], we can get it by fetching the current string i or discarding the current string, which would result in dp[i][j][k] = dp[i-1][j-numOfZero(strs[i...原创 2019-02-26 09:19:17 · 1476 阅读 · 2 评论 -
leetcode刷题笔记-graph
399.Evaluate Divisionclass Solution(object): def calcEquation(self, equations, values, queries): graph = collections.defaultdict(set) for i, vertices in enumerate(eq...原创 2019-02-21 07:04:55 · 311 阅读 · 1 评论 -
Leetcode刷题笔记-最大矩形,最大正方形
221.Maximal Square思路:This problem can be solved by dynamic programming. They key to DP is the state equation. In this problem, we define the state asthe maximal size of the square that can be ...原创 2019-03-10 06:22:48 · 644 阅读 · 0 评论 -
Leetcode刷题笔记-两个字符串比较dp
72.Edit Distancef(i, j) := minimum cost (or steps) required to convert first i characters of word1 to first j characters of word2class Solution(object): def minDistance(self, word1, word...原创 2019-03-10 08:56:22 · 592 阅读 · 1 评论 -
leetcode刷题笔记-substring题
76.Minimum Window Substring有模板的题!https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problemsclass Solution(object)...原创 2019-03-11 08:59:32 · 470 阅读 · 0 评论 -
Leetcode刷题笔记-两点间距离
675.Cut Off Trees for Golf Eventclass Solution(object): def cutOffTree(self, forest): # add border 0 so we do not need to index-checks later on forest.append([0] * len(for...原创 2019-03-16 06:16:12 · 641 阅读 · 0 评论 -
leetcode刷题整理-用java做的题
JAVA一些语法整理:Queue:Queue<String> queue = new LinkedList<String>();queue.offer("a"); // 添加一个元素并返回true queue.poll(); // get and pop the first elementqueue.peek(); // 返回队列头部的元素 ...原创 2019-03-13 04:07:47 · 3615 阅读 · 0 评论 -
Leetcode刷题笔记-Array2
386.Lexicographical Numbersclass Solution(object): def lexicalOrder(self, n): def dfs(num): if num > n: return res.append(num) for i in xr...原创 2019-03-24 11:36:46 · 305 阅读 · 1 评论 -
leetcode刷题笔记-binary search
410.Split Array Largest Sum还有一种dp的解法,没有二分法好,写在dp分类里面思路:https://leetcode.com/problems/split-array-largest-sum/discuss/89817/Clear-Explanation%3A-8ms-Binary-Search-Java用Go写的:func split...原创 2018-09-24 23:40:32 · 451 阅读 · 0 评论 -
leetcode刷题笔记-后续遍历
332. Reconstruct ItineraryGiven a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man wh...原创 2018-08-23 05:51:30 · 331 阅读 · 0 评论 -
leetcode刷题笔记-topological sort拓扑排序(DFS)
207.Course Schedule 找环There are a total ofncourses you have to take, labeled from0ton-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, whi...原创 2018-08-23 04:38:44 · 1679 阅读 · 1 评论 -
leetcode刷题笔记-回溯 Backtracking
140.Word Break IIclass Solution(object): def wordBreak(self, s, wordDict): return self.helper(s, wordDict, {}) def helper(self, s, wordDict, memo): if s in memo: ret...原创 2018-08-20 03:33:54 · 527 阅读 · 0 评论 -
leetcode刷题笔记-stack
255.Verify Preorder Sequence in Binary Search Treeclass Solution(object): def verifyPreorder(self, preorder): if not preorder:return True largerThan = float('-inf') s...原创 2018-08-20 02:57:49 · 349 阅读 · 0 评论 -
leetcode刷题笔记-DFS and BFS
212.Word Search II在trie分类529.Minesweeper不会点击数字的,所以不用考虑这点class Solution(object): def updateBoard(self, board, click): # 1. 如果是M雷的话,改成X结束 # 2. 如果不是M的话,附近有雷就改成数字结束。否则就是...原创 2018-08-19 09:07:25 · 1229 阅读 · 0 评论 -
Leetcode刷题笔记,palindromic strings回文
516.Longest Palindromic SubsequenceOne possible longest palindromic subsequence is "bb".class Solution(object): def longestPalindromeSubseq(self, s): """ :type s: str ...原创 2018-08-18 23:46:44 · 196 阅读 · 0 评论 -
Leetcode刷题笔记-dp
44.Wildcard Matchingfunc isMatch(s string, p string) bool { dp := make([][]bool, len(s)+1) // dp[s_idx][p_idx] for i := range dp { dp[i] = make([]bool, len(p)+1) } ...原创 2018-08-12 03:50:02 · 1137 阅读 · 1 评论 -
Leetcode刷题笔记-array
723.Candy Crushclass Solution(object): def candyCrush(self, board): # 只有横着或者竖着连续3个才可以消去。 # 把满足消去的设置为负数,比如2设置为-2,最后再一起消去 r, c = len(board), len(board[0]) fo...原创 2018-08-04 06:47:35 · 1394 阅读 · 1 评论 -
leetcode刷题笔记-Dijkstra‘s algorithm
Dijkstra's algorithm具体:https://blog.csdn.net/cjc211322/article/details/24933909505.The Maze IIclass Solution(object): def shortestDistance(self, maze, start, destination): des = ...原创 2018-08-20 22:52:37 · 8228 阅读 · 0 评论 -
Leetcode刷题笔记-Tree
Background:https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to t...原创 2018-08-20 23:12:23 · 783 阅读 · 1 评论 -
leetcode刷题笔记-bit
338.Counting Bitsclass Solution(object): def countBits(self, num): dp = [0] * (num+1) for i in xrange(num+1): dp[i] = dp[i/2] + i%2 return dp260.Sin...原创 2018-09-07 11:49:48 · 305 阅读 · 1 评论 -
leetcode刷题笔记-merge sort
Merge sort我研一上学期时候的笔记:148.Sort List简直就是我的笔记的代码版本。# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = No...原创 2018-09-07 10:31:49 · 797 阅读 · 0 评论 -
leetcode刷题笔记-heap
。原创 2018-09-07 09:33:01 · 921 阅读 · 0 评论 -
Leetcode刷题笔记-不知道怎么分类的题
731.My Calendar IIoverlap的部分放重复的,只要检查overlap。class MyCalendarTwo(object): # O(N) def __init__(self): self.overlap = [] self.nums = [] def book(self, start, end):...原创 2018-09-12 23:43:35 · 382 阅读 · 0 评论 -
leetcode刷题笔记-hashtable
939.Minimum Area Rectangle给一些点,找最小的rectangle的面积。方法2比较简单明了。https://leetcode.com/problems/minimum-area-rectangle/solution/380.Insert Delete GetRandom O(1)class RandomizedSet(object): de...原创 2018-09-07 02:18:45 · 533 阅读 · 0 评论 -
Leetcode刷题笔记-linklist
369.Plus One Linked Listp1指向最后面非9的数字。p2指向结尾。 class Solution(object): def plusOne(self, head): # two pointers dummy = ListNode(0) dummy.next = head p1 = p2 = ...原创 2018-09-08 05:41:59 · 621 阅读 · 0 评论 -
leetcode刷题笔记-math
780.Reaching PointsFirst while: x, y comes fromx, k*x+y or x+k*y, yfor each stepSecond:check if we reduce target points to (x, y+kx) or (x+ky, y)class Solution(object): def reachingP...原创 2018-09-04 05:32:05 · 646 阅读 · 1 评论 -
leetcode刷题笔记-string
809.Expressive Wordsclass Solution(object): def expressiveWords(self, S, words): re = 0 def check(W, S): i, j, s, w = 0, 0, len(S), len(W) for i in ...原创 2018-08-23 23:41:16 · 751 阅读 · 1 评论 -
DP回顾篇
前言:知识真的是学习了,总结了,练习了,运用了,再次总结归纳了,但是还是会一点点遗忘。动态规划(DP)又是算法里面非常非常重要的部分,所以一定要再次复习一遍。写这篇博客的时候不由想起上学期算法课上的一个小插曲。老师问有没有喜欢音乐的,一堆人举手。然后老师随便找了个黑人同学上去,让他用手在桌子上拍一首大家都知道的歌的节奏,让全班同学都来猜歌名。(全班70人左右)。拍了半天,有几个同学举手回答...原创 2018-03-11 04:18:48 · 278 阅读 · 0 评论