惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - 司徒正美

leetcode 91. Decode Ways leetcode 1214 Two Sum BSTs leetcode 213 House Robber II leetcode 198 House Robber I leetcode 986. Interval List Intersections leetcode 869. Reordered Power of 2 leetcode 925. Long Pressed Name leetcode 457. Circular Array Loop leetcode 881. Boats to Save People leetcode 977. Squares of a Sorted Array leetcode 844. Backspace String Compare leetcode 1032. Stream of Characters leetcode 1023. Camelcase Matching leetcode 745 Prefix and Suffix Search leetcode 720. Longest Word in Dictionary leetcode 692. Top K Frequent Words leetcode 677. Map Sum Pairs leetcode 676. Implement Magic Dictionary leetcode 648. Replace Words
leetcode 1093. Statistics from a Large Sample
司徒正美 · 2020-01-04 · via 博客园 - 司徒正美

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

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];
};