티스토리 뷰
Go를 이용한 연속된 부분합 구하기 방법입니다.
package main
import (
"fmt"
"math"
)
func main() {
nums := []int{100, 2, 3, -110, 1, 4, 5, 6, 1, 7, 8, 9}
result := maxSubSum(nums)
fmt.Println(result) // 105
}
func maxSubSum(nums []int) int {
if len(nums) == 0 {
return -1
}
maxSum := float64(0)
result := float64(math.MinInt64)
if len(nums) == 1 {
return int(result)
}
for index := -1; index < len(nums) - 1; index++ {
next := float64(nums[index + 1])
maxSum = math.Max(maxSum + next, next)
result = math.Max(result, maxSum)
}
return int(result)
}
'Coding' 카테고리의 다른 글
Python3. Interest Compute (0) | 2020.08.29 |
---|---|
Go. BinarySearch (0) | 2020.08.08 |
Go. 한글로 입력된 숫자를 더해서 한글로 출력하기 (0) | 2020.07.04 |
Go. Int64를 벗어나는 스트링 숫자 더하기 (0) | 2020.07.04 |
이진트리의 순회. (0) | 2020.07.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- git
- php
- docker
- cocoapods
- tomcat
- rxswift
- Java
- Spring
- android
- Gradle
- Xcode
- enum
- Python
- github
- centos8
- Codable
- go
- CentOS
- SWIFT
- war
- intellij
- windows10
- golang
- Kotlin
- ubuntu
- Windows
- ios
- nodejs
- Linux
- MySQL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함