LeetCode -- 第 371 场周赛(哈希表,tire字典树)

发布时间 2023-11-12 17:47:02作者: 深渊之巅

 

class Solution {
public:
    vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
        int n = access_times.size();
        vector<string> res;
        
        unordered_map<string, vector<string>> mp;
        
        for(int i = 0; i < n; i ++ ) {
            auto &it = access_times[i];
            mp[it[0]].push_back(it[1]);
        }
        
        auto func = [&](string &str) -> int {
            int h = stoi(str.substr(0, 2)), m = stoi(str.substr(2, 2));
            return h * 60 + m;
        };
        
        for(auto &it: mp) {
            auto &arr = it.second;
            sort(arr.begin(), arr.end());
            
            if(arr.size() < 3) continue;
            for(int i = 0; i < arr.size() - 2; i ++ ) {
                if(func(arr[i + 1]) - func(arr[i]) >= 60) {
                    continue;
                }
                
                if(func(arr[i + 2]) - func(arr[i]) < 60) {
                    res.push_back(it.first);
                    break;
                }   
            }
            
            for(int i = 0; i < arr.size(); i ++ ) {
                cout << func(arr[i]) << endl;
            }
            
        }
        
        return res;
        
    }
};

 

 

 

class Solution {
public:
    int minOperations(vector<int>& nums1, vector<int>& nums2) {
        int n = nums1.size();
        
        int ans = 0, ans1 = 1;
        if(n == 1) return 0;
        int mx1 = *max_element(nums1.begin(), nums1.end()), mx2 = *max_element(nums2.begin(), nums2.end());
        
        int mxt = max(nums1[n - 1], nums2[n - 1]);
        for(int i = 0; i < n - 1; i ++ ) {
            if(nums1[i] > nums1[n - 1] && nums2[i] > nums1[n - 1]) return -1;
            if(nums1[i] > nums2[n - 1] && nums2[i] > nums2[n - 1]) return -1;
        }
        if(mx1 > mxt || mx2 > mxt) return -1;
        
        for(int i = 0; i < n - 1; i ++ ) {
            if(nums1[i] > nums1[n - 1] || nums2[i] > nums2[n - 1]) ans ++ ;
        }
        
        swap(nums1[n - 1], nums2[n - 1]);
        
        for(int i = 0; i < n - 1; i ++ ) {
            if(nums1[i] > nums1[n - 1] || nums2[i] > nums2[n - 1]) ans1 ++ ;
        }

        
        return min(ans, ans1);
    }
};

 

 

  当我们要算的东西和数组的顺序没关系的时候,可以对数组排个序再进行思考

  |x - y| <= min(x, y) 当我们排序后 x < y 有 y - x <= x   2 * x >= y

  我们要求的问题:  1、最大异或和

          2、2 * x >= y这个条件,当我们枚举y的时候,x也一定是单调增加 -> 滑动窗口

 

方法1: 哈希表:

class Solution:
    def maximumStrongPairXor(self, nums: List[int]) -> int:
        nums.sort()
        ans = mask = 0
        high_bit = nums[-1].bit_length() - 1
        for i in range(high_bit, -1, -1):  # 从最高位开始枚举
            mask |= 1 << i
            new_ans = ans | (1 << i)  # 这个比特位可以是 1 吗?
            d = {}
            for y in nums:
                mask_y = y & mask  # 低于 i 的比特位置为 0
                if new_ans ^ mask_y in d and d[new_ans ^ mask_y] * 2 >= y:
                    ans = new_ans  # 这个比特位可以是 1
                    break
                d[mask_y] = y
        return ans

 

方法二: Tire

class Node:
    __slots__ = ('children', 'cnt')

    def __init__(self):
        self.children = [None, None]
        self.cnt = 0 # 子树大小, 伪删除

class Tire:
    HIGH_BIT = 19
    def __init__(self):
        self.root = Node()

    def insert(self, val: int) -> None:
        cur = self.root
        for i in range(Tire.HIGH_BIT, -1, -1):
            bit = (val >> i) & 1
            if cur.children[bit] is None:
                cur.children[bit] = Node()
            cur = cur.children[bit]
            cur.cnt += 1

    def remove(self, val: int) -> None:
        cur = self.root
        for i in range(Tire.HIGH_BIT, -1, -1):
            cur = cur.children[(val >> i) & 1]
            cur.cnt -= 1
        

    def max_xor(self, val: int) -> int:
        cur = self.root
        ans = 0
        for i in range(Tire.HIGH_BIT, -1, -1):
            bit = (val >> i) & 1
            # 去对面那个节点找
            if cur.children[bit ^ 1] and cur.children[bit ^ 1].cnt:
                ans |= 1 << i
                bit ^= 1 # 如果找到了就去对面
            cur = cur.children[bit]
        return ans
        

class Solution:
    def maximumStrongPairXor(self, nums: List[int]) -> int:
        nums.sort()
        t = Tire()

        ans = left = 0
        for y in nums:
            t.insert(y)
            while nums[left] * 2 < y:
                t.remove(nums[left])
                left += 1
            ans = max(ans, t.max_xor(y))

        return ans

 

更详细的讲解: b站up--灵茶山艾府