当前位置:网站首页>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;
}边栏推荐
- 夜神模擬器adb查看log
- cv2.fillPoly coco annotator segment坐标转化为mask图像
- Touch screen setting for win7 system dual screen extended display
- Redis data structure (VIII) -- Geo
- Redis queue
- Tips for using the potplayer video player
- Houdini & UE4 programmed generation of mountains and multi vegetation scattering points
- 勤于奋寻找联盟程序方法介绍
- Front desk display LED number (number type on calculator)
- Cause analysis of motion blur / smear caused by camera shooting moving objects
猜你喜欢

Information content security experiment of Harbin Institute of Technology

姿态估计之2D人体姿态估计 - PifPaf:Composite Fields for Human Pose Estimation

IBL of directx11 advanced tutorial PBR (3)

MNIST handwritten data recognition by CNN

Introduction to the method of diligently searching for the alliance procedure

Qt-- realize TCP communication

基于报错的 SQL 注入

BRDF of directx11 advanced tutorial PBR (2)

摄像头拍摄运动物体,产生运动模糊/拖影的原因分析

Sqlite Cross - compile Dynamic Library
随机推荐
Unity surface shader with template buffer
About why GPU early-z reduces overdraw
Univariate linear regression model
Bulk Rename Utility
哈工大信息内容安全实验
n次贝塞尔曲线
勤于奋寻找联盟程序方法介绍
Sensor bringup 中的一些问题总结
Idea common configuration
Leetcode sword finger offer (Second Edition) complete version of personal questions
Touch screen setting for win7 system dual screen extended display
Multithreading Foundation (XI) -- prevent CPU from occupying 100%
Summary of some problems in sensor bring up
Why is the union index the leftmost matching principle?
Cross compile libev
Solution to the problem of the 80th fortnight competition of leetcode
Word vector training based on nnlm
Information content security experiment of Harbin Institute of Technology
Leetcode-553. Optimal division
JS pre parsing