Leetcode Note: Go - Richest Customer Wealth

Richest Customer Wealth - LeetCode
https://leetcode.com/problems/richest-customer-wealth/

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

回答

Go solution using golang’s concurrency - Richest Customer Wealth - LeetCode
https://leetcode.com/problems/richest-customer-wealth/solutions/1735092/go-solution-using-golang-s-concurrency/

func maximumWealth(accounts [][]int) (max int) {
	calculateWealth := func(account []int, sumCh chan int) {
		sum := 0
		for _, val := range account {
			sum += val
		}
		sumCh <- sum
	}

	for _, account := range accounts {
		sumCh := make(chan int)
		go calculateWealth(account, sumCh)
		accountWealth := <-sumCh
		if accountWealth > max {
			max = accountWealth
		}
		close(sumCh)
	}
	return
}