Skip to content

Commit dbe025e

Browse files
committed
Add solution #747
1 parent 4172ba5 commit dbe025e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
733|[Flood Fill](./0733-flood-fill.js)|Easy|
162162
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
163163
746|[Min Cost Climbing Stairs](./0746-min-cost-climbing-stairs.js)|Easy|
164+
747|[Largest Number At Least Twice of Others](./0747-largest-number-at-least-twice-of-others.js)|Easy|
164165
762|[Prime Number of Set Bits in Binary Representation](./0762-prime-number-of-set-bits-in-binary-representation.js)|Easy|
165166
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
166167
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 747. Largest Number At Least Twice of Others
3+
* https://leetcode.com/problems/largest-number-at-least-twice-of-others/
4+
* Difficulty: Easy
5+
*
6+
* You are given an integer array nums where the largest integer is unique.
7+
*
8+
* Determine whether the largest element in the array is at least twice as much
9+
* as every other number in the array. If it is, return the index of the largest
10+
* element, or return -1 otherwise.
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @return {number}
16+
*/
17+
var dominantIndex = function(nums) {
18+
const max = Math.max(...nums);
19+
return nums.find(n => n !== max && n > max / 2) ? -1 : nums.indexOf(max);
20+
};

0 commit comments

Comments
 (0)