-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAncientTomb.java
More file actions
59 lines (44 loc) · 1.73 KB
/
Copy pathAncientTomb.java
File metadata and controls
59 lines (44 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Question : https://leetcode.com/discuss/interview-question/1767683/indeed-karat-phone-screening-internship
import java.util.Arrays;
public class AncientTomb {
public static void main(String args[]){
System.out.println("Ancient Tomb Problem");
System.out.println( minDistance("rRBGYygbr"));
}
private static int minDistance(String layout){
int d =0;
int dist[][] = new int[26][3];
for (int[] row: dist)
Arrays.fill(row, -1);
for (int i = layout.length()-1;i>=0;--i){
char cur = layout.charAt(i);
if(Character.isUpperCase(cur)){
dist[cur-'A'][0] = 1;
}else if(dist[Character.toUpperCase(cur)-'A'][0]==0){
dist[Character.toUpperCase(cur)-'A'][2] = i;
}else {
if(dist[Character.toUpperCase(cur)-'A'][1]==-1)
dist[Character.toUpperCase(cur)-'A'][1]=i;
}
}
for(int i=0;i<layout.length();i++){
char cur = layout.charAt(i);
if(Character.isUpperCase(cur)){
int cur_dis =0 ;
int left_index = dist[Character.toUpperCase(cur)-'A'][1];
int right_index = dist[Character.toUpperCase(cur)-'A'][2];
if(left_index==-1 && right_index==-1)
continue;
if(left_index==-1)
d = d + (right_index-i);
else if (right_index==-1)
d = d+ (i-left_index);
else {
cur_dis = (i-left_index) > (right_index-i) ? (right_index-i) : (i-left_index);
d+=cur_dis;
}
}
}
return d;
}
}