LeetCode 第 212 场周赛

按键持续时间最长的键

枚举模拟

LeetCode 第 212 场周赛
 1 class Solution {
 2 public:
 3     char slowestKey(vector<int>& releaseTimes, string keysPressed) {
 4         map<char, int> m;
 5         int mx = 0;
 6         for (int i = 0; i < releaseTimes.size(); i++) {
 7             if (i == 0) {
 8                 m[keysPressed[i]] = releaseTimes[i];
 9             } else {
10                 m[keysPressed[i]] = max(m[keysPressed[i]], releaseTimes[i] - releaseTimes[i - 1]);
11             }
12             mx = max(mx, m[keysPressed[i]]);
13         }
14         char ans = 'a';
15         for (int i = 0; i < keysPressed.size(); i++) {
16             if (m[keysPressed[i]] == mx) {
17                 ans = max(ans, keysPressed[i]); 
18             }
19         }
20         return ans;
21     }
22 };
View Code

等差子数组

枚举模拟 时间复杂度O(n^2 * logn)

LeetCode 第 212 场周赛
 1 class Solution {
 2 public:
 3     vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
 4         vector<int> v;
 5         vector<bool> ans;
 6         for (int i = 0; i < l.size(); i++) {
 7             v.clear();
 8             for (int j = l[i]; j <= r[i]; j++) v.push_back(nums[j]);
 9             sort(v.begin(), v.end());
10             for (int j = 2; j < v.size(); j++) {
11                 if ((v[j] - v[j - 1]) != (v[1] - v[0])) {
12                     ans.push_back(false);
13                     break;
14                 }
15             }
16             if (ans.size() != i + 1) ans.push_back(true);
17         }        
18         return ans;
19     }
20 };
View Code

 

上一篇:AtCoder Beginner Contest 212【A - E】


下一篇:2【leetcode】字典树Trie 208、212