Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Coding Interview
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dmytro Bogatov
Coding Interview
Commits
b8e8d433
Verified
Commit
b8e8d433
authored
Jun 1, 2020
by
Dmytro Bogatov
Browse files
Options
Downloads
Patches
Plain Diff
Add docs to portals.
parent
59d9dbd8
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/School/Portals.cs
+17
-5
17 additions, 5 deletions
src/School/Portals.cs
with
17 additions
and
5 deletions
src/School/Portals.cs
+
17
−
5
View file @
b8e8d433
...
...
@@ -35,40 +35,50 @@ namespace CodingInterview.School
/// Returns the minmum amount of money needed to get all plannets connected.
/// </summary>
/// <param name="costs">the costs of building a portal on each planet (index is a planet)</param>
/// <param name="connections">pairs of indices representing planets which already have portals between them</param>
/// <param name="connections">pairs of indices representing planets which already have portals between them
(here indexaton starts from 1)
</param>
/// <returns>the minmum amount of money needed to get all plannets connected</returns>
public
int
Solve
(
int
[]
costs
,
(
int
from
,
int
to
)[]
connections
)
{
// create planets given costs
var
planets
=
new
Planet
[
costs
.
Length
];
for
(
var
planet
=
0
;
planet
<
costs
.
Length
;
planet
++)
{
planets
[
planet
]
=
new
Planet
{
Cost
=
costs
[
planet
]
};
}
// setup connections
foreach
(
var
connection
in
connections
)
{
planets
[
connection
.
from
-
1
].
Neighbors
.
Add
(
planets
[
connection
.
to
-
1
]);
planets
[
connection
.
to
-
1
].
Neighbors
.
Add
(
planets
[
connection
.
from
-
1
]);
}
// go over all planets in a BFS manner
var
minCosts
=
new
List
<
int
>();
for
(
var
planet
=
0
;
planet
<
costs
.
Length
;
planet
++)
{
// skip visited planets
if
(
planets
[
planet
].
Visited
)
{
continue
;
}
// initialize BFS queue with the initial (unvisited) planet
var
minCost
=
Int32
.
MaxValue
;
var
bfsQueue
=
new
Queue
<
Planet
>();
bfsQueue
.
Enqueue
(
planets
[
planet
]);
// until entire cluster is traversed
while
(
bfsQueue
.
Count
>
0
)
{
var
current
=
bfsQueue
.
Dequeue
();
// skip visited
if
(!
current
.
Visited
)
{
// update min cost in the cluster
minCost
=
Math
.
Min
(
current
.
Cost
,
minCost
);
// don't go here again
current
.
Visited
=
true
;
// add not visited neighbors to the queue
foreach
(
var
neighbor
in
current
.
Neighbors
)
{
if
(!
neighbor
.
Visited
)
...
...
@@ -78,14 +88,16 @@ namespace CodingInterview.School
}
}
}
if
(
minCost
!=
Int32
.
MaxValue
)
{
// remember min cost of the cluster
minCosts
.
Add
(
minCost
);
}
}
// find the smallest of all min costs
var
minOfMin
=
minCosts
.
Min
();
// if T is the number of clusters, the final cost is building T - 1 portals in the cheapest cluster on its cheapest planet,
// then building a portal on a cheapest planet on each of the other clusters
return
minOfMin
*
(
minCosts
.
Count
-
1
)
+
(
minCosts
.
Sum
()
-
minOfMin
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment