Skip to content

Commit 737f0da

Browse files
authored
Merge pull request #19 from vivekv96/1464-max-products
Leetcode Problem #1464: Maximum Product
2 parents 17d0a9d + 4958b5b commit 737f0da

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

easy/1464-max-products/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Problem: https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/
2+
package main
3+
4+
import (
5+
"sort"
6+
)
7+
8+
func maxProduct(nums []int) (product int) {
9+
if len(nums) == 2 {
10+
return (nums[0] - 1) * (nums[1] - 1)
11+
}
12+
13+
sort.Ints(nums)
14+
15+
product = (nums[len(nums)-1] - 1) * (nums[len(nums)-2] - 1)
16+
17+
return
18+
}

easy/1464-max-products/main_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_maxProduct(t *testing.T) {
6+
type args struct {
7+
nums []int
8+
}
9+
10+
tests := []struct {
11+
name string
12+
args args
13+
want int
14+
}{
15+
{
16+
name: "first case",
17+
args: args{nums: []int{3, 4, 5, 2}},
18+
want: 12,
19+
},
20+
{
21+
name: "second case",
22+
args: args{nums: []int{1, 5, 4, 5}},
23+
want: 16,
24+
},
25+
{
26+
name: "third case",
27+
args: args{nums: []int{3, 7}},
28+
want: 12,
29+
},
30+
}
31+
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
if got := maxProduct(tt.args.nums); got != tt.want {
35+
t.Errorf("maxProduct() = %v, want %v", got, tt.want)
36+
}
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)