Leetcode Note: Go - Rank Transform of an Array

Rank Transform of an Array - LeetCode
https://leetcode.com/problems/rank-transform-of-an-array/

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

回答

Golang solution with quick walk through - Rank Transform of an Array - LeetCode
https://leetcode.com/problems/rank-transform-of-an-array/solutions/1212836/golang-solution-with-quick-walk-through/

func arrayRankTransform(arr []int) []int {
	g := make([]int, len(arr))
	m := make(map[int]int)
	counter := 1

	copy(g, arr)
	sort.Ints(g)

	for i := range g {
		if i >= 1 && g[i] == g[i-1] {
			counter -= 1
		}
		m[g[i]] = counter
		counter++
	}

	for i := range arr {
		arr[i] = m[arr[i]]
	}

	return arr
}