Leetcode Note: Go - Find Subsequence of Length K With the Largest Sum

Find Subsequence of Length K With the Largest Sum - LeetCode
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/

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

回答

Go(lang) Solution - Find Subsequence of Length K With the Largest Sum - LeetCode
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/solutions/1960424/go-lang-solution/

func maxSubsequence(nums []int, k int) []int {
    dupe := make([]int, len(nums))
    copy(dupe, nums)
    sort.Ints(dupe)
    
    seen := make(map[int]int, k)
    
    for _, val := range dupe[len(dupe) - k : ] {
        seen[val]++
    }
    
    ans := make([]int, 0, k)
    
    for _, val := range nums {
        if v, ok := seen[val]; ok && v > 0 {
            ans = append(ans, val)
            seen[val]--
        }
    }    

    return ans
}