Skip to content

Commit 3116311

Browse files
committed
feat: add the solution of Largest Number At Least Twice of Others(747) with Java.
1 parent 17d7bcf commit 3116311

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
| [733][733-question] | [Flood Fill][733-tips] | [][733-java] | [][733-js] | |
8787
| [744][744-question] | [Find Smallest Letter Greater Than Target][744-tips] | [][744-java] | [][744-js] | |
8888
| [746][746-question] | [Min Cost Climbing Stairs][746-tips] | [][746-java] | [][746-js] | |
89-
| [747][747-question] | [Largest Number At Least Twice of Others][747-tips] | | [][747-js] | |
89+
| [747][747-question] | [Largest Number At Least Twice of Others][747-tips] | [][747-java] | [][747-js] | |
9090
| [762][762-question] | [Prime Number of Set Bits in Binary Representation][762-tips]| | [][762-js] | |
9191
| [766][766-question] | [Toeplitz Matrix][766-tips] | | [][766-js] | |
9292
| [771][771-question] | [Jewels and Stones][771-tips] | [][771-java] | [][771-js] | [][771-kotlin] |
@@ -484,6 +484,7 @@
484484
[733-java]: ./src/_733/Solution.java
485485
[744-java]: ./src/_744/Solution.java
486486
[746-java]: ./src/_746/Solution.java
487+
[747-java]: ./src/_747/Solution.java
487488
[771-java]: ./src/_771/Solution.java
488489
[804-java]: ./src/_804/Solution.java
489490

src/_747/Solution.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package _747;
2+
3+
class Solution {
4+
public int dominantIndex(int[] nums) {
5+
int max = 0;
6+
int index = 0;
7+
for (int i = 0; i < nums.length; i++) {
8+
if (nums[i] > max) {
9+
max = nums[i];
10+
index = i;
11+
}
12+
}
13+
for (int num : nums) {
14+
if (num < max && max < num + num) {
15+
return -1;
16+
}
17+
}
18+
return index;
19+
}
20+
21+
public static void main(String[] args) {
22+
Solution solution = new Solution();
23+
System.out.println(solution.dominantIndex(new int[] {3, 6, 1, 0}));
24+
System.out.println(solution.dominantIndex(new int[] {1, 2, 3, 4}));
25+
}
26+
}

tips/747/README.md

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
1-
[xxxx][title]
1+
[Largest Number At Least Twice of Others][title]
22

33
## Description
4-
// 抄题目
54

5+
In a given integer array `nums`, there is always exactly one largest element.
66

7-
**Example:**
7+
Find whether the largest element in the array is at least twice as much as every other number in the array.
8+
9+
If it is, return the **index** of the largest element, otherwise return -1.
10+
11+
**Example 1:**
812

913
```
10-
// 抄Example
14+
Input: nums = [3, 6, 1, 0]
15+
Output: 1
16+
Explanation: 6 is the largest integer, and for every other number in the array x,
17+
6 is more than twice as big as x. The index of value 6 is 1, so we return 1.
1118
```
1219

13-
**Note:**
14-
// Note
20+
**Example 2:**
1521

16-
**Tags:** // tags
22+
```
23+
Input: nums = [1, 2, 3, 4]
24+
Output: -1
25+
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.
26+
```
1727

28+
**Note:**
1829

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
30+
1. `nums` will have a length in the range `[1, 50]`.
31+
2. Every `nums[i]` will be an integer in the range `[0, 99]`.
2332

24-
```
25-
```javascript
33+
**Tags:** [Array](https://leetcode.com/tag/array/)
2634

27-
```
35+
## 思路
2836

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
37+
给出一个数组,此数组必定有一个最大值。要求判断此最大值是否是其他数的至少两倍大。
3238

33-
```
39+
遍历找出最大值,再遍历进行比较即可。
3440

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
41+
**Java:**
3842

43+
```
44+
public int dominantIndex(int[] nums) {
45+
int max = 0;
46+
int second = 0;
47+
int index = 0;
48+
for (int i = 0; i < nums.length; i++) {
49+
if (nums[i] > max) {
50+
second = max;
51+
max = nums[i];
52+
index = i;
53+
} else if (nums[i] > second) {
54+
second = nums[i];
55+
}
56+
}
57+
return max >= second + second ? index : -1;
58+
}
3959
```
4060

4161
## 结语
42-
62+
4363
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4464

45-
[title]: https://leetcode.com/problems/xxxx
65+
[title]: https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/
4666
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)