3386. Button with Longest Push Time LeetCode Solution
In this guide, you will get 3386. Button with Longest Push Time LeetCode Solution with the best time and space complexity. The solution to Button with Longest Push Time problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Button with Longest Push Time solution in C++
- Button with Longest Push Time solution in Java
- Button with Longest Push Time solution in Python
- Additional Resources
Problem Statement of Button with Longest Push Time
You are given a 2D array events which represents a sequence of events where a child pushes a series of buttons on a keyboard.
Each events[i] = [indexi, timei] indicates that the button at index indexi was pressed at time timei.
The array is sorted in increasing order of time.
The time taken to press a button is the difference in time between consecutive button presses. The time for the first button is simply the time at which it was pressed.
Return the index of the button that took the longest time to push. If multiple buttons have the same longest time, return the button with the smallest index.
Example 1:
Input: events = [[1,2],[2,5],[3,9],[1,15]]
Output: 1
Explanation:
Button with index 1 is pressed at time 2.
Button with index 2 is pressed at time 5, so it took 5 – 2 = 3 units of time.
Button with index 3 is pressed at time 9, so it took 9 – 5 = 4 units of time.
Button with index 1 is pressed again at time 15, so it took 15 – 9 = 6 units of time.
Example 2:
Input: events = [[10,5],[1,7]]
Output: 10
Explanation:
Button with index 10 is pressed at time 5.
Button with index 1 is pressed at time 7, so it took 7 – 5 = 2 units of time.
Constraints:
1 <= events.length <= 1000
events[i] == [indexi, timei]
1 <= indexi, timei <= 105
The input is generated such that events is sorted in increasing order of timei.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
3386. Button with Longest Push Time LeetCode Solution in C++
class Solution {
public:
int buttonWithLongestTime(vector<vector<int>>& events) {
int ans = 0;
int maxTimeTaken = 0;
int prevTime = 0;
for (const vector<int>& event : events) {
const int index = event[0];
const int time = event[1];
const int timeTaken = time - prevTime;
if (timeTaken > maxTimeTaken ||
timeTaken == maxTimeTaken && index < ans) {
maxTimeTaken = timeTaken;
ans = index;
}
prevTime = time;
}
return ans;
}
};
/* code provided by PROGIEZ */
3386. Button with Longest Push Time LeetCode Solution in Java
class Solution {
public int buttonWithLongestTime(int[][] events) {
int ans = 0;
int maxTimeTaken = 0;
int prevTime = 0;
for (int[] event : events) {
final int index = event[0];
final int time = event[1];
final int timeTaken = time - prevTime;
if (timeTaken > maxTimeTaken || timeTaken == maxTimeTaken && index < ans) {
maxTimeTaken = timeTaken;
ans = index;
}
prevTime = time;
}
return ans;
}
}
// code provided by PROGIEZ
3386. Button with Longest Push Time LeetCode Solution in Python
class Solution:
def buttonWithLongestTime(self, events: list[list[int]]) -> int:
ans = 0
maxTimeTaken = 0
prevTime = 0
for index, time in events:
timeTaken = time - prevTime
if timeTaken > maxTimeTaken or timeTaken == maxTimeTaken and index < ans:
maxTimeTaken = timeTaken
ans = index
prevTime = time
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.