Expedia OA

Complementary

Expedia OA_第1张图片

for word in words:
    # 字符串转换为integer便于位运算  int()
    bitmask = 0
    for c in word:
        bitmask ^= (1<<(ord(c) - ord('a'))
    # check bitmask 是否在hash_map(key: bitmask, value: count)中
    # 对于26位来说各异或一次,并判断是否在hash_map中
    # 如果在就加上对应count的数目到result中

每次有一样的bitmask先 res += dict.get(bitmask)然后再 dict.put(bitmask, get+1),然后再loop26次每次和a-z的字母异或,新bitmask如果在哈希表里就再 res+=dict.get(newbitmask)

for a in range(0, 26):
	# check if bitmask^a is in hashmap
if key in dic.keys()

Expedia OA_第2张图片

Binary Game


我们可以用一个一维int数组, 称它为dp. dp[j]表示当string长度是j的时候, 有多少个good string. 此时根据我们上面讨论的逻辑, dp[j] = dp[j - zero_group] + dp[j - one_group]. 但是有可能zero_group或者one_group大于此时的j, 这表示此时string中不能出现0或者1, 否则就会不满足题目要求, 遇到这种情况我们直接跳过. 还有就是如果zero_group或者one_group等于j时, 我们会需要dp[0]的值, 那么dp[0]这个base case的值应该是多少? 其实当zero_group或者one_group等于j时, 我们很容易就知道此时的good string就一种: 当zero_group等于j时, 那string全部填0; 当one_group等于j时, 那string全部填1, 当二者都等于j时, string要么全填0, 要么全填1. 于是为了让这种情况满足我们上面得出的dp状态转移方程, dp[0]需要等于1.

Array Generator

Expedia OA_第3张图片
注意保持diff严格递增,同时注意brr也是非递减的
所以 brr = max(brr[i-1], arr + diff + 1)
新的diff = brr - arr

Maximum Score


top k (min heap): https://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/

Bank Transaction

long bal = 0;
int counter = 0;
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for(int i = 0; i < transaction.size(); i++){
      bal += transaction.get(i);
      if(transaction.get(i) < 0){
          minHeap.add(transaction.get(i));
      }
      if(bal < 0){
           bal -= minHeap.poll();
            counter++;
       }
}
return (transaction.size() - counter);
Minimum Health

Expedia OA_第4张图片
设置数组size为k的最小堆,不停往里面加数,size超过k就pop,所以堆顶就一直是第k大的数,看堆顶peek就可以了。
minheap保留前k大:https://stackoverflow.com/questions/30443150/maintain-a-fixed-size-heap-python
quickselect:
https://www.geeksforgeeks.org/quickselect-algorithm/

Data Updates


优化就是,我们只需要标记left and right+1 boundary of update,到count数组,用一个公共变量cnt去计算需要改变符号的次数
也就是 (-1) 的cnt次方,再乘以data就可以

Even Tag

Expedia OA_第5张图片
one pass, 把所有正数加起来,并记录绝对值最小的奇数,
如果结果为even,直接返回
如果结果为odd,减去这个绝对值最小的奇数。

Efficient Teams

我用的解法是O(n),就是先算出每个team的target skill sum:一定是sum(skill) / (n/2),就是平均值的两倍,而且必须是整数。想到这点之后就很简单了,用一个hashmap统计一下skill,然后遍历hashmap,给每个key找同样数量的pair,找不到就返回-1。所以其实就是two sum
hashmap two sum:
https://leetcode.com/problems/two-sum/discuss/1378064/C%2B%2BJavaPython-HashMap-Two-pointers-Solutions-Clean-and-Concise

你可能感兴趣的:(算法)