当前位置:网站首页>LeetCode-1629. Key with the longest key duration
LeetCode-1629. Key with the longest key duration
2022-06-12 06:22:00 【Border wanderer】
LeetCode Designed a new keyboard , Testing its availability . The tester will click a series of keys ( A total of n individual ), One at a time .
Give you a length of n String keysPressed , among keysPressed[i] Represents the... In the test sequence i A pressed key .releaseTimes It's a list in ascending order , among releaseTimes[i] It means that the... Is released i Key time . String and array Subscripts are all from 0 Start . The first 0 Keys at time 0 When pressed , Next, each key just Pressed when the previous key is released .
The tester wants to find the key The longest duration Key . The first i The duration of the second key is releaseTimes[i] - releaseTimes[i - 1] , The first 0 The duration of the second key is releaseTimes[0] .
Be careful , Testing period , The same key can be pressed many times at different times , And the duration of each time may be different .
Please return to the key The longest duration Key , If there are multiple such keys , Then return to In alphabetical order, the largest The key of .
Example 1:
Input :releaseTimes = [9,29,49,50], keysPressed = "cbcd"
Output :"c"
explain : The key sequence and duration are as follows :
Press down 'c' , The duration of the 9( Time 0 Press down , Time 9 Release )
Press down 'b' , The duration of the 29 - 9 = 20( Time to release the last key 9 Press down , Time 29 Release )
Press down 'c' , The duration of the 49 - 29 = 20( Time to release the last key 29 Press down , Time 49 Release )
Press down 'd' , The duration of the 50 - 49 = 1( Time to release the last key 49 Press down , Time 50 Release )
The key with the longest key duration is 'b' and 'c'( When pressed the second time ), The duration is 20
'c' Arrange in alphabetical order 'b' Big , So the answer is 'c'
Example 2:
Input :releaseTimes = [12,23,36,46,62], keysPressed = "spuda"
Output :"a"
explain : The key sequence and duration are as follows :
Press down 's' , The duration of the 12
Press down 'p' , The duration of the 23 - 12 = 11
Press down 'u' , The duration of the 36 - 23 = 13
Press down 'd' , The duration of the 46 - 36 = 10
Press down 'a' , The duration of the 62 - 46 = 16
The key with the longest key duration is 'a' , The duration of the 16
Tips :
releaseTimes.length == n
keysPressed.length == n
2 <= n <= 1000
1 <= releaseTimes[i] <= 109
releaseTimes[i] < releaseTimes[i+1]
keysPressed It's only made up of lowercase letters
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
class Solution {
public:
char slowestKey(vector<int>& releaseTimes, string keysPressed) {
uint32_t maxVal = 0;
vector<char> result;
unordered_map<char, int> hash;
unordered_map<char, int>::iterator it;
hash[keysPressed[0]] = releaseTimes[0];
maxVal = releaseTimes[0];
for (int i = 1; i < keysPressed.size(); i++) {
int val = releaseTimes[i] - releaseTimes[i - 1];
if (hash.count(keysPressed[i]) == 0 || val > hash[keysPressed[i]]) {
hash[keysPressed[i]] = val;
}
if (val > maxVal) {
maxVal = val;
}
}
it = hash.begin();
while (it != hash.end()) {
if (it->second == maxVal) {
result.push_back(it->first);
}
it++;
}
sort(result.begin(), result.end(), cmp);
return result[0];
}
private:
static bool cmp(char a, char b) {
return (a >= b ? true : false);
}
};
int main() {
vector < int> v = { 23, 34, 43, 59, 62, 80, 83, 92, 97 };
string str = "qgkzzihfc";
unique_ptr<Solution> ptr(new Solution());
cout << ptr->slowestKey(v, str) << endl;
system("pause");
return 0;
}边栏推荐
- Leetcode-93. Restore IP address
- (UE4 4.27) customize primitivecomponent
- Multithreading (2) -- pipeline (2) -- synchronized underlying monitor, lightweight, biased lock, lock inflation
- Chartextcnn (Ag dataset - news topic classification)
- Findasync and include LINQ statements - findasync and include LINQ statements
- JS variable scope
- First note
- Leetcode-1512. Number of good pairs
- How do I get the date and time from the Internet- How to get DateTime from the internet?
- RMB classification II
猜你喜欢
随机推荐
Findasync and include LINQ statements - findasync and include LINQ statements
[reinstall system] 01 system startup USB flash disk production
Touch screen setting for win7 system dual screen extended display
Book classification based on Naive Bayes
Open the camera in unity3d and display the contents of the camera in the scene as texture2d
SQL 注入-盲注
PHP 开发环境搭建及数据库增删改查
Nocturnal simulator ADB view log
Bulk Rename Utility
Automatic modeling of Interchange
Redis queue
zip 和.items()区别
Textcnn (MR dataset - emotion classification)
SQL注入原理即sqli-labs搭建,sql注入简单实战
JS变量作用域
Redis configuration (III) -- master-slave replication
The unity3d script searches for colliders with overlaps within the specified radius
【思维方法】之第一性原理
About why GPU early-z reduces overdraw
Univariate linear regression model









