1182. Shortest Distance to Target Color
You are given an array colors, in which there are three colors: 1, 2 and 3.
You are also given some queries. Each query consists of two integers i and c, return the shortest distance between the given index i and the target color c. If there is no solution return -1.
- binary search solution
- Time : O(n + mlogn)
- Space : O(n)
c++
1 | class Solution { |
- dynamic programming solution
- Time : O(n + m)
- Space : O(n)
c++
1 | class Solution { |