add new solutions

This commit is contained in:
Yiyang Kang 2026-03-19 15:29:54 +09:00
parent f297e11859
commit 4720cbefc4
8 changed files with 290 additions and 0 deletions

View 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

View 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