File tree 5 files changed +66
-1
lines changed
5 files changed +66
-1
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ Leetcode刷题记录
28
28
29
29
+ No.1 [ cpp] ( cpp/1.cpp ) [ python] ( python/1.py )
30
30
+ No.3 [ cpp] ( cpp/3.cpp )
31
+ + No.350 [ cpp] ( cpp/350.cpp ) [ python] ( python/350.py )
31
32
32
33
### 双指针
33
34
@@ -68,7 +69,7 @@ Leetcode刷题记录
68
69
+ No.837 [ cpp] ( cpp/837.cpp ) [ python] ( python/837.py )
69
70
+ No.126 [ cpp] ( cpp/126.cpp ) [ python] ( python/126.py )
70
71
+ No.1014 [ cpp] ( cpp/1014.cpp ) [ python] ( python/1014.py ) easy
71
- + No.174 [ java] ( java/Problem174.java )
72
+ + No.174 [ cpp ] ( cpp/174.cpp ) [ python ] ( python/174.py ) [ java] ( java/Problem174.java )
72
73
73
74
#### 状态机DP
74
75
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ int calculateMinimumHP (vector<vector<int >>& dungeon) {
7
+ int m = dungeon.size (), n = dungeon[0 ].size ();
8
+ // 为了少写一个判断,增加一行一列
9
+ vector<vector<int >> dp (m+1 , vector<int >(n+1 , 100000 ));
10
+ dp[m][n-1 ] = dp[m-1 ][n] = 1 ; // 不能死
11
+ for (int i = m-1 ; i >= 0 ; --i) {
12
+ for (int j = n-1 ; j >= 0 ; --j) {
13
+ // 找到最少的路
14
+ int min_pre = min (dp[i+1 ][j], dp[i][j+1 ]);
15
+ dp[i][j] = max (min_pre - dungeon[i][j], 1 ); // 如果小于零证明血量够后面用了,改为0通过前面的路即可
16
+ }
17
+ }
18
+ return dp[0 ][0 ];
19
+ }
20
+ };
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ vector<int > intersect (vector<int >& nums1, vector<int >& nums2) {
7
+ unordered_map<int , int > hashmap;
8
+ // unordered_set<int> set;
9
+ vector<int > res;
10
+ for (int num1: nums1) ++hashmap[num1];
11
+ for (int num2: nums2)
12
+ if (hashmap.find (num2) != hashmap.end ())
13
+ if (hashmap[num2]) {
14
+ res.push_back (num2);
15
+ --hashmap[num2];
16
+ }
17
+ return res;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def calculateMinimumHP (self , dungeon : List [List [int ]]) -> int :
3
+ m , n = len (dungeon ), len (dungeon [0 ])
4
+ dp = [[100000 ] * (n + 1 ) for _ in range (m + 1 )]
5
+ dp [n ][m - 1 ] = dp [n - 1 ][m ] = 1
6
+
7
+ for i in range (m - 1 , - 1 , - 1 ):
8
+ for j in range (n - 1 , - 1 , - 1 ):
9
+ minn = min (dp [i + 1 ][j ], dp [i ][j + 1 ])
10
+ dp [i ][j ] = max (minn - dungeon [i ][j ], 1 )
11
+
12
+ return dp [0 ][0 ]
Original file line number Diff line number Diff line change
1
+ from collections import Counter
2
+ class Solution :
3
+ def intersect (self , nums1 : List [int ], nums2 : List [int ]) -> List [int ]:
4
+ hashtable = Counter ()
5
+ result = []
6
+ for num1 in nums1 :
7
+ hashtable [num1 ] += 1
8
+ for num2 in nums2 :
9
+ if num2 in hashtable :
10
+ if hashtable [num2 ]:
11
+ result .append (num2 )
12
+ hashtable [num2 ] -= 1
13
+ return result
You can’t perform that action at this time.
0 commit comments