-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# """ | ||
# This is the interface that allows for creating nested lists. | ||
# You should not implement it, or speculate about its implementation | ||
# """ | ||
#class NestedInteger(object): | ||
# def isInteger(self): | ||
# """ | ||
# @return True if this NestedInteger holds a single integer, rather than a nested list. | ||
# :rtype bool | ||
# """ | ||
# | ||
# def getInteger(self): | ||
# """ | ||
# @return the single integer that this NestedInteger holds, if it holds a single integer | ||
# Return None if this NestedInteger holds a nested list | ||
# :rtype int | ||
# """ | ||
# | ||
# def getList(self): | ||
# """ | ||
# @return the nested list that this NestedInteger holds, if it holds a nested list | ||
# Return None if this NestedInteger holds a single integer | ||
# :rtype List[NestedInteger] | ||
# """ | ||
|
||
class NestedIterator(object): | ||
|
||
def __init__(self, nestedList): | ||
""" | ||
Initialize your data structure here. | ||
:type nestedList: List[NestedInteger] | ||
""" | ||
if nestedList: | ||
self.stack = nestedList[::-1] | ||
else: | ||
self.stack = [] | ||
|
||
def next(self): | ||
""" | ||
:rtype: int | ||
""" | ||
return self.stack.pop() | ||
|
||
def hasNext(self): | ||
""" | ||
:rtype: bool | ||
""" | ||
if self.stack: | ||
top = self.stack.pop() | ||
while not top.isInteger(): | ||
self.stack += top.getList()[::-1] | ||
if self.stack: | ||
top = self.stack.pop() | ||
else: | ||
return False | ||
self.stack.append(top) | ||
return True | ||
else: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# """ | ||
# This is the interface that allows for creating nested lists. | ||
# You should not implement it, or speculate about its implementation | ||
# """ | ||
#class NestedInteger(object): | ||
# def __init__(self, value=None): | ||
# """ | ||
# If value is not specified, initializes an empty list. | ||
# Otherwise initializes a single integer equal to value. | ||
# """ | ||
# | ||
# def isInteger(self): | ||
# """ | ||
# @return True if this NestedInteger holds a single integer, rather than a nested list. | ||
# :rtype bool | ||
# """ | ||
# | ||
# def add(self, elem): | ||
# """ | ||
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it. | ||
# :rtype void | ||
# """ | ||
# | ||
# def setInteger(self, value): | ||
# """ | ||
# Set this NestedInteger to hold a single integer equal to value. | ||
# :rtype void | ||
# """ | ||
# | ||
# def getInteger(self): | ||
# """ | ||
# @return the single integer that this NestedInteger holds, if it holds a single integer | ||
# Return None if this NestedInteger holds a nested list | ||
# :rtype int | ||
# """ | ||
# | ||
# def getList(self): | ||
# """ | ||
# @return the nested list that this NestedInteger holds, if it holds a nested list | ||
# Return None if this NestedInteger holds a single integer | ||
# :rtype List[NestedInteger] | ||
# """ | ||
|
||
class Solution(object): | ||
def depthSumInverse(self, nestedList): | ||
""" | ||
:type nestedList: List[NestedInteger] | ||
:rtype: int | ||
""" | ||
record = [0 for _ in range(10000)] | ||
res = 0 | ||
self.max_weight = 0 | ||
|
||
def work(weight, l): | ||
self.max_weight = max(weight, self.max_weight) | ||
if l.isInteger(): | ||
record[weight] += l.getInteger() | ||
# print stack | ||
else: | ||
for item in l.getList(): | ||
work(weight + 1, item) | ||
|
||
for item in nestedList: | ||
work(1, item) | ||
|
||
for i in range(1, self.max_weight + 1): | ||
res += (self.max_weight + 1 - i) * record[i] | ||
|
||
return res |