Created
October 21, 2023 06:22
-
-
Save bunnykek/250c969d75e7a924f60c3a8255179386 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Security Values | |
ALL The 26 characters of the alphabet are each assigned a security value represented as an array of integers, where security values[i] is associated with the character of the alphabet. Given an encrypted message, msg, and the array security values, rearrange the characters in msg and find the minimum possible sum of the absolute differences of the security values of adjacent characters. | |
Example | |
Given security values[i]-[1, 2, 1, 3, 1, 3, 5, 7, 1, 1, 5, 5, 8, 10, 11, 1, 23, 2, 3, 7, 8, 9, 1, 6, 5, 9] and s = "afeb". | |
Here 'a' relates to 1, 'f' relates to 3, 'e' relates to 1, | |
and 'b' relates to 2. Some of the rearrangements are: | |
Rearrangement | |
Adjacent Difference | |
afeb | |
fbae | |
11-31+13-11+11-21=5 | |
13-21+12-11+11-11=2 | |
eabf | |
11-11 11-21+12-31-2 | |
Function Description | |
ALL | |
O | |
ndous | |
Complete the function getMinSum in the editor | |
below | |
getMinSum takes the following arguments: int security values[n]: the values of the | |
characters string msg the encrypted message | |
Returns | |
int: the minimum possible sum of absolute differences of the values of adjacent characters | |
Constraints | |
1s|msgls 105 | |
1s security values(i) ≤ 109 | |
Input Format For Custom Testing | |
Sample Case 0 | |
Sample Input For Custom Testing | |
STDIN | |
FUNCTION | |
26 | |
5526 | |
security values[] | |
size | |
int solve(vector<int> sec_values, string s){ | |
vector<int> aux; | |
for(char &c: s) | |
aux.push_back(sec_values[c-'a']); | |
sort(aux.begin(), aux.end()); | |
int sum = 0; | |
for(int i=0; i<aux.size()-1; i++) | |
sum+= abs(aux[i+1]-aux[i]); | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment