Leetcode Note: Go - Special Array With X Elements Greater Than or Equal X

Special Array With X Elements Greater Than or Equal X - LeetCode
https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/

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

回答

GO O(N) - Special Array With X Elements Greater Than or Equal X - LeetCode
https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/solutions/1147673/go-o-n/

func specialArray(nums []int) int {
	var mp [1001]int
	for i := 0; i < len(nums); i++ {
		mp[nums[i]]++
	}
	var cnt int
	for i := 1000; i > 0; i-- {
		cnt += mp[i]
		if cnt == i {
			return i
		}
	}
	return -1
}