|
4 | 4 | import java.util.HashMap;
|
5 | 5 | import java.util.Map;
|
6 | 6 |
|
7 |
| -/** |
8 |
| - * 747. Largest Number Greater Than Twice of Others |
9 |
| - * |
10 |
| - * In a given integer array nums, there is always exactly one largest element. |
11 |
| - * Find whether the largest element in the array is at least twice as much as every other number in the array. |
12 |
| - * If it is, return the index of the largest element, otherwise return -1. |
13 |
| -
|
14 |
| - Example 1: |
15 |
| - Input: nums = [3, 6, 1, 0] |
16 |
| - Output: 1 |
17 |
| - Explanation: 6 is the largest integer, and for every other number in the array x, |
18 |
| - 6 is more than twice as big as x. The index of value 6 is 1, so we return 1. |
19 |
| -
|
20 |
| - Example 2: |
21 |
| - Input: nums = [1, 2, 3, 4] |
22 |
| - Output: -1 |
23 |
| - Explanation: 4 isn't at least as big as twice the value of 3, so we return -1. |
24 |
| -
|
25 |
| - Note: |
26 |
| - nums will have a length in the range [1, 50]. |
27 |
| - Every nums[i] will be an integer in the range [0, 99]. |
28 |
| - */ |
29 | 7 | public class _747 {
|
30 | 8 |
|
31 |
| - public static class Solution1 { |
32 |
| - public int dominantIndex(int[] nums) { |
33 |
| - Map<Integer, Integer> map = new HashMap<>(); |
34 |
| - int max; |
35 |
| - int secondMax; |
36 |
| - for (int i = 0; i < nums.length; i++) { |
37 |
| - map.put(nums[i], i); |
38 |
| - } |
39 |
| - Arrays.sort(nums); |
40 |
| - max = nums[nums.length - 1]; |
41 |
| - secondMax = nums[nums.length - 2]; |
42 |
| - if (max >= 2 * secondMax) { |
43 |
| - return map.get(max); |
44 |
| - } else { |
45 |
| - return -1; |
46 |
| - } |
| 9 | + public static class Solution1 { |
| 10 | + public int dominantIndex(int[] nums) { |
| 11 | + Map<Integer, Integer> map = new HashMap<>(); |
| 12 | + int max; |
| 13 | + int secondMax; |
| 14 | + for (int i = 0; i < nums.length; i++) { |
| 15 | + map.put(nums[i], i); |
| 16 | + } |
| 17 | + Arrays.sort(nums); |
| 18 | + max = nums[nums.length - 1]; |
| 19 | + secondMax = nums[nums.length - 2]; |
| 20 | + if (max >= 2 * secondMax) { |
| 21 | + return map.get(max); |
| 22 | + } else { |
| 23 | + return -1; |
| 24 | + } |
| 25 | + } |
47 | 26 | }
|
48 |
| - } |
49 | 27 |
|
50 |
| - public static class Solution2 { |
51 |
| - public int dominantIndex(int[] nums) { |
52 |
| - int max = Integer.MIN_VALUE; |
53 |
| - int maxIndex = -1; |
54 |
| - for (int i = 0; i < nums.length; i++) { |
55 |
| - if (nums[i] > max) { |
56 |
| - max = nums[i]; |
57 |
| - maxIndex = i; |
58 |
| - } |
59 |
| - } |
60 |
| - for (int i = 0; i < nums.length; i++) { |
61 |
| - if (nums[i] * 2 > max && i != maxIndex) { |
62 |
| - return -1; |
| 28 | + public static class Solution2 { |
| 29 | + public int dominantIndex(int[] nums) { |
| 30 | + int max = Integer.MIN_VALUE; |
| 31 | + int maxIndex = -1; |
| 32 | + for (int i = 0; i < nums.length; i++) { |
| 33 | + if (nums[i] > max) { |
| 34 | + max = nums[i]; |
| 35 | + maxIndex = i; |
| 36 | + } |
| 37 | + } |
| 38 | + for (int i = 0; i < nums.length; i++) { |
| 39 | + if (nums[i] * 2 > max && i != maxIndex) { |
| 40 | + return -1; |
| 41 | + } |
| 42 | + } |
| 43 | + return maxIndex; |
63 | 44 | }
|
64 |
| - } |
65 |
| - return maxIndex; |
66 | 45 | }
|
67 |
| - } |
68 | 46 | }
|
0 commit comments