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
|
||||
Loading…
Add table
Add a link
Reference in a new issue