赞
踩
class Solution { public: int hardestWorker(int n, vector<vector<int>>& logs) { vector<int> workers(n); int curTime = 0; for (auto log : logs) { workers[log[0]] = max(workers[log[0]], log[1] - curTime); curTime = log[1]; } int curId = 0, maxTime = 0; for (int i = 0; i < n; i++) { if (maxTime < workers[i]) { curId = i; maxTime = workers[i]; } } return curId; } };
class Solution {
public:
vector<int> findArray(vector<int>& pref) {
int n = pref.size();
vector<int> ans(n);
ans[0] = pref[0];
int pre = ans[0];
for (int i = 1; i < n; i++) {
ans[i] = pref[i] ^ pre;
pre = pre ^ ans[i];
// cout << "pre:"<< pre << " ans[" << i << "]:"<< ans[i] << endl;
}
return ans;
}
};
class Solution { public: string robotWithString(string s) { stack<char> st; vector<pair<char, int>> dict; for (int i = 0; i < s.size(); i++) { dict.push_back({s[i], i}); } sort(dict.begin(), dict.end()); int cur_pos = 0; string ans = ""; for (auto [ch, pos] : dict) { if (!st.empty() && st.top() == ch) { // cout << ch << endl; while (!st.empty() && st.top() <= ch) { ans += st.top(); st.pop(); } } if (pos >= cur_pos) { ans += ch; while (cur_pos < pos) { st.push(s[cur_pos]); // cout << ch << " " << s[cur_pos] << endl; cur_pos++; } cur_pos++; } } while (!st.empty()) { ans += st.top(); st.pop(); } return ans; } };
class Solution { private: const int NUM = 1000000007; public: int numberOfPaths(vector<vector<int>>& grid, int k) { int m = grid.size(); int n = grid[0].size(); vector<vector<unordered_map<int, int>>> dp(m, vector<unordered_map<int, int>>(n)); dp[0][0][grid[0][0] % k] = 1; int sum = grid[0][0]; for (int i = 1; i < m; i++) { sum += grid[i][0]; dp[i][0][sum % k] = 1; } sum = grid[0][0]; for (int j = 1; j < n; j++) { sum += grid[0][j]; dp[0][j][sum % k] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { for (auto &left : dp[i][j - 1]) { dp[i][j][(left.first + grid[i][j]) % k] += left.second % NUM; } for (auto &up : dp[i - 1][j]) { dp[i][j][(up.first + grid[i][j]) % k] += up.second % NUM; } } } return dp[m - 1][n - 1][0] % NUM; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。