Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2f21fdd

Browse files
committed
Add solution #2463
1 parentfbed85a commit2f21fdd

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#1,963 LeetCode solutions in JavaScript
1+
#1,964 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1858,6 +1858,7 @@
18581858
2460|[Apply Operations to an Array](./solutions/2460-apply-operations-to-an-array.js)|Easy|
18591859
2461|[Maximum Sum of Distinct Subarrays With Length K](./solutions/2461-maximum-sum-of-distinct-subarrays-with-length-k.js)|Medium|
18601860
2462|[Total Cost to Hire K Workers](./solutions/2462-total-cost-to-hire-k-workers.js)|Medium|
1861+
2463|[Minimum Total Distance Traveled](./solutions/2463-minimum-total-distance-traveled.js)|Hard|
18611862
2467|[Most Profitable Path in a Tree](./solutions/2467-most-profitable-path-in-a-tree.js)|Medium|
18621863
2469|[Convert the Temperature](./solutions/2469-convert-the-temperature.js)|Easy|
18631864
2482|[Difference Between Ones and Zeros in Row and Column](./solutions/2482-difference-between-ones-and-zeros-in-row-and-column.js)|Medium|
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* 2463. Minimum Total Distance Traveled
3+
* https://leetcode.com/problems/minimum-total-distance-traveled/
4+
* Difficulty: Hard
5+
*
6+
* There are some robots and factories on the X-axis. You are given an integer array robot where
7+
* robot[i] is the position of the ith robot. You are also given a 2D integer array factory where
8+
* factory[j] = [positionj, limitj] indicates that positionj is the position of the jth factory
9+
* and that the jth factory can repair at most limitj robots.
10+
*
11+
* The positions of each robot are unique. The positions of each factory are also unique. Note
12+
* that a robot can be in the same position as a factory initially.
13+
*
14+
* All the robots are initially broken; they keep moving in one direction. The direction could be
15+
* the negative or the positive direction of the X-axis. When a robot reaches a factory that did
16+
* not reach its limit, the factory repairs the robot, and it stops moving.
17+
*
18+
* At any moment, you can set the initial direction of moving for some robot. Your target is to
19+
* minimize the total distance traveled by all the robots.
20+
*
21+
* Return the minimum total distance traveled by all the robots. The test cases are generated
22+
* such that all the robots can be repaired.
23+
*
24+
* Note that:
25+
* - All robots move at the same speed.
26+
* - If two robots move in the same direction, they will never collide.
27+
* - If two robots move in opposite directions and they meet at some point, they do not collide.
28+
* They cross each other.
29+
* - If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.
30+
* - If the robot moved from a position x to a position y, the distance it moved is |y - x|.
31+
*/
32+
33+
/**
34+
*@param {number[]} robot
35+
*@param {number[][]} factory
36+
*@return {number}
37+
*/
38+
varminimumTotalDistance=function(robot,factory){
39+
robot.sort((a,b)=>a-b);
40+
factory.sort((a,b)=>a[0]-b[0]);
41+
42+
constrobotCount=robot.length;
43+
constfactoryCount=factory.length;
44+
constmemo=newArray(robotCount+1).fill(null).map(()=>newArray(factoryCount+1).fill(-1));
45+
46+
returncalculateMinDistance(0,0);
47+
48+
functioncalculateMinDistance(robotIndex,factoryIndex){
49+
if(robotIndex===robotCount)return0;
50+
if(factoryIndex===factoryCount)return1e18;
51+
52+
if(memo[robotIndex][factoryIndex]!==-1){
53+
returnmemo[robotIndex][factoryIndex];
54+
}
55+
56+
letresult=calculateMinDistance(robotIndex,factoryIndex+1);
57+
58+
lettotalDistance=0;
59+
for(letrobotsTaken=0;robotsTaken<factory[factoryIndex][1]
60+
&&robotIndex+robotsTaken<robotCount;robotsTaken++){
61+
totalDistance+=Math.abs(robot[robotIndex+robotsTaken]-factory[factoryIndex][0]);
62+
result=Math.min(
63+
result,
64+
totalDistance+calculateMinDistance(robotIndex+robotsTaken+1,factoryIndex+1)
65+
);
66+
}
67+
68+
memo[robotIndex][factoryIndex]=result;
69+
returnresult;
70+
}
71+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp