Premium problem122. One-Hot Encode an Index Array

Easy Locked

codes is a 1-D integer array with values in [0, k). Return the (n, k) integer matrix whose row i has a 1 in column codes[i] and 0 elsewhere.

Input

codes = [0 2 1 2]
k = 3

Output

[[1 0 0]
 [0 0 1]
 [0 1 0]
 [0 0 1]]

Premium problem

This one's part of Premium. Unlock the full NumPy track plus every other premium problem on the site.

Implement solve(...)