Ruby's Louvre

每天学习一点点算法

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

leetcode 1093. Statistics from a Large Sample

求一个数组的最小值,最大值,平均数,中位数与众数

function sampleStats(count) {
            let ret = [], i, j
            //众数
            let most = -1;
            //中位数
            let medium = -1;
            //最小值
            let minimum = Infinity;
            //最大值
            let maximum = -Infinity;
            //平均值
            let average = -1;
            //出现次数最多的数出现了多少次
            let maxsum = -Infinity;
            //用于统计序列中数的个数
            let cnt = 0;
            //序列和 用于求平均值
            let sum = 0;
            for (i = 0; i < 256; i++) {
                if (count[i] > 0) {
                    //找最大
                    if (i > maximum) {
                        maximum = i;
                    }
                    //找最小
                    if (i < minimum) {
                        minimum = i;
                    }
                    //为了求平均
                    sum += i * count[i];
                    cnt += count[i];
                    //找众数
                    if (count[i] > maxsum) {
                        most = i;
                        maxsum = count[i];
                    }
                }
            }

            ret.push(minimum);
            ret.push(maximum);
            //求平均值
            average = sum / cnt;
            ret.push(average);
            //找中位数
            let nextcnt = 0;
            if (cnt % 2 == 1) {
                //奇数
                for (i = 0; i < 256; i++) {
                    if (count[i] > 0) {
                        if (nextcnt <= cnt / 2 && nextcnt + count[i] > cnt / 2) {
                            medium = i;
                            break;
                        }
                        nextcnt += count[i];
                    }
                }
            } else {
                //偶数
                let pre = -1;
                for (i = 0; i < 256; i++) {
                    if (count[i] > 0) {
                        if (nextcnt < cnt / 2 && nextcnt + count[i] > cnt / 2) {
                            medium = i;
                            break;
                        }
                        else if (nextcnt == cnt / 2 && nextcnt + count[i] > cnt / 2) {
                            medium = (i + pre) / 2.0;
                            break;
                        }
                        nextcnt += count[i];
                        pre = i;
                    }
                }
            }
            ret.push(medium);
            ret.push(most);

            return ret;
        }

更简洁的实现

const sampleStats = function(count) {
  let min = Infinity;
  let max = -Infinity;
  let sum = 0;
  let totalCount = 0;
  let modCount = 0;
  let mod;
  
  for (let i = 0; i < count.length; i++) {
    if (!count[i]) continue;
    sum += i * count[i];
    totalCount += count[i];
    min = Math.min(min, i);
    max = Math.max(max, i);
    if (count[i] > modCount) {
      modCount = count[i];
      mod = i;
    }
  }
  
  const medIndex1 = Math.floor((totalCount - 1) / 2); 
  const medIndex2 = Math.ceil((totalCount - 1) / 2);
  
  let index = 0;
  let med1, med2;
  for (let i = 0; i < count.length; i++) {
    if (!count[i]) continue;
    index += count[i];
    if (med1 === undefined && medIndex1 < index) {
      med1 = i;
    }
    if (medIndex2 < index) {
      med2 = i;
      break;
    }
  }
  
  return [min, max, sum / totalCount, (med1 + med2) / 2, mod];
};

如果您觉得此文有帮助,可以打赏点钱给我支付宝1669866773@qq.com ,或扫描二维码

posted on   司徒正美  阅读(1259)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2011-01-05 iframe高度自适应
2010-01-05 javascript bind函数
点击右上角即可分享
微信分享提示