Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0d31b5a

Browse filesBrowse files
[LEET-3386] add 3386
1 parent e3f477d commit 0d31b5a
Copy full SHA for 0d31b5a

File tree

3 files changed

+86
-0
lines changed
Filter options
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+86
-0
lines changed

‎paginated_contents/algorithms/4th_thousand/README.md

Copy file name to clipboardExpand all lines: paginated_contents/algorithms/4th_thousand/README.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
33
| 3396 | [Minimum Number of Operations to Make Elements in Array Distinct](https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3396.java) | | Easy |
44
| 3392 | [Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3392.java) | | Easy |
5+
| 3386 | [Button with Longest Push Time](https://leetcode.com/problems/button-with-longest-push-time/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3386.java) | | Easy |
56
| 3379 | [Transformed Array](https://leetcode.com/problems/transformed-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3379.java) | | Easy |
67
| 3375 | [Minimum Operations to Make Array Values Equal to K](https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3375.java) | | Easy |
78
| 3370 | [Smallest Number With All Set Bits](https://leetcode.com/problems/smallest-number-with-all-set-bits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3370.java) | | Easy |
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _3386 {
7+
public static class Solution1 {
8+
public int buttonWithLongestTime(int[][] events) {
9+
Map<Integer, Integer> map = new HashMap<>();
10+
int ans = events[0][0];
11+
map.put(events[0][0], events[0][1]);
12+
for (int i = 1; i < events.length; i++) {
13+
int duration = events[i][1] - events[i - 1][1];
14+
if (map.getOrDefault(events[i][0], 0) < duration) {
15+
map.put(events[i][0], duration);
16+
}
17+
}
18+
int maxDuration = events[0][1];
19+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
20+
if (entry.getValue() > maxDuration) {
21+
ans = entry.getKey();
22+
maxDuration = entry.getValue();
23+
} else if (entry.getValue() == maxDuration) {
24+
if (entry.getKey() < ans) {
25+
ans = entry.getKey();
26+
}
27+
}
28+
}
29+
return ans;
30+
}
31+
}
32+
}
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.fishercoder.common.utils.CommonUtils;
6+
import com.fishercoder.solutions.fourththousand._3386;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class _3386Test {
11+
private _3386.Solution1 solution1;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _3386.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(
21+
1,
22+
solution1.buttonWithLongestTime(
23+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
24+
"[1,2],[2,5],[3,9],[1,15]")));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
assertEquals(
30+
2,
31+
solution1.buttonWithLongestTime(
32+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
33+
"[9,4],[19,5],[2,8],[3,11],[2,15]")));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
assertEquals(
39+
2,
40+
solution1.buttonWithLongestTime(
41+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
42+
"[7,1],[19,3],[9,4],[12,5],[2,8],[15,10],[18,12],[7,14],[19,16]")));
43+
}
44+
45+
@Test
46+
public void test4() {
47+
assertEquals(
48+
16,
49+
solution1.buttonWithLongestTime(
50+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
51+
"[5,5],[16,17],[16,19]")));
52+
}
53+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.