当前位置:网站首页>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;
}边栏推荐
- Sensor bringup 中的一些问题总结
- Solution to the problem of the 80th fortnight competition of leetcode
- Multithreading mode (I) -- protective pause and join source code
- QT--实现TCP通信
- Une explication du 80e match bihebdomadaire de leetcode
- 2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation
- Overview of camera image quality
- Opencv_100问_第五章 (21-25)
- dlib 人脸检测
- Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
猜你喜欢

Analysis of memory management mechanism of (UE4 4.26) UE4 uobject

Houdini script vex learning

BRDF of directx11 advanced tutorial PBR (2)

QT--实现TCP通信

关于 Sensor flicker/banding现象的解释
![[reinstall system] 01 system startup USB flash disk production](/img/0d/9b3d4b8e286a75f8b58e35d02f261b.jpg)
[reinstall system] 01 system startup USB flash disk production

线程有哪些状态?

(UE4 4.27) customize globalshader

2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation

sqlite交叉编译动态库
随机推荐
Understand Houdini's (heightfield) remap operation
SQL 注入读写文件
About why GPU early-z reduces overdraw
Touch screen setting for win7 system dual screen extended display
Project progress on February 28, 2022
dlib 人脸检测
LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
Explanation of sensor flicker/banding phenomenon
Cause analysis of motion blur / smear caused by camera shooting moving objects
勤于奋寻找联盟程序方法介绍
单通道图片的读入
zip 和.items()区别
Three years of sharpening a sword: insight into the R & D efficiency of ant financial services
[word] word 2010 recording macro batch replacing paragraph marks in the selected text
Leetcode January 10 daily question 306 Additive number
Poisson disk sampling for procedural placement
Directx11 advanced tutorial PBR (1) summary of physical phenomena of light
Video based fire smoke detection using robust AdaBoost
Trunet: short videos generation from long videos via story preserving truncation (thesis translation)
Idea common configuration