蚂蚁国际
蚂蚁国际的年终,是从今年三月中开始陆续开奖的,目前基本尘埃落定。
今年蚂蚁国际的年终奖,仍然是"备受瞩目",主要原因是去年蚂蚁国际的年终奖水平相比其他 BU 实在太高。
虽然去年蚂蚁国际的年终奖相比于前年打了 8~9 折,但仍然显著高于其他部门。例如同为 3.5 绩效,蚂蚁国际去年可获得 3.5 个月年终,而网商部门仅有 2 个月。
我们来看一下今年的年终奖情况如何:
-
3.5:年终 3.5~4 个月,涨薪 5%~6%(去年 1.5~2.5 个月) -
3.5+:年终 4~4.5 个月,涨薪 5%~8%(去年 3~4.5 个月,其中 3.5 个月居多) -
3.75:年终 4.5~6 个月,涨薪 6%~10%(去年 4.5~6 个月)
整体看来,今年蚂蚁国际的年终奖有所回升,但涨幅不算明显,尤其是对于高绩效的同学来说。
不过更有意思的是,今年蚂蚁进行了 13 薪合并,因此理论上每位员工的月薪都有所上升。
月薪上升,年终月数也回暖,如此一来,今年到手的年终奖提升应该还是不小的。
对此,你怎么看?蚂蚁国际今年的年终奖,符合你的猜测预期吗?欢迎评论区交流。
...
回归主题。
周末,安排一道经典算法题。
题目描述
平台:LeetCode
题号:451
给定一个字符串,请将字符串里的字符按照出现的频率降序排列。
示例 1:
输入:
"tree"
输出:
"eert"
解释:
'e'出现两次,'r'和't'都只出现一次。
因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。
示例 2:
输入:
"cccaaa"
输出:
"cccaaa"
解释:
'c'和'a'都出现三次。此外,"aaaccc"也是有效的答案。
注意"cacaca"是不正确的,因为相同的字母必须放在一起。
示例 3:
输入:
"Aabb"
输出:
"bbAa"
解释:
此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。
注意'A'和'a'被认为是两种不同的字符。
提示:
-
-
s
由大小写英文字母和数字组成
数据结构 + 模拟
这是一道考察数据结构运用的模拟题。
具体做法如下:
-
先使用「哈希表」对词频进行统计; -
遍历统计好词频的哈希表,将每个键值对以 {字符,词频}
的形式存储到「优先队列(堆)」中。并规定「优先队列(堆)」排序逻辑为:-
如果 词频
不同,则按照词频
倒序; -
如果 词频
相同,则根据字符字典序
升序(由于本题采用 Special Judge 机制,这个排序策略随意调整也可以。但通常为了确保排序逻辑满足「全序关系」,这个地方可以写正写反,但理论上不能不写,否则不能确保每次排序结果相同);
-
-
从「优先队列(堆)」依次弹出,构造答案。
Java 代码:
class Solution {
public String frequencySort(String s) {
char[] cs = s.toCharArray();
Map<Character, Integer> map = new HashMap<>();
for (char c : cs) map.put(c, map.getOrDefault(c, 0) + 1);
PriorityQueue<int[]> q = new PriorityQueue<>((a,b)->{
return a[1] != b[1] ? b[1] - a[1] : a[0] - b[0];
});
for (char c : map.keySet()) q.add(new int[]{c, map.get(c)});
StringBuilder sb = new StringBuilder();
while (!q.isEmpty()) {
int[] poll = q.poll();
int c = poll[0], k = poll[1];
while (k-- > 0) sb.append((char)(c));
}
return sb.toString();
}
}
C++ 代码:
class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> freq;
for (char c : s) freq[c]++;
auto cmp = [](const pair<char, int>& a, const pair<char, int>& b) {
return a.second == b.second ? a.first > b.first : a.second < b.second;
};
priority_queue<pair<char, int>, vector<pair<char, int>>, decltype(cmp)> q(cmp);
for (auto& entry : freq) q.push(entry);
string res;
while (!q.empty()) {
auto [c, k] = q.top();
q.pop();
res += string(k, c);
}
return res;
}
};
Python 代码:
class Solution:
def frequencySort(self, s: str) -> str:
freq = defaultdict(int)
for c in s:
freq[c] += 1
heap = [(-count, ord(c), c) for c, count in freq.items()]
heapq.heapify(heap)
res = []
while heap:
k, _, c = heapq.heappop(heap)
res.append(c * (-k))
return ''.join(res)
-
时间复杂度:令字符集的大小为 。使用「哈希表」统计词频的复杂度为 ;最坏情况下字符集中的所有字符都有出现,最多有 个节点要添加到「优先队列(堆)」中,复杂度为 ;构造答案需要从「优先队列(堆)」中取出元素并拼接,复杂度为 。整体复杂度为 -
空间复杂度:
数组实现 + 模拟
基本思路不变,将上述过程所用到的数据结构使用数组替代。
具体的,利用 ASCII 字符集共 位,预先建立一个大小为 的数组,利用「桶排序」的思路替代「哈希表」和「优先队列(堆)」的作用。
Java 代码:
class Solution {
public String frequencySort(String s) {
int[][] cnts = new int[128][2];
char[] cs = s.toCharArray();
for (int i = 0; i < 128; i++) cnts[i][0] = i;
for (char c : cs) cnts[c][1]++;
Arrays.sort(cnts, (a, b)->{
return a[1] != b[1] ? b[1] - a[1] : a[0] - b[0];
});
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 128; i++) {
char c = (char)cnts[i][0];
int k = cnts[i][1];
while (k-- > 0) sb.append(c);
}
return sb.toString();
}
}
C++ 代码:
class Solution {
public:
string frequencySort(string s) {
vector<pair<int, int>> cnts(128);
for (int i = 0; i < 128; i++) cnts[i].first = i;
for (char c : s) cnts[c].second++;
sort(cnts.begin(), cnts.end(), [](const auto& a, const auto& b) {
return a.second != b.second ? a.second > b.second : a.first < b.first;
});
string res;
for (const auto& p : cnts) {
if (p.second == 0) continue;
res.append(p.second, (char)p.first);
}
return res;
}
};
Python 代码:
class Solution:
def frequencySort(self, s: str) -> str:
freq = defaultdict(int)
for c in s:
freq[c] += 1
sorted_chars = sorted(freq.keys(), key=lambda c: (-freq[c], ord(c)))
return ''.join([c * freq[c] for c in sorted_chars])
-
时间复杂度:令字符集的大小为 ,复杂度为 -
空间复杂度:
最后
巨划算的 LeetCode 会员优惠通道目前仍可用 ~
使用福利优惠通道 leetcode.cn/premium/?promoChannel=acoier,年度会员 有效期额外增加两个月,季度会员 有效期额外增加两周,更有超大额专属 🧧 和实物 🎁 福利每月发放。
我是宫水三叶,每天都会分享算法知识,并和大家聊聊近期的所见所闻。
欢迎关注,明天见。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地 🎉🎉
本文由 mdnice 多平台发布