1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold

Given an array of integers arr and two integers k and threshold.

Return the number of sub-arrays of size k and average greater than or equal to threshold.

Example 1:

Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
Output: 3
Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).

Example 2:

Input: arr = [1,1,1,1,1], k = 1, threshold = 0
Output: 5

Example 3:

Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5
Output: 6
Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.

Example 4:

Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7
Output: 1

Example 5:

Input: arr = [4,4,4,4], k = 4, threshold = 1
Output: 1

Constraints:

  • 1 <= arr.length <= 10^5
  • 1 <= arr[i] <= 10^4
  • 1 <= k <= arr.length
  • 0 <= threshold <= 10^4
class Solution {
    public int numOfSubarrays(int[] arr, int k, int threshold) {
        int res = 0;
        for(int i = 0; i <= arr.length - k; i++){
            if(helper(arr, i, k) >= threshold) res++;
        }
        return res;
    }
    public int helper(int[] arr, int l, int k){
        int res = 0;
        for(int i = 0; i < k; i++){
            res += arr[l + i];
        }
        return res / k;
    }
}
class Solution {
    public int numOfSubarrays(int[] arr, int k, int threshold) {
        int n = arr.length, ans = 0, s = 0;
        for(int i = 0; i < k - 1; i++) s += arr[i];
        for(int i = k - 1; i < n; i++) {
            s += arr[i];
            if(s / k >= threshold) ans++;
            s -= arr[i - k + 1];
        }
        return ans;
    }
}

下面的方法聪明点

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12302081.html

时间: 2024-11-03 17:25:21

1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold的相关文章

【leetcode】1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold

题目如下: Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold. Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Su

The kth great number multiset应用(找第k大值)

The kth great number Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feel

[LeetCode]Median of Two Sorted Arrays查找第k数(中位数)

二分.情况讨论 class Solution { public: int findPos(int* p,int n,int x){ int low=0,high=n-1,mid; while(low<=high){ mid=(low+high)>>1; if(p[mid]<=x)low=mid+1; else high=mid-1; } return low; } double findK(int a[], int m, int b[], int n,int k){ int mid

POJ 2104:K-th Number(主席树静态区间k大)

题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var t:array[0..100000*50]of point; a,b,id,root:array[0..100000]of longint; n,i,m,x,y,k,v,len:longint; procedure qsort(l,h:longint); var i,j,t,m:longint; b

Leetcode-996 Number of Squareful Arrays(正方形数组的数目)

1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 bool judge(int n) 6 { 7 if(n == (int)sqrt(n)*(int)sqrt(n)) 8 return true; 9 return false; 10 } 11 int numSquarefulPerms(vector<int>& A) 12 { 13 int sz = A.si

Oracle安装报错:SGA size can not be greater than maximum shared memeory segment size

安装数据的时候报错: 查看操作系统参数设置: [[email protected] ~]# vi /etc/sysctl.conf # Kernel sysctl configuration file for Red Hat Linux # # For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and # sysctl.conf(5) for more details. # Controls IP packet forwa

HashMap(2) 源码剖析(推荐)

今天看代码,想到去年发生的HashMap发生的CPU使用率100%的事件,转载下当时看的三个比较不错的博客(非常推荐) 参考:http://coolshell.cn/articles/9606.html   http://github.thinkingbar.com/hashmap-analysis/ http://developer.51cto.com/art/201102/246431.htm 在 Java 集合类中,使用最多的容器类恐怕就是 HashMap 和 ArrayList 了,所以

HashMap类源码解析

HashMap特点: 1.key-value键值对表示一个数据项 2.内部通过数组实现 3.没有实现同步方法,多线程线程不安全,效率较高 4. 所在包 package java.util; import java.io.*; 继承AbstractMap 实现Map.Cloneable.Serializable public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable

jdk7 HashMap阅读笔记

基于版本jdk1.7.0_80 java.util.HashMap 代码如下 /* * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package java.util;