Skip to content

Commit f57519c

Browse files
committed
added nested list ii
1 parent 24c2020 commit f57519c

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This is my way of saying - change your interview style. There are lots of smart
2626
| [Sum 4 arrays](https://leetcode.com/problems/4sum-ii/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/4-sum.rb) |
2727
| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/reverse-linked-list.rb) |
2828
| [Reverse String In-place](https://leetcode.com/problems/reverse-string/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/reverse-string-inplace.rb) |
29+
| [Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/) | [click](https://github.com/sagivo/algorithms/blob/master/src/nested_list_weight_sum_ii.rb) |
2930
| [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [click](https://github.com/sagivo/algorithms/blob/master/src/mirror.rb) |
3031
| [Dijkstra's shortest path between two nodes](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/dijkstra.rb) |
3132
| [Kruskal's minimum spanning tree algorithm](http://en.wikipedia.org/wiki/Kruskal%27s_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/kruskal.rb) |

src/nested_list_weight_sum_ii.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This is the interface that allows for creating nested lists.
2+
# You should not implement it, or speculate about its implementation
3+
#
4+
#class NestedInteger
5+
# def is_integer()
6+
# """
7+
# Return true if this NestedInteger holds a single integer, rather than a nested list.
8+
# @return {Boolean}
9+
# """
10+
#
11+
# def get_integer()
12+
# """
13+
# Return the single integer that this NestedInteger holds, if it holds a single integer
14+
# Return nil if this NestedInteger holds a nested list
15+
# @return {Integer}
16+
# """
17+
#
18+
# def set_integer(value)
19+
# """
20+
# Set this NestedInteger to hold a single integer equal to value.
21+
# @return {Void}
22+
# """
23+
#
24+
# def add(elem)
25+
# """
26+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
27+
# @return {Void}
28+
# """
29+
#
30+
# def get_list()
31+
# """
32+
# Return the nested list that this NestedInteger holds, if it holds a nested list
33+
# Return nil if this NestedInteger holds a single integer
34+
# @return {NestedInteger[]}
35+
# """
36+
37+
# @param {NestedInteger[]} nested_list
38+
# @return {Integer}
39+
40+
def depth_sum_inverse(nested_list)
41+
h = {}
42+
lvl = 0
43+
44+
while nested_list.any?
45+
q = []
46+
lvl+=1
47+
h[lvl] = 0
48+
49+
nested_list.each do |n|
50+
if n.is_integer()
51+
h[lvl] += n.get_integer
52+
else
53+
q << n.get_list()
54+
end
55+
end
56+
57+
nested_list = q.flatten
58+
end
59+
60+
sum = 0
61+
62+
h.each do |k, v|
63+
sum += ((lvl-k+1) * v)
64+
end
65+
66+
sum
67+
end

src/phone.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def letters_for_number num
2626
def join_sets set, i = 0, str = '', result = []
2727
return result << str if str.size == set.size
2828
for item in set[i]
29-
join_sets set, i.succ, str + item.to_s, result
29+
join_sets set, i + 1, str + item.to_s, result
3030
end
3131
result
3232
end

0 commit comments

Comments
 (0)