Skip to content

677. Map Sum Pairs #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ continually updating 😃.
* [307. Range Sum Query - Mutable](src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)   *`segment tree`*
* [404. Sum of Left Leaves](src/0404_sum_of_left_leaves/sum_of_left_leaves.go)   *`binary tree`*
* [437. Path Sum III](src/0437_path_sum_3/path_sum_3.go)   *`binary tree`*
* [677. Map Sum Pairs](src/0677_map_sum_pairs/map_sum_pairs.go)   *`trie`*
* [872. Leaf-Similar Trees](src/0872_leaf_similar_trees/leaf_similar_trees.go)   *`binary tree`*

### Binary Search
Expand Down
7 changes: 3 additions & 4 deletions src/0208_implement_trie_prefix_tree/impltrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (trie *Trie) Insert(word string) {
if _, ok := cur.next[word[i]]; !ok {
cur.next[word[i]] = &node{next: make(map[byte]*node)}
}
cur, _ = cur.next[word[i]]
cur = cur.next[word[i]]
}
if !cur.isWord {
cur.isWord = true
Expand All @@ -52,9 +52,8 @@ func (trie *Trie) Search(word string) bool {
if _, ok := cur.next[word[i]]; !ok {
return false
}
cur, _ = cur.next[word[i]]
cur = cur.next[word[i]]
}

return cur.isWord
}

Expand All @@ -66,7 +65,7 @@ func (trie *Trie) StartsWith(prefix string) bool {
if _, ok := cur.next[prefix[i]]; !ok {
return false
}
cur, _ = cur.next[prefix[i]]
cur = cur.next[prefix[i]]
}
return true
}
63 changes: 63 additions & 0 deletions src/0677_map_sum_pairs/map_sum_pairs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
677. Map Sum Pairs
https://leetcode.com/problems/map-sum-pairs/

Implement a MapSum class with insert, and sum methods.

For the method insert, you'll be given a pair of (string, integer).
The string represents the key and the integer represents the value.
If the key already existed, then the original key-value pair will be overridden to the new one.

For the method sum, you'll be given a string representing the prefix,
and you need to return the sum of all the pairs' value whose key starts with the prefix.
*/
// time: 2019-02-01

package mapsumpairs

type node struct {
val int
next map[rune]*node
}

// MapSum data structure for solution.
type MapSum struct {
root *node
}

// Constructor initialize data structure here.
func Constructor() MapSum {
return MapSum{&node{next: make(map[rune]*node)}}
}

// Insert inserts a word into the trie.
func (ms *MapSum) Insert(key string, val int) {
cur := ms.root
for _, c := range key {
if _, ok := cur.next[c]; !ok {
cur.next[c] = &node{next: make(map[rune]*node)}
}
cur = cur.next[c]
}
cur.val = val
}

// Sum sum of all the pairs' value whose key starts with the prefix.
func (ms *MapSum) Sum(prefix string) int {
cur := ms.root
for _, c := range prefix {
if _, ok := cur.next[c]; !ok {
return 0
}
cur = cur.next[c]
}
return sum(cur)
}

func sum(n *node) int {
res := n.val
for _, nextNode := range n.next {
res += sum(nextNode)
}
return res
}
20 changes: 20 additions & 0 deletions src/0677_map_sum_pairs/map_sum_pairs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mapsumpairs

import "testing"

func TestMapSumPairs(t *testing.T) {
obj := Constructor()
obj.Insert("apple", 3)
if res := obj.Sum("ap"); res != 3 {
t.Errorf("expected %d, got %d", 3, res)
}

obj.Insert("app", 2)
if res := obj.Sum("ap"); res != 5 {
t.Errorf("expected %d, got %d", 5, res)
}

if res := obj.Sum("al"); res != 0 {
t.Errorf("expected %d, got %d", 0, res)
}
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
|0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*|
|0557|[557. Reverse Words in a String III](0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go)|Easy|*`string`*|
|0674|[674. Longest Continuous Increasing Subsequence](0674_longest_continuous_increasing_subsequence/lcis.go)|Easy||
|0677|[677. Map Sum Pairs](0677_map_sum_pairs/map_sum_pairs.go)|Medium|*`trie`*|
|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*|
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|
|0717|[717. 1-bit and 2-bit Characters](0717_1_bit_and_2_bit_characters/1bitand2bitc.go)|Easy||
Expand Down