8000 :sparkles: Added solution to 747 · reverse/awesome-golang-leetcode@5060bbf · GitHub
[go: up one dir, main page]

Skip to content

Commit 5060bbf

Browse files
author
Sathish Babu
committed
✨ Added solution to 747
1 parent ed50c8b commit 5060bbf

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/0747.Largest-Number-At-Least-Twice-of-Others/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,27 @@
55
66
## Description
77

8+
In a given integer array nums, there is always exactly one largest element.
9+
10+
Find whether the largest element in the array is at least twice as much as every other number in the array.
11+
12+
If it is, return the index of the largest element, otherwise return -1.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: nums = [3, 6, 1, 0]
18+
Output: 1
19+
Explanation: 6 is the largest integer, and for every other number in the array x,
20+
6 is more than twice as big as x. The index of value 6 is 1, so we return 1.
21+
```
22+
823
**Example 1:**
924

1025
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
26+
Input: nums = [1, 2, 3, 4]
27+
Output: -1
28+
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.
1329
```
1430

1531
## 题意
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
n := len(nums)
5+
if n == 1 {
6+
return 0
7+
}
8+
max, maxIndex := -1, -1
9+
for i, num := range nums {
10+
if num > max {
11+
max, maxIndex = num, i
12+
}
13+
}
14+
for _, num := range nums {
15+
if num != max && 2*num > max {
16+
return -1
17+
}
18+
}
19+
return maxIndex
520
}

src/0747.Largest-Number-At-Least-Twice-of-Others/Solution_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", []int{3, 6, 1, 0}, 1},
17+
{"TestCase", []int{1, 2, 3, 4}, -1},
18+
{"TestCase", []int{6}, 0},
1919
}
2020

2121
// 开始测试

0 commit comments

Comments
 (0)
0