|
1 | | -学习笔记 |
| 1 | +# 本周作业 |
| 2 | + |
| 3 | +- [柠檬水找零](https://leetcode-cn.com/problems/lemonade-change/description/)(亚马逊在半年内面试中考过) |
| 4 | + |
| 5 | + ```c++ |
| 6 | + class Solution { |
| 7 | + public: |
| 8 | + bool lemonadeChange(vector<int>& bills) { |
| 9 | + // 贪心 |
| 10 | + map<int, int> have; |
| 11 | + for (int i = 0; i < bills.size(); i++) { |
| 12 | + // 模拟 加 贪心 |
| 13 | + if (bills[i] == 5) { |
| 14 | + if (have.count(5) == 1) have[5] += 1; |
| 15 | + else have[5] = 1; |
| 16 | + } else if (bills[i] == 10) { |
| 17 | + // 先判断是否有钱找 |
| 18 | + if (have.count(5) == 1 && have[5] > 0) have[5] -= 1; |
| 19 | + else return false; |
| 20 | + // 拿10元 |
| 21 | + if (have.count(10) == 1) have[10] += 1; |
| 22 | + else have[10] = 1; |
| 23 | + // 给20的可以贪心,因为希望保留5元 有10元就给10元 |
| 24 | + } else { |
| 25 | + // 有10元 |
| 26 | + if (have.count(10) == 1 && have[10] > 0) { |
| 27 | + have[10] -= 1; |
| 28 | + // 有5元找5元, 没有五元直接错误 |
| 29 | + if (have.count(5) == 1 && have[5] > 0) have[5] -= 1; |
| 30 | + else return false; |
| 31 | + // 没有10元 |
| 32 | + } else { |
| 33 | + if (have.count(5) == 1 && have[5] >= 3) have[5] -= 3; |
| 34 | + else return false; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + return true; |
| 39 | + } |
| 40 | + }; |
| 41 | + ``` |
| 42 | +
|
| 43 | +- [买卖股票的最佳时机 II ](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/)(亚马逊、字节跳动、微软在半年内面试中考过) |
| 44 | +
|
| 45 | + ```c++ |
| 46 | + class Solution { |
| 47 | + public: |
| 48 | + int maxProfit(vector<int>& prices) { |
| 49 | + // 双指针 |
| 50 | + int left = 0, right, ans = 0; |
| 51 | + while (left < prices.size()) { |
| 52 | + // 确定最小的做指针 |
| 53 | + while (left + 1 < prices.size() && prices[left + 1] < prices[left]) left++; |
| 54 | + right = left + 1; |
| 55 | + // 确定最大的右指针 |
| 56 | + while (right + 1 < prices.size() && prices[right + 1] > prices[right]) right++; |
| 57 | + if (right < prices.size()) ans += prices[right] -prices[left]; |
| 58 | + // 下一次从 right + 1 开始 |
| 59 | + left = right + 1; |
| 60 | + } |
| 61 | + return ans; |
| 62 | + } |
| 63 | + }; |
| 64 | + ``` |
| 65 | + |
| 66 | +- [分发饼干](https://leetcode-cn.com/problems/assign-cookies/description/)(亚马逊在半年内面试中考过) |
| 67 | + |
| 68 | + ```c++ |
| 69 | + class Solution: |
| 70 | + def findContentChildren(self, g: List[int], s: List[int]) -> int: |
| 71 | + s.sort() |
| 72 | + g.sort() |
| 73 | + len_s = len(s) |
| 74 | + len_g = len(g) |
| 75 | + # 贪心 |
| 76 | + i = j = result = 0 |
| 77 | + while i < len_g and j < len_s: |
| 78 | + if s[j] >= g[i]: |
| 79 | + result += 1 |
| 80 | + i += 1 |
| 81 | + j += 1 |
| 82 | + return result |
| 83 | + ``` |
| 84 | +
|
| 85 | +- [岛屿数量](https://leetcode-cn.com/problems/number-of-islands/)(近半年内,亚马逊在面试中考查此题达到 350 次) |
| 86 | +
|
| 87 | + - BFS |
| 88 | +
|
| 89 | + ```c++ |
| 90 | + class Solution { |
| 91 | + public: |
| 92 | + int numIslands(vector<vector<char>>& grid) { |
| 93 | + int count = 0; |
| 94 | + for (int row = 0; row < grid.size(); row++) { |
| 95 | + for (int col = 0; col < grid[0].size(); col++) { |
| 96 | + if (grid[row][col] == '1') { |
| 97 | + bfs(row, col, grid); |
| 98 | + count++; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return count; |
| 103 | + } |
| 104 | + void bfs(int row, int col, vector<vector<char>>& grid) { |
| 105 | + queue<pair<int, int>> que; |
| 106 | + que.push({row, col}); |
| 107 | + // 下面一行是先访问再入队列的写法。 |
| 108 | + grid[row][col] = '0'; |
| 109 | + int maxrow = grid.size(); |
| 110 | + int maxcol = grid[0].size(); |
| 111 | + // 队列保证一层数据在一起 如果每次遍历一层再访问,那么每一层的每一个元素会多很多重复操作(周围),每次入队列的时候就visited会减少时间复杂度 |
| 112 | + while (!que.empty()) { |
| 113 | + // 访问当前值 |
| 114 | + pair<int, int> cur = que.front(); |
| 115 | + que.pop(); |
| 116 | + int i = cur.first; |
| 117 | + int j = cur.second; |
| 118 | + // grid[i][j] = '0'; |
| 119 | + // 下一层入队列 左右上下 |
| 120 | + if (0 <= i - 1 && grid[i - 1][j] == '1') { |
| 121 | + que.push({i - 1, j}); |
| 122 | + grid[i - 1][j] = '0'; |
| 123 | + } |
| 124 | + if (i + 1 < maxrow && grid[i + 1][j] == '1') { |
| 125 | + que.push({i + 1, j}); |
| 126 | + grid[i + 1][j] = '0'; |
| 127 | + } |
| 128 | + if (0 <= j - 1 && grid[i][j - 1] == '1'){ |
| 129 | + que.push({i, j - 1}); |
| 130 | + grid[i][j - 1] = '0'; |
| 131 | + } |
| 132 | + if (j + 1 < maxcol && grid[i][j + 1] == '1') { |
| 133 | + que.push({i, j + 1}); |
| 134 | + grid[i][j + 1] = '0'; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + }; |
| 139 | + ``` |
| 140 | + |
| 141 | + + DFS |
| 142 | + |
| 143 | + ```c++ |
| 144 | + class Solution: |
| 145 | + def numIslands(self, grid: List[List[str]]) -> int: |
| 146 | + result = 0 |
| 147 | + for row in range(len(grid)): |
| 148 | + for col in range(len(grid[0])): |
| 149 | + if grid[row][col] == '1': |
| 150 | + self.dfs(row, col, grid) |
| 151 | + result += 1 |
| 152 | + return result |
| 153 | + |
| 154 | + |
| 155 | + def dfs(self, row, col, grid): |
| 156 | + # terminal |
| 157 | + if row < 0 or col < 0 or row >= len(grid) or col >= len(grid[0]) or grid[row][col] != '1': |
| 158 | + return |
| 159 | + # visited |
| 160 | + grid[row][col] = '0' |
| 161 | + |
| 162 | + # next |
| 163 | + self.dfs(row - 1, col, grid) |
| 164 | + self.dfs(row + 1, col, grid) |
| 165 | + self.dfs(row, col - 1, grid) |
| 166 | + self.dfs(row, col + 1, grid) |
| 167 | + |
| 168 | + ``` |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +# 本周总结 |
| 173 | + |
| 174 | ++ 本周是之前一直比较害怕,目前学习自己感觉还是很满意的。 |
| 175 | ++ 对于分治、回溯感觉还是比较模糊 |
| 176 | ++ 深度优先于广度优先在做岛屿数量时有了更清晰的理解。 |
| 177 | ++ 二分查找,小细节很多,思想一直会,但是还是得多敲代码。 |
0 commit comments