질문

e95518a2-5cfa-4bef-8b26-c0f4719a2895.jpg
6e4323bb-e4a1-4c58-8f5e-4cd055a046cd.jpg
f1c049e2-98e4-482b-bf9b-ad9b86c7e8ce.jpg
a59ba37e-0a9b-41bd-b501-bd41edb6b128.jpg

in c++

사고

Understanding the problem logic and constraints

The task requires counting equal-character pairs in substrings, and each partition has a cost. In the example "abcabcc", counting pairs and splits shows a sum of sameTime and partitionTime. The goal is to minimize this. Given the constraints (with n up to 2500), it suggests using a long long internally, but returning an int based on the function signature.

Handling potential overflow and solving with DP

It seems like the constraints might allow values to exceed the typical int range (up to 2e9), so I’ll use long long internally, but the function signature requires an int return type. I’ll cast the result to int at the end. For the actual solution, I’ll use dynamic programming (DP) to minimize the cost, considering prefix lengths and calculating pairs. The time complexity should work efficiently with n=2500.

Thought for 14s

답변

Paste this inside getMinimumTime:

cpp
int getMinimumTime(int sameTime, int partitionTime, string s) { int n = s.size(); const long long INF = 4e18; vector<long long> dp(n + 1, INF); dp[0] = 0; for (int i = 1; i <= n; i++) { vector<int> freq(256, 0); long long pairs = 0; // Try every last partition: s[j ... i-1] for (int j = i - 1; j >= 0; j--) { unsigned char ch = s[j]; // Adding this character creates pairs with same characters already inside pairs += freq[ch]; freq[ch]++; long long cost = pairs * sameTime; if (j > 0) { cost += partitionTime; // cut before this partition } dp[i] = min(dp[i], dp[j] + cost); } } return (int)dp[n]; }

Idea: dp[i] is the minimum time for the first i characters.
For every possible last substring, we count how many equal-character pairs it contains and choose the cheapest split.

질문

c4af7e51-4f85-4137-9616-d2740d37c64a.jpg
749bea65-613b-4d1c-af8f-1f14fb53c1c9.jpg
39208bb9-37f3-4321-9ab1-b5220bd1e002.jpg
89414c88-1402-4a05-ab4d-2fe8e9d42be5.jpg

in c++

사고

Handling function signature and calculation

I’m considering the function signature for the formula: it should be long getTotalPalindromeTransformationCost(string dna), as the HackerRank editor expects a 64-bit long. I can compute the total palindrome transformation cost with an efficient approach. I’ll ensure the signature and return match the expected type.

Thought for 25s

답변

Paste this inside the function:

cpp
long getTotalPalindromeTransformationCost(string dna) { int n = dna.size(); vector<long long> cnt0(26, 1), cnt1(26, 0); vector<int> parity(26, 0); for (char ch : dna) { int x = ch - 'a'; parity[x] ^= 1; for (int i = 0; i < 26; i++) { if (parity[i]) cnt1[i]++; else cnt0[i]++; } } long long totalOddFreq = 0; for (int i = 0; i < 26; i++) { totalOddFreq += cnt0[i] * cnt1[i]; } long long evenPrefix = n / 2 + 1; long long oddPrefix = (n + 1) / 2; long long oddLengthSubstrings = evenPrefix * oddPrefix; long long answer = (totalOddFreq - oddLengthSubstrings) / 2; return answer; }

For any substring, only character counts with odd frequency matter.
If a substring has k odd-frequency characters, its minimum transformation cost is:

cpp
k / 2

The code sums this efficiently for all substrings in O(26 * n), which works for n <= 2 * 10^5.

이 Q&A 공유하기