Skip to content

Commit e5dab3e

Browse files
committed
add 1779 in JS
1 parent 48503bb commit e5dab3e

4 files changed

Lines changed: 61 additions & 42 deletions

File tree

javascript/LeetCode/Array/1779.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1779. Find Nearest Point That Has the Same X or Y Coordinate
3+
*
4+
* x & y = current location
5+
* A point is valid if it shares the same x-coordinate or the same y-coordinate as your location.
6+
* 傳回與目前位置曼哈頓距離最小的有效點的索引(從 0 開始索引)。
7+
* 如果存在多個有效點,則傳回索引最小的有效點。如果沒有有效點,則傳回 -1。
8+
*
9+
* The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2).
10+
*
11+
* @param {number} x
12+
* @param {number} y
13+
* @param {number[][]} points
14+
* @return {number}
15+
*/
16+
var nearestValidPoint = function(x, y, points) {
17+
let ans = -1;
18+
let smallest = Infinity;
19+
for(let i = 0;i < points.length;++i) {
20+
let prev = Math.abs(x - parseInt(points[i][0]));
21+
let next = Math.abs(y - parseInt(points[i][1]));
22+
if(prev * next === 0 && (prev + next) < smallest){
23+
smallest = prev + next;
24+
ans = i;
25+
}
26+
27+
}
28+
return ans;
29+
};
30+
// let x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]];
31+
// 2
32+
// Of all the points, only [3,1], [2,4] and [4,4] are valid.
33+
// Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1.
34+
// [2,4] has the smallest index, so return 2.
35+
36+
// let x = 3, y = 4, points = [[3,4]];
37+
// 0
38+
// let x = 3, y = 4, points = [[2,3]];
39+
// -1
40+
41+
let x = 5, y = 1, points = [[1,1],[6,2],[1,5],[3,1]];
42+
// 3
43+
console.log(nearestValidPoint(x,y,points));

javascript/index.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,40 +1176,3 @@ let nums = [8,4,2,8,4];
11761176
// console.log(specialTriplets(nums));
11771177

11781178

1179-
/**
1180-
* 1779. Find Nearest Point That Has the Same X or Y Coordinate
1181-
*
1182-
* x & y = current location
1183-
* A point is valid if it shares the same x-coordinate or the same y-coordinate as your location.
1184-
* 沒有回傳-1
1185-
*
1186-
* The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2).
1187-
*
1188-
* @param {number} x
1189-
* @param {number} y
1190-
* @param {number[][]} points
1191-
* @return {number}
1192-
*/
1193-
var nearestValidPoint = function(x, y, points) {
1194-
1195-
// abs(x-validPoint[0]) or abs(y-validPoint[1]) .
1196-
let totalDistance = x + y;
1197-
let ans = [];
1198-
for(const p of points) {
1199-
let prev = parseInt(p[0]),next = parseInt(p[1]);
1200-
if(prev === x || next === y){
1201-
let distance = Math.abs(x - prev) + Math.abs(y - next);
1202-
ans.push(distance);
1203-
}
1204-
1205-
}
1206-
console.log(ans)
1207-
};
1208-
let x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]];
1209-
// 2
1210-
// Of all the points, only [3,1], [2,4] and [4,4] are valid.
1211-
// Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1.
1212-
// [2,4] has the smallest index, so return 2.
1213-
// let x = 3, y = 4, points = [[3,4]];
1214-
// 0
1215-
console.log(nearestValidPoint(x,y,points));

python/index.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,21 @@ def replaceDigits(s: str) -> str:
267267
# Output: "abcdef"
268268
# print(replaceDigits(s))
269269

270-
def startswith(w):
271-
return w.startswith("a")
272-
li = ['apple','orange','pineapple','grape']
273-
res = filter(startswith,li)
274-
print(list(res))
270+
class So:
271+
def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
272+
ans = -1
273+
smallest = float('inf')
274+
for i in range(0,len(points)):
275+
prev = x - int(points[i][0])
276+
nextOne = y - int(points[1][0])
277+
if (prev * nextOne) == 0 and abs(prev + nextOne) < smallest:
278+
smallest = abs(prev + nextOne)
279+
ans = i
280+
return ans
281+
282+
c = So()
283+
x = 3
284+
y = 4
285+
points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
286+
# 2
287+
print(c.nearestValidPoint(x,y,points))

0 commit comments

Comments
 (0)