time:O(n),space:O(k)