add new solutions
This commit is contained in:
parent
f297e11859
commit
4720cbefc4
8 changed files with 290 additions and 0 deletions
22
solutions/8/q829/solution.go
Normal file
22
solutions/8/q829/solution.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// Package q829 implements a solution for https://leetcode.com/problems/consecutive-numbers-sum/
|
||||
package q829
|
||||
|
||||
import "math"
|
||||
|
||||
func consecutiveNumbersSum(n int) int {
|
||||
maxN := int(math.Sqrt(float64(n) * 2))
|
||||
for maxN*(maxN+1)/2 <= n {
|
||||
maxN++
|
||||
}
|
||||
|
||||
cnt := 1
|
||||
for nNums := maxN - 1; nNums > 1; nNums-- {
|
||||
start := (n*2/nNums + 1 - nNums) / 2
|
||||
if (start+start+nNums-1)*nNums/2 == n {
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
var _ = consecutiveNumbersSum
|
||||
17
solutions/8/q852/solution.go
Normal file
17
solutions/8/q852/solution.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Package q852 implements a solution for https://leetcode.com/problems/peak-index-in-a-mountain-array/
|
||||
package q852
|
||||
|
||||
func peakIndexInMountainArray(arr []int) int {
|
||||
l, r := 0, len(arr)-1
|
||||
for l < r {
|
||||
m := (l + r) / 2
|
||||
if arr[m] < arr[m+1] { // incrementing
|
||||
l = m + 1
|
||||
} else {
|
||||
r = m
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
var _ = peakIndexInMountainArray
|
||||
Loading…
Add table
Add a link
Reference in a new issue