Code Question
Amazon Web Services has nn servers, each of them either has high fault tolerance or high reliability. A system works better if all the servers have the same attributes. The inefficiency of a group of servers is defined as the number of adjacent pairs of servers that have different attributes.
Consider, for example, a set of servers described as 1001001
where '0' means the server has high fault tolerance, '1' means the server has high reliability. The inefficiency of this group is 4 as described in the image below:
(Each adjacent pair with different attributes contributes to inefficiency.)
Problem
Given a string serverType
of length nn consisting of '0'
, '1'
and '?'
, where:
'0'
means the server has high fault tolerance.'1'
means the server has high reliability.'?'
means you can install any type of server there.
Find the minimum inefficiency you can get after installing a server at each '?'
.
Example
Input:serverType = "??011??0"
In the above example, the number of servers n=8n = 8. One optimal way to install servers is to:
- Install a server having high fault tolerance (
0
) at the first and the second positions. - Install a server having high reliability (
1
) at the sixth and seventh positions.
After making these changes, the server types are "00011110"
. The number of adjacent pairs having different server types is 2.
It can be shown that the answer cannot be reduced from 2. Return 2.
Note
Another possible way to achieve a minimum number of different adjacent pairs as 2 would be 00011100
and 00011000
.
Code Question
Developers at Amazon are working on a new sorting algorithm for points on the x-axis of the coordinate system.
There are n points. The i<sup>th</sup> point initially has a weight of weight[i]
and is located at position i on the x-axis. In a single operation, the i<sup>th</sup> point can be moved to the right by a distance of dist[i]
.
Given weight
and dist
, find the minimum number of operations required to sort the points by their weights.
Example
Suppose n = 4, weight = [3, 6, 5, 1]
and dist = [4, 3, 2, 1]
.
Thus, the number of operations required are 1 + 2 + 2 = 5.
Function Description
Complete the function getMinOperations
in the editor below.
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.