LeetCode 495. Teemo Attacking(medium)

In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo‘s attacking ascending time series towards Ashe and the poisoning time duration per Teemo‘s attacking, you need to output the total time that Ashe is in poisoned condition.

You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.

Example 1:

Input: [1,4], 2
Output: 4
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. This poisoned status will last 2 seconds until the end of time point 2. And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds. So you finally need to output 4.

Example 2:

Input: [1,2], 2
Output: 3
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. This poisoned status will last 2 seconds until the end of time point 2. However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status. Since the poisoned status won‘t add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3. So you finally need to output 3.

Note:

  1. You may assume the length of given time series array won‘t exceed 10000.
  2. You may assume the numbers in the Teemo‘s attacking time series and his poisoning time duration per attacking are non-negative integers, which won‘t exceed 10,000,000.

很有意思的一道题目,从LOL游戏中抽象出来,题目的意思是求若干段序列累计的长度,重叠部分只计算一次,直接求解很麻烦,但是如果翻过脸求解,总最大的长度中减去相邻两段的间隔长度,最后会简单很多

代码如下:

 1 class Solution {
 2 public:
 3     int findPoisonedDuration(vector<int>& timeSeries, int duration) {
 4         //参考了答案中大神的解法
 5         //题目的意思是求若干段序列累计的长度,重叠部分只计算一次
 6         //直接去阶段很麻烦,但是如果用最大的长度减去减去相邻序列的间隔,最后结果会简单很多。
 7         if (timeSeries.empty())
 8             return 0;
 9         int res = timeSeries.back() + duration - timeSeries[0];
10         for (int i = 1; i < timeSeries.size(); i++)
11             res -= max(0, timeSeries[i] - (timeSeries[i - 1] + duration));
12         return res;
13     }
14 };
时间: 2024-11-06 03:53:49

LeetCode 495. Teemo Attacking(medium)的相关文章

LeetCode:18. 4Sum(Medium)

1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个数之和等于目标整数target.请找出所有满足此条件的四个整数. 3. 解题思路 先对nums进行排序,然后采用两层for循环来确定前两个数字,最后在第二层for循环中确定后两个数字. 注意可能存在重复解!! 如下图所示,对Input先进行排序:[-4, -1, -1,0, 1,2],target

LeetCode: 55. Jump Game(Medium)

1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的值代表每一次你能从当前位置跳跃的步数.问能否跳到该数组的最后一个元素位置 注意:可以跳的步数超出数组长度依旧视为可以达到最后位置 3. 解题思路 从第一个元素开始遍历,记录下你所能到达的最远位置,例如{2, 2, 0, 1, 2},遍历第一个元素时,你所能到达的最远位置是"i+nums[i]&quo

[LeetCode] Longest Consecutive Sequence(DP)

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

LeetCode OJ——练习笔记(1)Evaluate Reverse Polish Notation

院招终于开始了,然后期待与兴奋过后却是面临着笔试一次又一次的失败,然后开始留意到LeetCode. 也想自己去体验一下诸多大牛通向无限coding路上都攻克过的一关. 话不多说,贴出原题: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expres

[LeetCode] Palindrome Partitioning II (DP)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"

LeetCode之Easy篇 ——(13)Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 思路分析: 1.熟悉罗马数字的规则.见LeetCode之Easy篇 --(12)Integer to Roman 2.将输入的罗马数字转化成数组,并逐一通过case比对,然后根据其规则进行运算. Java代码示例: class Solution { public int romanT

Leetcode之深度优先搜索(DFS)专题-DFS+记忆化 329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].

Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)

Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合. 给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系.如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道.你必须输出所有学生

Leetcode之广度优先搜索(BFS)专题-1162. 地图分析(As Far from Land as Possible)

BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree Level Order Traversal) 你现在手里有一份大小为 N x N 的『地图』(网格) grid,上面的每个『区域』(单元格)都用 0 和 1 标记好了.其中 0 代表海洋,1 代表陆地,你知道距离陆地区域最远的海洋区域是是哪一个吗?请返回该海洋区域到离它最近的陆地区域的距离. 我们这里说的距离是『曼哈顿距离』( Manhattan Distance):(x0, y0)