[Swift]LeetCode1182. 與目標顏色間的最短距離 | Shortest Distance to Target Color

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-kgqexibe-hp.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You are given an array colors, in which there are three colors: 12 and 3.git

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.github

 

Example 1:算法

Input: colors = [1,1,2,1,3,2,2,3,3], queries = [[1,3],[2,2],[6,1]]
Output: [3,0,3]
Explanation: 
The nearest 3 from index 1 is at index 4 (3 steps away).
The nearest 2 from index 2 is at index 2 itself (0 steps away).
The nearest 1 from index 6 is at index 3 (3 steps away).

Example 2:數組

Input: colors = [1,2], queries = [[0,3]]
Output: [-1]
Explanation: There is no 3 in the array.

 

Constraints:微信

  • 1 <= colors.length <= 5*10^4
  • 1 <= colors[i] <= 3
  • 1 <= queries.length <= 5*10^4
  • queries[i].length == 2
  • 0 <= queries[i][0] < colors.length
  • 1 <= queries[i][1] <= 3

 

給你一個數組 colors,裏面有  12、 3 三種顏色。app

咱們須要在 colors 上進行一些查詢操做 queries,其中每一個待查項都由兩個整數 i 和 c 組成。ide

如今請你幫忙設計一個算法,查找從索引 i 到具備目標顏色 c 的元素之間的最短距離。spa

若是不存在解決方案,請返回 -1設計

 

示例 1:

輸入:colors = [1,1,2,1,3,2,2,3,3], queries = [[1,3],[2,2],[6,1]]
輸出:[3,0,3]
解釋: 
距離索引 1 最近的顏色 3 位於索引 4(距離爲 3)。
距離索引 2 最近的顏色 2 就是它本身(距離爲 0)。
距離索引 6 最近的顏色 1 位於索引 3(距離爲 3)。

示例 2:

輸入:colors = [1,2], queries = [[0,3]]
輸出:[-1]
解釋:colors 中沒有顏色 3。

 

提示:

  • 1 <= colors.length <= 5*10^4
  • 1 <= colors[i] <= 3
  • 1 <= queries.length <= 5*10^4
  • queries[i].length == 2
  • 0 <= queries[i][0] < colors.length
  • 1 <= queries[i][1] <= 3

Runtime: 1880 ms

Memory Usage: 28.9 MB
 1 class Solution {
 2     func shortestDistanceColor(_ colors: [Int], _ queries: [[Int]]) -> [Int] {
 3         let n:Int = colors.count
 4         var left:[[Int]] = [[Int]](repeating:[Int](repeating:-1,count:n),count:4)
 5         var right:[[Int]] = [[Int]](repeating:[Int](repeating:-1,count:n),count:4)
 6         for shade in 1...3
 7         {
 8             if colors[0] == shade
 9             {
10                 left[shade][0] = 0
11             }
12             for i in 1..<n
13             {
14                 if left[shade][i-1] != -1
15                 {
16                     left[shade][i] = left[shade][i-1] + 1
17                 }
18                 if colors[i] == shade
19                 {
20                     left[shade][i] = 0
21                 }
22             }
23         }
24         for shade in 1...3
25         {
26             if colors[n-1] == shade
27             {
28                 right[shade][n-1] = 0
29             }
30             for i in stride(from:n - 2,through:0,by:-1)
31             {
32                 if right[shade][i+1] != -1
33                 {
34                     right[shade][i] = right[shade][i+1] + 1
35                 }
36                 if colors[i] == shade
37                 {
38                     right[shade][i] = 0
39                 }
40             }
41         }
42         var result:[Int] = [Int]()
43         for query in queries
44         {
45             var index:Int = query[0]
46             var req_color = query[1]
47             var x:Int = left[req_color][index]
48             var y:Int = right[req_color][index]
49             var ans:Int = 0
50             if x == -1 || y == -1
51             {
52                 ans = max(x,y)
53             }
54             else
55             {
56                 ans = min(x,y)
57             }
58             result.append(ans)
59         }
60         return result
61     }
62 }
相關文章
相關標籤/搜索