Skip to content

Commit a9ff712

Browse files
committedJul 13, 2020
174 350
1 parent 6b69903 commit a9ff712

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed
 

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Leetcode刷题记录
2828

2929
+ No.1 [cpp](cpp/1.cpp) [python](python/1.py)
3030
+ No.3 [cpp](cpp/3.cpp)
31+
+ No.350 [cpp](cpp/350.cpp) [python](python/350.py)
3132

3233
### 双指针
3334

@@ -68,7 +69,7 @@ Leetcode刷题记录
6869
+ No.837 [cpp](cpp/837.cpp) [python](python/837.py)
6970
+ No.126 [cpp](cpp/126.cpp) [python](python/126.py)
7071
+ 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)
7273

7374
#### 状态机DP
7475

‎cpp/174.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
};

‎cpp/350.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
};

‎python/174.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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]

‎python/350.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

0 commit comments

Comments
 (0)
Please sign in to comment.