Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2d0252b

Browse files
committed
Add solution #677
1 parent bde6d5a commit 2d0252b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

0677-map-sum-pairs.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 677. Map Sum Pairs
3+
* https://leetcode.com/problems/map-sum-pairs/
4+
* Difficulty: Medium
5+
*
6+
* Design a map that allows you to do the following:
7+
* - Maps a string key to a given value.
8+
* - Returns the sum of the values that have a key with a prefix equal to a given string.
9+
*
10+
* Implement the MapSum class:
11+
* - MapSum() Initializes the MapSum object.
12+
* - void insert(String key, int val) Inserts the key-val pair into the map. If the key
13+
* already existed, the original key-value pair will be overridden to the new one.
14+
* - int sum(string prefix) Returns the sum of all the pairs' value whose key starts
15+
* with the prefix.
16+
*/
17+
18+
var MapSum = function() {
19+
this.map = new Map();
20+
};
21+
22+
/**
23+
* @param {string} key
24+
* @param {number} val
25+
* @return {void}
26+
*/
27+
MapSum.prototype.insert = function(key, val) {
28+
this.map.set(key, val);
29+
};
30+
31+
/**
32+
* @param {string} prefix
33+
* @return {number}
34+
*/
35+
MapSum.prototype.sum = function(prefix) {
36+
return [...this.map.entries()].reduce((total, [key, val]) => {
37+
return key.startsWith(prefix) ? total + val : total;
38+
}, 0);
39+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@
510510
674|[Longest Continuous Increasing Subsequence](./0674-longest-continuous-increasing-subsequence.js)|Easy|
511511
675|[Cut Off Trees for Golf Event](./0675-cut-off-trees-for-golf-event.js)|Hard|
512512
676|[Implement Magic Dictionary](./0676-implement-magic-dictionary.js)|Medium|
513+
677|[Map Sum Pairs](./0677-map-sum-pairs.js)|Medium|
513514
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
514515
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
515516
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|

0 commit comments

Comments
 (0)