LWC 64: 747. Largest Number Greater Than Twice of Others
来源:互联网 发布:郭德纲才学 知乎 编辑:程序博客网 时间:2024/06/06 08:56
LWC 64: 747. Largest Number Greater Than Twice of Others
传送门:747. Largest Number Greater Than Twice of Others
Problem:
In a given integer array nums, there is always exactly one largest element.
Find whether the largest element in the array is at least twice as much as every other number in the array.
If it is, return the index of the largest element, otherwise return -1.
Example 1:
Input: nums = [3, 6, 1, 0]
Output: 1
Explanation: 6 is the largest integer, and for every other number in the array x,
6 is more than twice as big as x. The index of value 6 is 1, so we return 1.
Exmaple 2:
Input: nums = [1, 2, 3, 4]
Output: -1
Explanation: 4 isn’t at least as big as twice the value of 3, so we return -1.
Note:
- nums will have a length in the range [1, 50].
- Every nums[i] will be an integer in the range [0, 99].
思路1:
非排序,如果存在一个数,比其他任何数的两倍还大必然是最大数。时间复杂度为
Java版本:
public int dominantIndex(int[] nums) { int n = nums.length; for (int i = 0; i < n; ++i) { boolean ok = true; for (int j = 0; j < n; ++j) { if (i != j && nums[i] < nums[j] * 2) { ok = false; break; } } if (ok) return i; } return -1; }
思路2:
数据量大的情况下,采用排序,最大的数和次大的数比较一波即可,注意下,排序需要带着下标。
Java版本:
class P implements Comparable<P>{ int idx; int val; P(int idx, int val){ this.idx = idx; this.val = val; } @Override public int compareTo(P that) { return that.val - this.val; } } public int dominantIndex(int[] nums) { int n = nums.length; if (n == 1) return -1; P[] ps = new P[n]; for (int i = 0; i < n; ++i) { ps[i] = new P(i, nums[i]); } Arrays.sort(ps); if (ps[0].val >= ps[1].val * 2) return ps[0].idx; return -1; }
Python版本:
def dominantIndex(self, nums): """ :type nums: List[int] :rtype: int """ idx = nums.index(max(nums)) nums = sorted(nums) return idx if nums[-1] >= nums[-2] * 2 else -1
- LWC 64: 747. Largest Number Greater Than Twice of Others
- leetcode-747. Largest Number Greater Than Twice of Others
- Leetcode——747. Largest Number Greater Than Twice of Others
- LWC 62:744. Find Smallest Letter Greater Than Target
- Row index must be greater than or equal to zero and less than the number of rows.
- LWC 53:694. Number of Distinct Islands
- LWC 58:726. Number of Atoms
- LWC 63: 750. Number Of Corner Rectangles
- Whole is Greater than Sum of Parts
- LWC 49:673. Number of Longest Increasing Subsequence
- ORA-00823: Specified value of sga_target greater than sga_max_size
- ORA-00837: Specified value of MEMORY_TARGET greater than MEMORY_MAX_TARGET
- Find next greater number with same set of digits
- CareerCup Compute the rearrangement of x that is closest to y but still greater than y
- OGG-01705 input trail file './dirdat/fc075295' is greater than the size of the file
- The number of points is less than required for feature
- The number of points is less than required for feature
- Number of actions are more than display limit 25.
- 1.Java数据类型说明
- k邻近算法
- Zynq 停止在starting kernel……
- 安装Ubuntu 出现 SQUASFS error / sd 8:0:0:0 :[sdb] assuming drive cache:write through
- 2017阿里技术年度精选(下).pdf 免费下载
- LWC 64: 747. Largest Number Greater Than Twice of Others
- 机房收费系统之组合查询
- linux--tar解压/压缩
- P1025 小飞侠的游园方案
- mabaties基础篇
- 关于linux的学习分享
- 动态规划算法
- java对象池commons-pool-1.6详解(三)
- 494. Target Sum