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

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

博客园 - 司徒正美

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 1093. Statistics from a Large Sample 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 676. Implement Magic Dictionary leetcode 648. Replace Words
leetcode 677. Map Sum Pairs
司徒正美 · 2019-12-28 · via 博客园 - 司徒正美

使用前缀树

 function Node() {
      this.value = 0
      this.children = {}
    }
    class MapSum {
      constructor() {
        this.root = new Node()
      }
      insert(word, val) {
        var node = this.root;
        for (let next of word) {
          if (!node.children[next]) {
            node.children[next] = new Node()
          }
          node = node.children[next]
          node.value += val
        }
        node.value += val
        node.isEnd = true
      }
      sum(word) {
        var node = this.root;
        for (let next of word) {
          node = node.children[next]
        }
        return node && node.value
      }
    }

    var tire = new MapSum()
    tire.insert("apple", 3)
    console.log(tire.sum("ap"))
    tire.insert("app", 2);
    console.log(tire.sum("ap"))

或者 使用map优化一下

    var MapSum = function () { // Space: O(n)
      this.map = new Map();
      this.root = new TrieNode();
    };

    /** 
     * @param {string} key 
     * @param {number} val
     * @return {void}
     */
    MapSum.prototype.insert = function (key, val) { // Time: O(k), k = leng(key)
      const delta = val - (this.map.get(key) || 0);
      this.map.set(key, val);
      let node = this.root;
      node.score += delta;
      for (const char of key) {
        if (!node.children.has(char)) {
          node.children.set(char, new TrieNode());
        }
        node = node.children.get(char);
        node.score += delta;
      }
    };

    /** 
     * @param {string} prefix
     * @return {number}
     */
    MapSum.prototype.sum = function (prefix) { // Time: O(k), k = len(prefix)
      let node = this.root;
      for (const char of prefix) {
        node = node.children.get(char);
        if (!node) return 0;
      }
      return node.score;
    };

    function TrieNode() {
      this.children = new Map();
      this.score = 0;
    }