File tree 2 files changed +40
-0
lines changed 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 510
510
674|[ Longest Continuous Increasing Subsequence] ( ./0674-longest-continuous-increasing-subsequence.js ) |Easy|
511
511
675|[ Cut Off Trees for Golf Event] ( ./0675-cut-off-trees-for-golf-event.js ) |Hard|
512
512
676|[ Implement Magic Dictionary] ( ./0676-implement-magic-dictionary.js ) |Medium|
513
+ 677|[ Map Sum Pairs] ( ./0677-map-sum-pairs.js ) |Medium|
513
514
680|[ Valid Palindrome II] ( ./0680-valid-palindrome-ii.js ) |Easy|
514
515
684|[ Redundant Connection] ( ./0684-redundant-connection.js ) |Medium|
515
516
686|[ Repeated String Match] ( ./0686-repeated-string-match.js ) |Easy|
You can’t perform that action at this time.
0 commit comments