LeetCode · Daily Question · 2488. Statistical Median K Subarray · Hash + Prefix Sum

Author: Xiao Xun

Link: https://leetcode.cn/problems/count-subarrays-with-median-k/solutions/2172155/ha-xi-qian-zhui-he-zhu-shi-chao-ji-xiang-hgpu/

Source: LeetCode

Copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

topic

example

train of thought

Title -> Given an array, return the number of subarrays whose median is k

  • The median is equal to the sub-array of k, then the sub-array that satisfies the condition must be counted around the position of k. Since the topic knows that the number greater than k is one or equal to the number less than k, for the convenience of counting, the contribution is greater than 1, less than contribution −1, and the contribution itself is 0.

  • In this way, the prefix sum is counted, and the k bit starts as the end bit. Considering the count of the start bit on the left, the sum is equal to the current value and the current value minus one, that is, the sum of the interval after k is 0,1. .

the code


typedef struct {//哈希结构
    int key;
    int val;
    UT_hash_handle hh;
} HashItem; 

HashItem *hashFindItem(HashItem **obj, int key) {//查询key
    HashItem *pEntry = NULL;
    HASH_FIND_INT(*obj, &key, pEntry);
    return pEntry;
}

bool hashAddItem(HashItem **obj, int key, int val) {//添加哈希结构
    if (hashFindItem(obj, key)) {
        return false;
    }
    HashItem *pEntry = (HashItem *)malloc(sizeof(HashItem));
    pEntry->key = key;
    pEntry->val = val;
    HASH_ADD_INT(*obj, key, pEntry);
    return true;
}

bool hashSetItem(HashItem **obj, int key, int val) {//设置哈希结构
    HashItem *pEntry = hashFindItem(obj, key);
    if (!pEntry) {
        hashAddItem(obj, key, val);
    } else {
        pEntry->val = val;
    }
    return true;
}

int hashGetItem(HashItem **obj, int key, int defaultVal) {//获取哈希结构
    HashItem *pEntry = hashFindItem(obj, key);
    if (!pEntry) {
        return defaultVal;
    }
    return pEntry->val;
}

void hashFree(HashItem **obj) {//销毁哈希表
    HashItem *curr = NULL, *tmp = NULL;
    HASH_ITER(hh, *obj, curr, tmp) {
        HASH_DEL(*obj, curr);  
        free(curr);             
    }
}

static inline int sign(int num) {//取大小贡献
    if (num == 0) {
        return 0;
    }
    return num > 0 ? 1 : -1;
}

int countSubarrays(int* nums, int numsSize, int k) {
    int kIndex = -1;
    for (int i = 0; i < numsSize; i++) {//找k位置
        if (nums[i] == k) {
            kIndex = i;
            break;
        }
    }
    int ans = 0;
    HashItem *counts = NULL;
    hashAddItem(&counts, 0, 1);//入哈希表
    int sum = 0;
    for (int i = 0; i < numsSize; i++) {//遍历
        sum += sign(nums[i] - k);
        if (i < kIndex) {//小于k的位置
            hashSetItem(&counts, sum, hashGetItem(&counts, sum, 0) + 1);
        } else {//大于k的位置
            int prev0 = hashGetItem(&counts, sum, 0);
            int prev1 = hashGetItem(&counts, sum - 1, 0);//取贡献值
            ans += prev0 + prev1;
        }
    }
    hashFree(&counts);
    return ans;
}



作者:小迅
链接:https://leetcode.cn/problems/count-subarrays-with-median-k/solutions/2172155/ha-xi-qian-zhui-he-zhu-shi-chao-ji-xiang-hgpu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin blog.csdn.net/m0_64560763/article/details/129583342