6 kyu

Simple Fun #222: Gold Mine Race

62 of 76myjinxin2015

Description:

Story

On a particular continent far far away, a precious gold mine was reported to be found. Every country started to gather up armies and went for the mine. Only one will dominate!

.

Task

Given the x and y coordinates of each city on the continent, the coordinates of the goldmine and speed of each army, determine which city will take over the gold mine if all armies start to move from the cities simultaneously.

The first army to reach the mine captures it. However, if two or more armies reach the gold mine at the same time, they will fight to death, so none of them will get it.

Returns the 0-based index of the city that will dominate, or -1 if no one will.

.

Input/Output

[input] 2D integer array cities

A matrix of cities' coordinates in the format [x, y].

[input] float array speed

Array of the same length as cities, speed[i] is the speed of the army from cities[i].

[input] integer array goldmine

The mine's coordinates in the format [x, y].

[output] an integer

The 0-based index of the city that will dominate, or -1 if no one will.

.

Example

For cities = [[3,0],[2,0],[0,1]], speed = [3,3,3] and goldmine = [0, 0],

the output should be 2.

The armies travel with the same speed, but the second city (0-based) is closer to the gold mine than other two, so their army will reach the mine first.

For cities = [[-3,-4],[3,4],[-3,4],[3,-4]], speed = [2, 2, 5, 5] and goldmine = [0, 0],

the output should be -1.

At first armies from cities 2 and 3 will meet and fight to death. After some time, armies from cities 0 and 1 will battle for the mine and die a heroic death. Thus, no one will conquer the mine, so the answer is -1.

Algorithms
Fundamentals

Stats:

CreatedApr 28, 2017
PublishedApr 28, 2017
Warriors Trained282
Total Skips26
Total Code Submissions402
Total Times Completed76
JavaScript Completions62
Haskell Completions18
Total Stars7
% of votes with a positive feedback rating88% of 34
Total "Very Satisfied" Votes28
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes2
Total Rank Assessments13
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • JohanWiltink Avatar
  • SQSCWQ Avatar
Ad