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

Commitbfae659

Browse files
committed
Add solution #2477
1 parent62689a5 commitbfae659

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-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,972 LeetCode solutions in JavaScript
1+
#1,973 LeetCode solutions in JavaScript
22

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

@@ -1868,6 +1868,7 @@
18681868
2472|[Maximum Number of Non-overlapping Palindrome Substrings](./solutions/2472-maximum-number-of-non-overlapping-palindrome-substrings.js)|Hard|
18691869
2475|[Number of Unequal Triplets in Array](./solutions/2475-number-of-unequal-triplets-in-array.js)|Easy|
18701870
2476|[Closest Nodes Queries in a Binary Search Tree](./solutions/2476-closest-nodes-queries-in-a-binary-search-tree.js)|Medium|
1871+
2477|[Minimum Fuel Cost to Report to the Capital](./solutions/2477-minimum-fuel-cost-to-report-to-the-capital.js)|Medium|
18711872
2482|[Difference Between Ones and Zeros in Row and Column](./solutions/2482-difference-between-ones-and-zeros-in-row-and-column.js)|Medium|
18721873
2490|[Circular Sentence](./solutions/2490-circular-sentence.js)|Easy|
18731874
2493|[Divide Nodes Into the Maximum Number of Groups](./solutions/2493-divide-nodes-into-the-maximum-number-of-groups.js)|Hard|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 2477. Minimum Fuel Cost to Report to the Capital
3+
* https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/
4+
* Difficulty: Medium
5+
*
6+
* There is a tree (i.e., a connected, undirected graph with no cycles) structure country network
7+
* consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads. The capital city is
8+
* city 0. You are given a 2D integer array roads where roads[i] = [ai, bi] denotes that there
9+
* exists a bidirectional road connecting cities ai and bi.
10+
*
11+
* There is a meeting for the representatives of each city. The meeting is in the capital city.
12+
*
13+
* There is a car in each city. You are given an integer seats that indicates the number of seats
14+
* in each car.
15+
*
16+
* A representative can use the car in their city to travel or change the car and ride with another
17+
* representative. The cost of traveling between two cities is one liter of fuel.
18+
*
19+
* Return the minimum number of liters of fuel to reach the capital city.
20+
*/
21+
22+
/**
23+
*@param {number[][]} roads
24+
*@param {number} seats
25+
*@return {number}
26+
*/
27+
varminimumFuelCost=function(roads,seats){
28+
constn=roads.length+1;
29+
constgraph=Array.from({length:n},()=>[]);
30+
for(const[a,b]ofroads){
31+
graph[a].push(b);
32+
graph[b].push(a);
33+
}
34+
35+
letfuel=0;
36+
dfs(0,-1);
37+
returnfuel;
38+
39+
functiondfs(node,parent){
40+
letrepresentatives=1;
41+
for(constneighborofgraph[node]){
42+
if(neighbor!==parent){
43+
representatives+=dfs(neighbor,node);
44+
}
45+
}
46+
if(node!==0){
47+
fuel+=Math.ceil(representatives/seats);
48+
}
49+
returnrepresentatives;
50+
}
51+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp