File tree Expand file tree Collapse file tree 3 files changed +144
-0
lines changed
Expand file tree Collapse file tree 3 files changed +144
-0
lines changed Original file line number Diff line number Diff line change 1+ ###23 . Merge k Sorted Lists
2+
3+
4+
5+ 题目:
6+ < https://leetcode.com/problems/merge-k-sorted-lists/ >
7+
8+
9+ 难度:
10+ Hard
11+
12+ 思路:
13+
14+ 看到思路有heap,similar question有ugly number|| -》 这个是用heapq来解决的
15+
16+ 那么就用heap吧? heapsort
17+
18+ 最简单的做法是只要每个list里面还有node,就把他们扔到minheap里面去,然后再把minheap pop,一个一个node连起来,听起来时间复杂度和空间复杂度都蛮高的。
19+ 直接merge必然是不好的,因为没有利用有序这个点,应该做的是每次取来一个,然后再把应该的下一个放入
20+
21+ 写到这里瞬间明白和ugly number ii像的点了,甚至感觉跟find in sorted matrix ii也像
22+
23+ ```
24+ class Solution(object):
25+ def mergeKLists(self, lists):
26+ """
27+ :type lists: List[ListNode]
28+ :rtype: ListNode
29+ """
30+ import heapq
31+ h = []
32+
33+ for listhead in lists:
34+ if listhead:
35+ heapq.heappush(h, (listhead.val,listhead))
36+
37+ cur = ListNode(-1)
38+ dummy = cur
39+
40+ while h:
41+ smallestNode = heapq.heappop(h)[1]
42+ cur.next = smallestNode
43+ cur = cur.next
44+ if smallestNode.next:
45+ heapq.heappush(h, (smallestNode.next.val,smallestNode.next))
46+ return dummy.next
47+ ```
48+
49+ 当然还像merge two sorted list
Original file line number Diff line number Diff line change 1+ ###152 . Maximum Product Subarray
2+
3+
4+
5+ 题目:
6+ < https://leetcode.com/problems/maximum-product-subarray/ >
7+
8+
9+ 难度:
10+ Medium
11+
12+ 思路:
13+
14+ 粗一看, 一股浓烈的DP气息飘来,想要套用53题的思路和方程。但是这个跟sum是不一样的,因为乘积可以正负正负的跳,这样的动归方程肯定是不对的
15+
16+ dp[ i] = max(dp[ i-1] * a[ i] ,a[ i] )
17+
18+ 举个例子 : [ -2,3,-4]
19+
20+
21+ 用O(N^2)超时,厉害啊!
22+
23+ 想,可不可以记录+的和-的,记录两个dp数组,我哭了,真的是这样做的
24+
25+ 最大值可能来源于最小值 -> 哲学般的句子
26+
27+ ```
28+ class Solution(object):
29+ def maxProduct(self, nums):
30+ """
31+ :type nums: List[int]
32+ :rtype: int
33+ """
34+ n = len(nums)
35+ maxdp = [ nums[0] for i in range(n)]
36+ mindp = [ nums[0] for i in range(n)]
37+
38+
39+ for i in range(1,n):
40+ maxdp[i] = max(mindp[i-1]*nums[i], maxdp[i-1]*nums[i],nums[i])
41+ mindp[i] = min(maxdp[i-1]*nums[i], mindp[i-1]*nums[i],nums[i])
42+
43+ return max(maxdp)
44+ ```
Original file line number Diff line number Diff line change 1+ ###437 . Path Sum III
2+
3+
4+
5+ 题目:
6+ < https://leetcode.com/problems/path-sum-iii/ >
7+
8+
9+ 难度:
10+ Easy
11+
12+ 思路:
13+
14+ 这题一看到,时间复杂度就把我给吓尿了,不是么|||科科
15+
16+ 我的🐟👄的代码
17+
18+ 是有优化,待做
19+
20+
21+
22+ ```
23+ # Definition for a binary tree node.
24+ # class TreeNode(object):
25+ # def __init__(self, x):
26+ # self.val = x
27+ # self.left = None
28+ # self.right = None
29+
30+ class Solution(object):
31+ def pathSum(self, root, sum):
32+ """
33+ :type root: TreeNode
34+ :type sum: int
35+ :rtype: int
36+ """
37+ # empty
38+ def nodeValue(root, sum):
39+ if not root: return 0
40+ # one node
41+ if root.val == sum:
42+ return 1 + nodeValue(root.left, 0) + nodeValue(root.right,0)
43+ else:
44+ return nodeValue(root.left, sum - root.val) + nodeValue(root.right, sum - root.val)
45+
46+ if not root: return 0
47+ ans = nodeValue(root,sum)
48+ ans += self.pathSum(root.left, sum)
49+ ans += self.pathSum(root.right, sum)
50+ return ans
51+ ```
You can’t perform that action at this time.
0 commit comments