Musical Rods
Topics : Greedy algorithms, Prefix sums , sort , Math
You have NN rods with you. The ii-th rod has a length of AiAi and a beauty of BiBi.
You'd like to arrange these rods side-by-side in some order on the number line,
starting from 00.
Let xixi be the starting position of the ii-th rod in an arrangement. The beauty of this
arrangement is
∑𝑖=1 𝑛 xi⋅Bi
What is the maximum beauty you can attain?
Note that the left endpoint of the first rod you place must be 00, and you cannot leave
any space between rods.
Input Format
The first line of input contains an integer TT, denoting the number of test cases.
Each test case consists of three lines of input.
o The first line of each test case contains a single integer NN, the number of rods.
o The second line of each test case contains NN space-separated
integers A1,A2,…,ANA1,A2,…,AN
o The third line of each test case contains NN space-separated
integers B1,B2,…,BNB1,B2,…,BN
Output Format
For each test case print on a new line the answer: the maximum value
of ∑i=1NxiBi∑i=1NxiBi if the order of rods is chosen optimally.
Constraints
1≤T≤1031≤T≤103
1≤N≤1051≤N≤105
1≤Ai≤1041≤Ai≤104
1≤Bi≤1041≤Bi≤104
The sum of NN across all testcases won't exceed 105105.
Sample 1:
Input
2
2
12
46
4
2 8 9 11
25 27 100 45
Output
8
2960
Explanation:
Test case 11: Place the second rod followed by the first one. This makes x2=0x2
=0 and x1=2x1=2, giving us a beauty of 2⋅4+0⋅6=82⋅4+0⋅6=8, which is the maximum
possible.
Test case 22: Place the rods in the order [2,4,3,1][2,4,3,1]. This gives
us x=[28,0,19,8]x=[28,0,19,8], and the beauty
is 28⋅25+0⋅27+19⋅100+8⋅45=296028⋅25+0⋅27+19⋅100+8⋅45=2960.
Red Green Grids
Combinatorics, Dynamic Programming / Precomputatio, Modulo arithmetic
There is an empty grid (all cells are colored white) of NN rows and MM columns.
Chef can fill each cell with either RED or GREEN color.
Chef defines a valid path as a path of cells starting from (1,1)(1,1) and ending
at (N,M)(N,M), where, Chef moves either right or down by 11 cell each time.
For a particular grid, Chef defines the score of the grid as the number of valid paths
containing equal number of RED and GREEN cells.
Find the sum of scores of all possible colored grids containing NN rows
and MM columns.
Since the answer can be large, output it modulo 998244353998244353.
Input Format
The first line of input will contain a single integer TT, denoting the number of test
cases.
Each test case consists of two integers NN and MM - denoting the dimensions of
the grid.
Output Format
For each test case, output the sum of scores of all possible colored grids
containing NN rows and MM columns, modulo 998244353998244353.
Constraints
1≤T≤10001≤T≤1000
1≤N,M≤10001≤N,M≤1000
Sample 1:
Input
Output
3
11
12
23
0
2
72
Explanation:
Test case 11: There are 22 possible coloured grids of size 1×11×1. In the first grid,
the one and only cell is colored RED. In the second grid, the one and only cell is
colored GREEN. Neither of the grids have a valid path with equal number of red and
green cells. Thus, score of each grid is 00.
Test case 22: There are 44 possible coloured grids of the given dimension:
For grids 11 and 44, the scores are 00 as there are no valid paths with equal red and
green cells. Grids 22 and 33 have score 11 each. The valid path with equal red and
green cells is (1,1)→(1,2)(1,1)→(1,2) in both the grids.
Thus, the sum of scores of all possible coloured grids is 22.
Cherry and Bits
Topics : 2D Prefix Sum / Difference Array / XOR properties
Cherry has a binary matrix AA consisting of NN rows and MM columns. The rows are
numbered from 11 to NN, columns are numbered from 11 to MM. Element at
row ii (11 ≤ ii ≤ NN) and column jj (11 ≤ jj ≤ MM) is denoted as AijAij. All elements
of AA are either 00 or 11.
He performs QQ queries on matrix. Each query is provided by four integers x1x1
, y1y1, x2x2, y2y2 which define the rectangle, where (x1x1, y1y1) stands for the
coordinates of the top left cell of the rectangle, while (x2x2, y2y2) stands for the
coordinates of the bottom right cell. You need to flip all the bits i.e.
(00 to 11, 11 to 00) that are located fully inside the query rectangle.
Finally, print the matrix after performing all the queries.
Note: x1x1 represents the row number while y1y1 represents the column number.
###Input:
The first line of the input contains two integers NN and MM — the number of rows
and the number of columns in the matrix.
Each of the next NN lines contains a string of length MM, where the jthjth character
of ithith line denotes the value of Ai,jAi,j.
Next line contains an integer QQ — the number of queries.
Then follow QQ lines with queries descriptions. Each of them contains four space-
seperated integers x1x1, y1y1, x2x2, y2y2 — coordinates of the up left and bottom
right cells of the query rectangle.
###Output:
Print the matrix, in the form of NN strings, after performing all the queries.
###Constraints
1≤N,M≤10001≤N,M≤1000
0≤Aij≤10≤Aij≤1
1≤Q≤1061≤Q≤106
1≤x1≤x2≤N1≤x1≤x2≤N
1≤y1≤y2≤M1≤y1≤y2≤M
Sample 1:
Input
22
00
00
3
1111
2222
1122
Output
01
10
Explanation:
Example case 1:
After processing the 1st query 1 1 1 1, matrix becomes:
[1000][1000] After processing the 2nd query 2 2 2 2, the matrix
becomes: [1001][1001] After processing the 3rd query 1 1 2 2, matrix
becomes: [0110][0110] We need to output the matrix after processing all queries.
Evaluate Division
Topics
Array String Depth-First Search Breadth-First SearchU nion Find Graph Shortest Path
You are given an array of variable pairs equations and an array of real
numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai /
Bi = values[i]. Each Ai or Bi is a string that represents a single variable.
You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query
where you must find the answer for Cj / Dj = ?.
Return the answers to all queries. If a single answer cannot be determined, return -
1.0.
Note: The input is always valid. You may assume that evaluating the queries will not
result in division by zero and that there is no contradiction.
Note: The variables that do not occur in the list of equations are undefined, so the
answer cannot be determined for them.
Example 1:
Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries =
[["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
Explanation:
Given: a / b = 2.0, b / c = 3.0
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
note: x is undefined => -1.0
Example 2:
Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries =
[["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
Output: [3.75000,0.40000,5.00000,0.20000]
Example 3:
Input: equations = [["a","b"]], values = [0.5], queries =
[["a","b"],["b","a"],["a","c"],["x","y"]]
Output: [0.50000,2.00000,-1.00000,-1.00000]
Constraints:
1 <= equations.length <= 20
equations[i].length == 2
1 <= Ai.length, Bi.length <= 5
values.length == equations.length
0.0 < values[i] <= 20.0
1 <= queries.length <= 20
queries[i].length == 2
1 <= Cj.length, Dj.length <= 5
Ai, Bi, Cj, Dj consist of lower case English letters and digits.
Cheapest Flights Within K Stops
Topics
Dynamic Programming Depth -First SearchBreadth- First Search Graph Heap
(Priority Queue)
There are n cities connected by some number of flights. You are given an
array flights where flights[i] = [fromi, toi, pricei] indicates that there is a flight from
city fromi to city toi with cost pricei.
You are also given three integers src, dst, and k, return the cheapest
price from src to dst with at most k stops. If there is no such route, return -1.
Example 1:
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst
= 3, k = 1
Output: 700
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost
100 + 600 = 700.
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2
stops.
Example 2:
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost
100 + 100 = 200.
Example 3:
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph is shown above.
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
Constraints:
1 <= n <= 100
0 <= flights.length <= (n * (n - 1) / 2)
flights[i].length == 3
0 <= fromi, toi < n
fromi != toi
1 <= pricei <= 104
There will not be any multiple flights between two cities.
0 <= src, dst, k < n
src != dst
Get Watched Videos by Your Friends
Topics
Array Hash Table Breadth-First Search Graph
There are n people, each person has a unique id between 0 and n-1. Given the
arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the
list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all
watched videos by the friends of your friends and so on. In general, the level k of
videos are all watched videos by people with the shortest path exactly equal to k with
you. Given your id and the level of videos, return the list of videos ordered by their
frequencies (increasing). For videos with the same frequency order them
alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends =
[[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the
figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends =
[[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the
person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each
bar is 1, compute how much water it can trap after raining.
Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array
[0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2:
Input: height = [4,2,0,3,2,5]
Output: 9
Constraints:
n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105
Reverse Nodes in k-Group
Given the head of a linked list, reverse the nodes of the list k at a time, and return the
modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the
number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it
is.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Example 2:
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
Constraints:
The number of nodes in the list is n.
1 <= k <= n <= 5000
0 <= Node.val <= 1000
Sardar and GCD
Topics: Trees, GCD, Dynamic Programming on Tree, Depth-First Search
Sardar Khan has finally decided to attack Ramadhir - he will bomb one of Ramadhir's
businesses in Wasseypur. Sardar Khan knows that Ramadhir has NN businesses
(numbered 11 to NN) and that there are N−1N−1 roads connecting pairs of these
businesses in such a way that all businesses are connected. Further, Sardar knows that
each business has a specific value - the ii-th business has value AiAi.
When Sardar Khan destroys a business, he also destroys every road directly connected
to it. Formally, destroying business vv also destroys all existing roads of the
form (u,v)(u,v).
Sardar thinks that the damage dealt to Ramadhir upon destroying business vv equals
the sum of the greatest common divisors of every remaining maximal connected set of
businesses. More formally,
Let the connected components of the graph obtained on deleting vv and edges
incident to it be C1,C2,…,CkC1,C2,…,Ck.
Define gcd(Ci)gcd(Ci) to be gcd(Ai1,Ai2,…,Air)gcd(Ai1,Ai2,…,Air
) where CiCi consists of vertices i1,i2,…,iri1,i2,…,ir.
The damage caused by destroying vv is then ∑i=1kgcd(Ci)∑i=1kgcd(Ci).
Sardar Khan wants to maximize the damage he deals. Can you find the maximum
possible damage he can achieve, given that he destroys exactly one business?
If the process of computing damage is still unclear, please refer to sample test cases
for an explained example.
Input Format
The first line of input contains an integer TT, denoting the total number of test
cases. The description of TT test cases follows.
The first line of each test case contains an integer NN, denoting the number of
businesses Ramadhir owns.
Each of the next N−1N−1 lines contains 22 integers aa and bb, denoting a road
between businesses aa and bb.
Finally, the last line of each test case contains NN space-separated positive
integers A1,A2,…,ANA1,A2,…,AN, where AiAi is the value of the ii-th business.
Output Format
For each test case, output a single line containing one integer - the maximum
possible damage that Sardar Khan can deal.
Constraints
1≤T≤10001≤T≤1000
2≤N≤1052≤N≤105
1≤Ai≤1091≤Ai≤109
Sum of NN over all test cases will not exceed 5⋅1055⋅105.
It is guaranteed that the given roads connect every business.
Sample 1:
Input
1
5
12
13
34
35
4 6 8 12 15
Output
29
Explanation:
If business 11 is bombed, the remaining connected components
are {(2),(3,4,5)}{(2),(3,4,5)}. This gives a damage
of gcd(A2)+gcd(A3,A4,A5)=6+1=7gcd(A2)+gcd(A3,A4,A5)=6+1=7.
If business 22 is bombed, the remaining connected components
are {(1,3,4,5)}{(1,3,4,5)}. This gives a damage of gcd(A1,A3,A4,A5)=1gcd(A1
,A3,A4,A5)=1.
If business 33 is bombed, the remaining connected components
are {(1,2),(4),(5)}{(1,2),(4),(5)}. This gives a damage
of gcd(A1,A2)+gcd(A4)+gcd(A5)=2+12+15=29gcd(A1,A2)+gcd(A4
)+gcd(A5)=2+12+15=29.
If business 44 is bombed, the remaining connected components
are {(1,2,3,5)}{(1,2,3,5)}. This gives a damage of gcd(A1,A2,A3,A5)=1gcd(A1
,A2,A3,A5)=1.
If business 55 is bombed, the remaining connected components
are {(1,2,3,4)}{(1,2,3,4)}. This gives a damage of gcd(A1,A2,A3,A4)=2gcd(A1
,A2,A3,A4)=2.
Clearly, bombing business 33 gives the maximum possible damage, that being 29.
Love Squares Hate Cubes
Topic : Mathematics / Number Theory ،Binary Search / Search Algorithms
Kulyash loves perfect squares and hates perfect cubes.
For any natural number NN,
F(N)=number of perfect squares smaller than or equal to N−number of positive perfec
t cubes smaller than or equal to NF(N)=number of perfect squares smaller than or equ
al to N−number of positive perfect cubes smaller than or equal to N.
Kulyash gives you an integer XX and asks you to find the minimum value of NN,
such that F(N)≥XF(N)≥X.
Input Format
The first line of input will contain a single integer TT, denoting the number of test
cases.
Each test case consists of a single line of input.
o The first and only line of each test case contains an integer XX.
Output Format
For each test case, output on a new line, the minimum value of NN such
that F(N)≥XF(N)≥X.
Constraints
1≤T≤1051≤T≤105
1≤X≤1091≤X≤109
Sample 1:
Input
3
1
3
3151
Output
4
25
11397376
Explanation:
Test case 11: There are 22 perfect squares from 11 to 44, and 11 perfect cube
from 11 to 44, so F(4)=2−1=1F(4)=2−1=1, as required.
Test case 22: There are 55 perfect squares from 11 to 2525, and 22 perfect cubes
from 11 to 2525, so F(25)=5−2=3F(25)=5−2=3, as required.
Test case 33: There are 33763376 perfect squares from 11 to 1139737611397376,
and 225225 perfect cubes from 11 to 1139737611397376,
so F(11397376)=3376−225=3151F(11397376)=3376−225=3151, as required
Travelling in the Graph
Topics : Graph Theory, Shortest Path Algorithms- Dijkstra's Algorithm, Union-Find /
DSU (Disjoint Set Union), Math
You are given a graph with NN vertices (numbered 11 to NN) and MM bidirectional
edges, which doesn't contain multiple edges or self-loops — that is, the given graph is
a simple undirected graph.
For each pair of vertices a,ba,b such that 1≤a,b≤N1≤a,b≤N, it is possible to add a new
edge between vertices aa and bb to the graph, with a cost of (a−b)2(a−b)2.
Find the minimum cost of adding edges so that vertex NN is reachable from vertex 11.
Input Format
The first line of input contains a single integer TT, denoting the number of test
cases. The description of TT test cases follows.
The first line of each test case contains 22 space-separated integers, NN and MM.
The ithith of the next MM lines contains 22 space-separated integers uiui and vivi,
denoting a bidirectional edge between vertex uiui and vertex vivi in the graph.
Output Format
For each test case, output the answer in a single line.
Constraints
1≤T≤10001≤T≤1000
2≤N≤2⋅1052≤N≤2⋅105
0≤M≤2⋅1050≤M≤2⋅105
1≤ui,vi≤N1≤ui,vi≤N
The graph doesn't contain self-loops or multiple edges
Sum of NN over all testcases is no more than 2⋅1052⋅105.
Sum of MM over all testcases is no more than 2⋅1052⋅105.
Sample 1:
Input
3
20
54
32
14
53
42
63
12
32
13
Output
1
0
3
Explanation:
Test Case 11: Add an edge between vertices 11 and 22, with cost (1−2)2=1(1−2)2=1.
Test Case 22: Vertices 11 and 55 are already connected, so no edges need to be
added.
E. Manhattan Triangle
The Manhattan distance between two points (x1,y1) and (x2,y2) is defined as:
|x1−x2|+|y1−y2|.
We call a Manhattan triangle three points on the plane, the Manhattan distances between
each pair of which are equal.
You are given a set of pairwise distinct points and an even integer d. Your task is to find
any Manhattan triangle, composed of three distinct points from the given set, where the
Manhattan distance between any pair of vertices is equal to d.
Input
Each test consists of multiple test cases. The first line contains one
integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two
integers n and d (3≤n≤2⋅105, 2≤d≤4⋅105, d is even) — the number of points and the
required Manhattan distance between the vertices of the triangle.
The (i+1)-th line of each test case contains two integers xi and yi (−105≤xi,yi≤105) —
the coordinates of the i-th point. It is guaranteed that all points are pairwise distinct.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, output three distinct integers i, j, and k (1≤i,j,k≤n) — the indices of
the points forming the Manhattan triangle. If there is no solution, output "0 0 0" (without
quotes).
If there are multiple solutions, output any of them.
Example
Input
6
6 4
3 1
0 0
0 -2
5 -3
3 -5
2 -2
5 4
0 0
0 -2
5 -3
3 -5
2 -2
6 6
3 1
0 0
0 -2
5 -3
3 -5
2 -2
4 4
3 0
0 3
-3 0
0 -3
10 8
2 1
-5 -1
-4 -1
-5 -3
0 1
-2 5
-4 4
-4 2
0 0
-4 1
4 400000
100000 100000
-100000 100000
100000 -100000
-100000 -100000
Output
2 6 1
4 3 5
3 5 1
0 0 0
6 1 3
0 0 0
Note
In the first test case:
Points A, B, and F form a Manhattan triangle, the Manhattan distance between each pair of vertices
is 4. Points D, E, and F can also be the answer.
In the third test case:
Points A, C, and E form a Manhattan triangle, the Manhattan distance between each pair of vertices
is 6.
In the fourth test case, there are no two points with a Manhattan distance of 4, and
therefore there is no suitable Manhattan triangle
F. Colorful Polygon
You are given an array a1,a2,…,an, where n≤8 and a1+a2+⋯+an≤100.
Construct a simple polygon∗ with at most 333 vertices that has exactly
(a1+a2+⋯+an)!a1!a2!⋯an!
different triangulations†. It can be proven that such a polygon always exists.
∗A simple polygon is a polygon that does not intersect itself and has no holes. In other words, no two
non-consecutive edges can have common points, and consecutive edges must have exactly one
common point — the vertex between them. Consecutive edges may be collinear.
†A triangulation of a polygon with m vertices is a set of m−3 diagonals that intersect only at vertices. A
diagonal is a segment between two vertices which lies inside the polygon and has exactly two common
points with the polygon sides — the vertices it connects.
Input
The first line of each test contains a single integer n (2≤n≤8) — the number of elements
in the array.
The second line contains n integers a1,a2,…,an (1≤ai≤100) — the elements of the array.
It is guaranteed that a1+a2+⋯+an does not exceed 100.
Output
In the first line, output a single integer m (3≤m≤333) — the number of vertices in the
polygon.
In the i-th of the following m lines, output two integers xi, yi (−106≤xi,yi≤106) — the
coordinates of the i-th vertex of the polygon.
The polygon must be simple. The vertices may be given in either clockwise or
counterclockwise order.
Examples
Input
Copy
3
1 1 2
Output
8
0 0
2 1
4 2
6 1
3 5
4 7
0 5
1 2
Input
2
4 1
Output
Copy
5
-2 -2
-3 1
0 3
3 1
2 -2
Note
In the first test, the required polygon has to have 4!1!1!2!=12 triangulations. The following
are all the triangulations of the example polygon.
In the second test, the required polygon has to have 5!4!1!=5 triangulations.
B. The Third Side
The pink soldiers have given you a sequence a consisting of n positive integers.
You must repeatedly perform the following operation until there is only 1 element left.
Choose two distinct indices i and j.
Then, choose a positive integer value x such that there exists a non-degenerate
triangle∗ with side lengths ai, aj, and x.
Finally, remove two elements ai and aj, and append x to the end of a.
Please find the maximum possible value of the only last element in the sequence a.
∗A triangle with side lengths a, b, c is non-degenerate when a+b>c, a+c>b, b+c>a.
Input
Each test contains multiple test cases. The first line contains the number of test
cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤1000) — the
elements of the sequence a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, output the maximum possible value of the only last element on a
separate line.
Example
Input
4
1
10
3
998 244 353
5
1 2 3 4 5
9
9 9 8 2 4 4 3 5 3
Output
10
1593
11
39
Note
On the first test case, there is already only one element. The value of the only last
element is 10.
On the second test case, a is initially [998,244,353]. The following series of operations
is valid:
1. Erase a2=244 and a3=353, and append 596 to the end of a. a is now [998,596].
2. Erase a1=998 and a2=596, and append 1593 to the end of a. a is now [1593].
It can be shown that the only last element cannot be greater than 1593. Therefore, the
answer is 1593.
Max Points on a Line
Topics
ArrayHash TableMathGeometry
Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane,
return the maximum number of points that lie on the same straight line.
Example 1:
Input: points = [[1,1],[2,2],[3,3]]
Output: 3
Example 2:
Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Constraints:
1 <= points.length <= 300
points[i].length == 2
-104 <= xi, yi <= 104
All the points are unique
Best Position for a Service Centre
Topics
Array Math Geometry Randomized
A delivery company wants to build a new service center in a new city. The company
knows the positions of all the customers in this city on a 2D-Map and wants to build the
new center in a position such that the sum of the euclidean distances to all customers
is minimum.
Given an array positions where positions[i] = [xi, yi] is the position of the ith customer on the
map, return the minimum sum of the euclidean distances to all customers.
In other words, you need to choose the position of the service center [xcentre, ycentre] such that
the following formula is minimized:
Answers within 10-5 of the actual value will be accepted.
Example 1:
Input: positions = [[0,1],[1,0],[1,2],[2,1]]
Output: 4.00000
Explanation: As shown, you can see that choosing [xcentre, ycentre] = [1, 1] will make the distance
to each customer = 1, the sum of all distances is 4 which is the minimum possible we can
achieve.
Example 2:
Input: positions = [[1,1],[3,3]]
Output: 2.82843
Explanation: The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843
Constraints:
1 <= positions.length <= 50
positions[i].length == 2