緒論
雖然是上周日參加的比賽,但是這周沒有怎么學習,每天就是玩耍。也導致對周賽的總結遲遲沒有進行。想著再拖下去下次周賽都要開始了,在這里補一下。
這場比賽總體比上場簡單一些,但是最后一道題因為忘記初始化類內變量導致調試好久,血淚教訓。
題目分析
A:轉化時間需要的最少操作數
因為單獨考慮小時、分鐘太過繁瑣,我的方法是將其轉換成時間戳(類似chrono的time_since_epoch
方法)。
class Solution {
public:int convertTime(string current, string correct) {int h1 = (current[0] - '0') * 10 + current[1] - '0';int h2 = (correct[0] - '0') * 10 + correct[1] - '0';int m1 = (current[3] - '0') * 10 + current[4] - '0';int m2 = (correct[3] - '0') * 10 + correct[4] - '0';int x1 = h1 * 60 + m1;int x2 = h2 * 60 + m2;if (x1 > x2) x2 += 24 * 60;int x = x2 - x1;int ans = 0;ans += x / 60;x %= 60;ans += x / 15;x %= 15;ans += x / 5;x %= 5;ans += x / 1;x %= 1;return ans;}
};
B:找出輸掉零場或一場比賽的玩家
用map
去存儲每個節點的入度,之所以用map
是因為要求結果有序。
需要注意的一點是即使是贏家我們也應該更新這個信息,因為我們得統計入度為0的節點。
class Solution {
public:vector<vector<int>> findWinners(vector<vector<int>>& matches) {map<int, int> score;int x, y;for (auto &arr : matches) {x = arr[0]; y = arr[1];score[x] += 0;score[y] += 1;}vector<vector<int>> ans(2);auto &ans0 = ans[0];auto &ans1 = ans[1];for (auto &pr : score) {if (pr.second == 0) ans0.push_back(pr.first);else if (pr.second == 1) ans1.push_back(pr.first);}return ans;}
};
C:每個小孩最多能分到多少糖果
這個題目很關鍵的一點就是每個小孩拿到的糖果數目是相同的,如果沒有抓住這一點可能無從下手。如果確定每個小孩拿到的糖果數x,那么每堆可以分的孩子數目是確定的。我們可以將其轉換成一個判定問題:對于x,可以分給f(x)個孩子,f(x)是否大于等于k。每次判定的復雜度是O(n)的。
很容易發現,f(x)是單調遞減的。因此我們可以考慮使用二分。
需要注意糖果數目可能超過了整數范圍。
class Solution {
public:int maximumCandies(vector<int>& candies, long long k) {using ll = long long;ll r = 0;for (auto x : candies) r += x;if (r < k) return 0;auto check = [&](ll t) -> bool {ll sum = 0;for (auto x : candies) sum += x / t;return sum >= k;};ll l = 1, mid;ll ans = 1;while (l <= r) {mid = l + (r - l) / 2;if (check(mid)) {ans = mid;l = mid + 1;} else {r = mid - 1;}}return ans;}
};
D:加密解密字符串
最后一道題有一點意思,剛開始一看,這不就是一個簡單的模擬嗎?對于編碼沒什么說的,這么小的數據閉著眼睛都過了。對于解碼卻有一些講究。當時我看著200的數據有點上頭,覺得這還有什么好考慮的,直接模,結果果然超時了。。。
開始思考為什么會超時。因為values中可能出現重復的字符串,所有一個位置可能有不同的字符。假如values中每個字符串都是一樣的,而需要解碼的字符串就是這些一樣的字符串組成的,那么每個位置都有很多種組合,很容易就超過1e9了。
雖然組合有可能超過1e9,但是實際的dictionary卻很小,所以我們必須對組合進行剪枝,而且必須在前綴階段就進行剪枝,如果等到解碼結束再進行剪枝已經遲了。(我剛開始模擬就沒有剪枝)
如果我們直接用數據對比前綴,每次的時間復雜度都是O(200)左右,雖然貌似不大,但是也會超時。
為了更好的進行 剪枝,我們就必須高效地判斷一個前綴是否出現在dictionary。和前綴相關的操作容易聯想到字典樹。
字典樹雖然名字聽起來很高大上,但是實際上是一個非常容易理解的數據結構,每個節點存儲一個字符,有26個子孩子,用來記錄在這個字符后是否有其他字符。還需要一個布爾變量用來記錄當前字符是否可以作為某個單詞的結尾。
因為我們的字典樹只進行添加,不會刪除,所以可以使用內存池進行優化。如果直接使用new在堆上分配內存,那么每次添加新節點都需要new一次,最后程序結束還需要delete。這對內存的管理提出了要求。如果我們使用簡易的內存池就可以避免這個問題。具體的講,就是用vector管理內存。vector有自動擴容機制,并且不用擔心內存泄漏。
每個節點使用下標作為指針指向下一個節點。
struct Trie {struct Node {char c_;bool is_word_;vector<int> next_;Node(char c): next_(26, -1), is_word_(false) {}};int root_ = 0;vector<Node> nodes_;public:Trie() {nodes_.emplace_back(' ');}void add(const string &s) {int root = root_, t;for (auto c : s) {int t = nodes_[root].next_[c - 'a'];if (t == -1) {//節點不存在nodes_.emplace_back(c);t = nodes_[root].next_[c - 'a'] = nodes_.size() - 1;}root = t;}nodes_[root].is_word_ = true;}};
上面的字典樹沒有實現search方法匹配前綴。因為在實際搜索的過程中,我們是一個字母一個字母進行確認,所以可以將前綴匹配和搜索結合起來,這樣子我們在搜索下一個字符的時候只搜索在字典樹的字符,優化算法。
class Encrypter {struct Trie {struct Node {char c_;bool is_word_;vector<int> next_;Node(char c): next_(26, -1), is_word_(false) {}};int root_ = 0;vector<Node> nodes_;public:Trie() {nodes_.emplace_back(' ');}void add(const string &s) {int root = root_, t;for (auto c : s) {int t = nodes_[root].next_[c - 'a'];if (t == -1) {//節點不存在nodes_.emplace_back(c);t = nodes_[root].next_[c - 'a'] = nodes_.size() - 1;}root = t;}nodes_[root].is_word_ = true;}};vector<char> keys_;vector<string> values_;Trie trie_;unordered_map<char, int> keys_map_;unordered_map<string, vector<int>> values_map_;vector<vector<int>> init(const string &s) {vector<vector<int>> ans;int n = s.size();for (int i = 0; i < n; i += 2) {ans.emplace_back();auto &arr =ans.back();auto &t = values_map_[s.substr(i, 2)];for (auto x : t) {arr.push_back(keys_[x] - 'a');}}return ans;}
public:Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary): keys_(keys), values_(values){for (auto &s : dictionary) {trie_.add(s);}int n = keys.size();for (int i = 0; i < n; ++i) {keys_map_[keys[i]] = i;}n = values.size();for (int i = 0; i < n; ++i) {values_map_[values[i]].push_back(i);}}string encrypt(string word1) {string ans;for (auto c : word1) {ans.append(values_[keys_map_[c]]);}return ans;}int decrypt(string word2) {auto arr = init(word2);int n = arr.size();int ans = 0;function<void(int, int)> dfs;dfs = [&](int idx, int root) {if (idx == n) {if (trie_.nodes_[root].is_word_) ++ans;return;}auto &vec = arr[idx];int t;for (auto x : vec) {t = trie_.nodes_[root].next_[x];if (t == -1) continue;dfs(idx + 1, t);}};dfs(0, 0);return ans;}
};
但是在實現字典樹的過程中我忘記對Node的is_word_字段進行初始化,導致出錯。幸虧最終調試出來了。
經驗總結
這次周賽偏簡單,題目思維量不是很大,雖然有一些編碼量。前三道題里就是第三道的二分比較需要一些思考。第四道題需要考慮到使用字典樹進行前綴匹配。
雖然是第一次實現字典樹,出現了一個bug,但是總體還是比較滿意的。代碼緊湊,邏輯完整。