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

推荐订阅源

GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
F
Full Disclosure
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
NISL@THU
NISL@THU
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
Latest news
Latest news
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
C
Cybersecurity and Infrastructure Security Agency CISA
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
T
Tor Project blog
A
About on SuperTechFans
博客园 - 司徒正美
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LINUX DO - 热门话题
Know Your Adversary
Know Your Adversary
T
Tenable Blog
K
Kaspersky official blog
Simon Willison's Weblog
Simon Willison's Weblog
宝玉的分享
宝玉的分享
有赞技术团队
有赞技术团队
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
T
The Blog of Author Tim Ferriss
T
Threatpost
D
DataBreaches.Net
Engineering at Meta
Engineering at Meta
P
Palo Alto Networks Blog

博客园 - 司徒正美

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