Problem 1: "Goat Latin"-style string transformation
You are given a sentence s consisting of words separated by single spaces.
Transform it into a new sentence using these rules:
-
For each word:
-
If it begins with a vowel (
a,e,i,o,u
or
A,E,I,O,U
), keep the word as-is.
-
Otherwise, move its first character to the end of the word.
-
Append the string
"ma"
to the end of the word.
-
Append the letter
'a'
repeated
i
times, where
i
is the 1-based index of the word in the sentence.
-
Join the transformed words with single spaces.
Input: a string s
Output: the transformed string
Problem 2: Minimum total moving distance to sort an array
You are given an integer array arr (may contain duplicates), e.g. [5, 2, 2, 4, 3].
You want to reorder the array into nondecreasing order.
-
An element can be moved by swapping with adjacent elements.
-
The
cost
of moving an element is the number of adjacent positions it shifts (equivalently, each adjacent swap costs 1).
Return the minimum total cost required to transform arr into sorted order.
Input: integer array arr
Output: minimum total moving distance (an integer)
Notes/constraints (typical interview scale):
-
Aim for better than
O(n^2)
time for large
n
.
-
Be careful with duplicates (multiple valid sorted arrangements exist).