当前位置:网站首页>Li Kou daily question - day 25 -495 Timo attack
Li Kou daily question - day 25 -495 Timo attack
2022-06-23 17:21:00 【Chongyou research Sen】
2022.6.23 Did you brush the questions today ?
subject :
stay 《 Hero alliance 》 In the world of , One called “ Timo ” The hero of . His attack can make enemy hero ash ( Editor's note : Cold shooter ) Into a toxic state .
When Timo attacked ash , Ash's poisoning just continued duration second .
Formally speaking , Timo is here t Launching an attack means that ash is in a time interval [t, t + duration - 1]( contain t and t + duration - 1) In a state of poisoning . If Timo is at the end of the poisoning effect front Attack again , The poisoning status timer will Reset , After a new attack , The toxic effect will be in duration Seconds later .
To give you one The decreasing Array of integers for timeSeries , among timeSeries[i] It means Timo is timeSeries[i] Attack ash in seconds , And an integer representing the duration of poisoning duration .
Return to ash's poisoned total Number of seconds .
analysis :
This question means , Given an increasing array and a fixed value , Start with the first element of the array , Let it come from x Add in sequence as x+1,x+2,,x+ Set value , Then iterate over the second element of the array y, Also repeat y+1,y+2,,y+ Set value , And then add these numbers , How many in all , The number of repetitions is counted only once . for example
【1,3】,n=2 1+1,1+2,3+1,3+2, So that is 4
【1,3】,n=3 1+1,1+2,1+3,3+1,3+2,3+3, So that is 6
So we can save these numbers every time map in , Then count map Just a few of them ( But the operation timed out !!!)
analysis :
1. Hashtable
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
unordered_map<int, int>map;
int res = 0;
for (auto num : timeSeries)
{
int val = num;
for (; num < val + duration; num++)
{
map[num]++;
}
}
return map.size();
}
};2. Common solution
Thought is : By comparing the difference between the two numbers before and after a given array and comparing the fixed value , Find the first number “ Valid values ”, And the last number “ Valid values ” Then it is the constant value , Finally, put these “ The valid values add up to the answer
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int ans = duration;
int res = 0;
for (int i = 1; i < timeSeries.size(); i++)
{
res = min(timeSeries[i] - timeSeries[i - 1], duration);
ans += res;
}
return ans;
}
};边栏推荐
猜你喜欢

Interface ownership dispute

How to use SQL window functions
![[go]沙盒环境下调用支付宝扫码支付](/img/d4/c6d72a697bc08f69f11121a15109b3.png)
[go]沙盒环境下调用支付宝扫码支付

Redis cluster operation method

Digital twin excavator of Tupu software realizes remote control

公司招了个五年经验的测试员,见识到了真正的测试天花板

How to configure MySQL log management

Tensorrt Paser loading onnx inference use

ASEMI快恢复二极管RS1M、US1M和US1G能相互代换吗

使用Jmeter进行性能测试及性能监控平台搭建
随机推荐
Interface ownership dispute
短视频平台开发,点击输入框时自动弹出软键盘
Asemi ultrafast recovery diode es1j parameters, es1j package, es1j specification
[network communication -- webrtc] analysis of webrtc source code -- supplement of pacingcontroller related knowledge points
MySQL事务及其特性与锁机制
你女朋友也能读懂的LAMP架构
Innovative technology leader! Huawei cloud gaussdb won the 2022 authoritative award in the field of cloud native database
Robot Orientation and some misunderstandings in major selection in college entrance examination
hands-on-data-analysis 第二单元 第四节数据可视化
How to use SQL window functions
Why do we say that the data service API is the standard configuration of the data midrange?
Date转换为LocalDateTime
Importance of ERP management system
[go] calling Alipay to scan code for payment in a sandbox environment
How to make sales management more efficient?
What is an abstract class? How to define abstract classes?
Spdlog logging example - create a logger using sink
Three minutes to learn how to retrieve the MySQL password
Importance and purpose of test
混沌工程在云原生中间件稳定性治理中的实践分享