te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>algorithm - Can I solve "Given an array of numbers, find number of subarrays that contain AT MOST k odd numbers&quo
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

algorithm - Can I solve "Given an array of numbers, find number of subarrays that contain AT MOST k odd numbers&quo

programmeradmin4浏览0评论

I solved a coding challenge "Given an array of numbers, find number of subarrays that contain EXACTLY k odd numbers" by prefix sum as below:

        int numberOfSubarrays(vector<int>& nums, int k) {
            int total = 0;
            int ans = 0; 
            std::vector<int> prefixSum{ 1 };
            for ( int num : nums ) {
                if ( num % 2 != 0 ) {
                    total += 1;
                    prefixSum.push_back( 1 );
                } else {
                    prefixSum[prefixSum.size() - 1] += 1;
                }
                if ( total - k < prefixSum.size() ) {
                    ans += prefixSum[total - k];
                }
            }
            return ans;
        }

I then get a follow-up variation of it: "Given an array of numbers, find number of subarrays that contain AT MOST k odd numbers", and the time complexity is required to be same, i.e., O(n), so naturally I turned to prefix sum again, but I found it hard to copy the same solution to this variation.

Could someone teach me if this "AT MOST" version is solvable by prefix sum, how?

N.B. I know there are some discussions on similar challenge but the subarrays are required to be distinct in terms of elements. The regulation doesn't apply here, as long as starting and ending indices are different, subarray is considered distinct, e.g., array[1:3] is different than array[2:4] no matter how their elements are same or not.

I solved a coding challenge "Given an array of numbers, find number of subarrays that contain EXACTLY k odd numbers" by prefix sum as below:

        int numberOfSubarrays(vector<int>& nums, int k) {
            int total = 0;
            int ans = 0; 
            std::vector<int> prefixSum{ 1 };
            for ( int num : nums ) {
                if ( num % 2 != 0 ) {
                    total += 1;
                    prefixSum.push_back( 1 );
                } else {
                    prefixSum[prefixSum.size() - 1] += 1;
                }
                if ( total - k < prefixSum.size() ) {
                    ans += prefixSum[total - k];
                }
            }
            return ans;
        }

I then get a follow-up variation of it: "Given an array of numbers, find number of subarrays that contain AT MOST k odd numbers", and the time complexity is required to be same, i.e., O(n), so naturally I turned to prefix sum again, but I found it hard to copy the same solution to this variation.

Could someone teach me if this "AT MOST" version is solvable by prefix sum, how?

N.B. I know there are some discussions on similar challenge but the subarrays are required to be distinct in terms of elements. The regulation doesn't apply here, as long as starting and ending indices are different, subarray is considered distinct, e.g., array[1:3] is different than array[2:4] no matter how their elements are same or not.

Share Improve this question asked Feb 17 at 12:09 LindaLinda 18712 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Not exactly prefix sum, but the idea is similar.

Your prefix sum solution effectively stores how many even numbers are there between each odd numbers. It does not apply to the revised "at most" scenario as it leads to overcounting.

Instead, you could store the index of each odd number, by which you get to count how many subarrays are there that ends with each number by a time complexity of O(n), as you wanted.

Taking nums = [8, 3, 6, 5, 2, 7], k = 2, as an example. For num[0], there is only one subarray [8] that ends with it, for sure it contains less than 1 odd number. For num[1], there are [8, 3], [3], etc. For nums[5], there are [6, 5, 2, 7], [5, 2, 7], [2, 7], [7].

Now hopefully you have caught the idea: For each number indexed at i, denoting the total odd numbers we have seen so far as total, and indices of each odd number as oddNumIndices, then:

  • If total <= k, there are exactly i - oddNumIndices[0] subarrays, where oddNumIndices[0] is initialized to -1 so as to avoiding special handing on boundary.

  • If total > k, there are exactly i - oddNumIndices[total - k] subarrays.

Also note that empty subarray [] is also valid for every k, so you need to add 1 to the final answer.

def myFunc( nums, k ):
    total = 0
    ans = 1 # [] contains odd numbers less than k for every k
    oddNumIndices = [-1]
    for i, num in enumerate( nums ):
        if num % 2 != 0:
            oddNumIndices.append( i )
            total += 1
        if total <= k:
            ans += i - oddNumIndices[0]
        else:
            ans += i - oddNumIndices[total - k]

    return ans

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论