Leetcode Note: Go - Minimum Subsequence in Non Increasing Order

Minimum Subsequence in Non-Increasing Order - LeetCode
https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/

  • Go 言語で取り組んだメモ

回答

Golang solution with explanation - Minimum Subsequence in Non-Increasing Order - LeetCode
https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/solutions/1249291/golang-solution-with-explanation/

func minSubsequence(nums []int) []int {
	res := []int{}
	sum := 0
	resSum := 0
	sort.Slice(nums, func(i, j int) bool { return nums[i] > nums[j] })
	for _, num := range nums { sum += num }
	
	for _, num := range nums {
		sum -= num
		resSum += num
		res = append(res, num)
		if resSum > sum { break }
	}
	return res
}