Skip to content

Commit 4054d41

Browse files
refactor 1394
1 parent 1039b29 commit 4054d41

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1394|[Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1394.java) | |Easy|Array|
1112
|1392|[Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1392.java) | |Hard|String, Rolling Hash|
1213
|1390|[Four Divisors](https://leetcode.com/problems/four-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1390.java) | |Medium|Math|
1314
|1389|[Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1389.java) | |Easy|Array|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 1394. Find Lucky Integer in an Array
8+
*
9+
* Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value.
10+
* Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.
11+
*
12+
* Example 1:
13+
* Input: arr = [2,2,3,4]
14+
* Output: 2
15+
* Explanation: The only lucky number in the array is 2 because frequency[2] == 2.
16+
*
17+
* Example 2:
18+
* Input: arr = [1,2,2,3,3,3]
19+
* Output: 3
20+
* Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.
21+
*
22+
* Example 3:
23+
* Input: arr = [2,2,2,3,3]
24+
* Output: -1
25+
* Explanation: There are no lucky numbers in the array.
26+
*
27+
* Example 4:
28+
* Input: arr = [5]
29+
* Output: -1
30+
*
31+
* Example 5:
32+
* Input: arr = [7,7,7,7,7,7,7]
33+
* Output: 7
34+
*
35+
* Constraints:
36+
* 1 <= arr.length <= 500
37+
* 1 <= arr[i] <= 500
38+
* */
39+
public class _1394 {
40+
public static class Solution1 {
41+
public int findLucky(int[] arr) {
42+
int lucky = -1;
43+
Map<Integer, Integer> map = new HashMap<>();
44+
for (int i = 0; i < arr.length; i++) {
45+
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
46+
}
47+
for (int num : map.keySet()) {
48+
if (num == map.get(num) && num > lucky) {
49+
lucky = num;
50+
}
51+
}
52+
return lucky;
53+
}
54+
}
55+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1394;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1394Test {
10+
11+
private static _1394.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1394.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(2, solution1.findLucky(new int[]{2, 2, 3, 4}));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(3, solution1.findLucky(new int[]{1, 2, 2, 3, 3, 3}));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals(-1, solution1.findLucky(new int[]{2, 2, 2, 3, 3}));
31+
}
32+
33+
@Test
34+
public void test4() {
35+
assertEquals(-1, solution1.findLucky(new int[]{5}));
36+
}
37+
38+
39+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy