Skip to content

Commit bf7c940

Browse files
committed
update cache
1 parent 62aaf7c commit bf7c940

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

‎Nearest City(Amazon OA).js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ function closestStraightCity(c, x, y, q) {
4242
let cache = {}
4343
for(let string of nbors) {
4444
if(string===query) continue
45-
let key = `${query}-${string}`
46-
let dist = (key in cache)
47-
? cache[key]
48-
: getDist(query, string, pointToIdx, x, y)
45+
let key1 = `${query}-${string}`
46+
let key2 = `${string}-${query}`
47+
let dist = 0
48+
if(key1 in cache) dist = cache[key1]
49+
else if(key2 in cache) dist = cache[key2]
50+
else {
51+
dist = getDist(query, string, pointToIdx, x, y)
52+
cache[key1] = dist
53+
cache[key2] = dist
54+
}
4955
if(dist<minDist) {
5056
minDist = dist
5157
min = string

0 commit comments

Comments
 (0)