For each word in a list, determine the minimum number of character replacements needed so that no two adjacent characters are the same. If any two adjacent characters in a string are equal, one of them must be changed.
Example
words = ["add", "boook", "break"]
Output: [1, 1, 0]
"add": change one'd'(1 change)"boook": change the middle'o'(1 change)"break": no change needed (0)
Constraints
- 1 ≤ n ≤ 100
- 2 ≤ length of
words[i]≤ 10⁵ - characters are lowercase
a-z.
解法
扫描字符串,统计每段同字符极大连续段的长度 L;每段贡献 L / 2 次替换(隔一个改一个即可)。累加所有段。复杂度 O(total length)。
def minimal_operations(words):
ans = []
for w in words:
cnt = 0
i = 0
n = len(w)
while i < n:
j = i
while j < n and w[j] == w[i]:
j += 1
cnt += (j - i) // 2
i = j
ans.append(cnt)
return ansclass Solution {
static int[] minimalOperations(String[] words) {
int[] ans = new int[words.length];
for (int k = 0; k < words.length; k++) {
String w = words[k];
int n = w.length(), cnt = 0, i = 0;
while (i < n) {
int j = i;
while (j < n && w.charAt(j) == w.charAt(i)) j++;
cnt += (j - i) / 2;
i = j;
}
ans[k] = cnt;
}
return ans;
}
}vector<int> minimalOperations(vector<string>& words) {
vector<int> ans;
for (auto& w : words) {
int n = w.size(), cnt = 0, i = 0;
while (i < n) {
int j = i;
while (j < n && w[j] == w[i]) j++;
cnt += (j - i) / 2;
i = j;
}
ans.push_back(cnt);
}
return ans;
}A marketing company tracks advertising campaigns and engagements. Generate a report that, for every active campaign, lists the campaign name, the total number of engagements, and the combined sum of views and clicks. Columns: campaign_name | total_engagements | total_views_and_clicks. Sort by campaign_name ascending.
Schema:
campaigns
| name | type | description |
|---|---|---|
| id | INT PK | campaign id |
| name | VARCHAR(255) | campaign name |
| is_active | SMALLINT | 1 if active, 0 if inactive |
engagements
| name | type | description |
|---|---|---|
| campaign_id | INT FK | reference to campaigns(id) |
| views | INT | views count |
| clicks | INT | clicks count |
解法
campaigns 与 engagements 按 id = campaign_id 内连接,过滤 is_active = 1,按 campaigns.name 分组,聚合 COUNT(*) 和 SUM(views + clicks),按 name 排序。若需要展示零互动的 campaign,换成 LEFT JOIN 加 COALESCE。
SELECT
c.name AS campaign_name,
COUNT(e.campaign_id) AS total_engagements,
COALESCE(SUM(e.views + e.clicks), 0) AS total_views_and_clicks
FROM campaigns c
LEFT JOIN engagements e ON e.campaign_id = c.id
WHERE c.is_active = 1
GROUP BY c.id, c.name
ORDER BY c.name ASC;Q1: Big-O notation definition
Big(O) notation:
- (A) identifies the best algorithm to solve a problem.
- (B) determines maximum size of a problem that can be solved on a system in a given time.
- (C) is the lower bound of the growth rate of an algorithm.
- (D) is the upper bound of the growth rate of an algorithm.
- (E) None of the above.
Answer: D. Big-O describes an asymptotic upper bound of the growth rate; Big-Omega is the lower bound, Big-Theta is tight.
Q2: Quicksort partition pivot index
Given array [76, 97, 25, 115, 83, 51, 18, 36], quicksort with the first element (76) as pivot. After one Lomuto/Hoare partition, what is the 1-indexed position of the pivot?
- (A) 1
- (B) 3
- (C) 5
- (D) 4
Answer: C (5). Elements less than 76 are 25, 51, 18, 36 (4 elements). After partition, 76 sits at index 5 (1-indexed) with the 4 smaller values to its left and 97, 115, 83 to its right.
Q3: SQL IN syntax
Pick the expression with the correct syntax.
- (A)
SELECT * FROM logs WHERE priority IN ('high', 'medium') - (B)
SELECT * FROM logs WHERE priority VALUES IN ('high', 'medium') - (C)
SELECT * FROM logs WHERE priority IN VALUES ('high', 'medium') - (D)
SELECT * FROM logs WHERE priority IN SET ('high', 'medium')
Answer: A. Standard SQL IN takes a parenthesized value list directly; VALUES / SET keywords are not valid here.