Skip to content

Commit

Permalink
[Design] Add a solution to Flatten Nested List Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Dec 27, 2019
1 parent 40054ec commit 9dcedb2
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
4 changes: 2 additions & 2 deletions DP/NestedListWeightSum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Primary idea: Track depth and caculate value at each level
* Time Complexity: O(n), Space Complexity: O(1)
*
* This is the interface that allows for creating nested lists.
* You should not implement it, or speculate about its implementation
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* public func isInteger() -> Bool
Expand Down
8 changes: 4 additions & 4 deletions DP/NestedListWeightSumII.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Question Link: https://leetcode.com/problems/nested-list-weight-sum-ii/
* Primary idea: Use a helper function to track depth
* Time Complexity: O(n), Space Complexity: O(1)
* Primary idea: Track depth for every number and max depth overall
* Time Complexity: O(n), Space Complexity: O(n)
*
* This is the interface that allows for creating nested lists.
* You should not implement it, or speculate about its implementation
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* public func isInteger() -> Bool
Expand Down
95 changes: 95 additions & 0 deletions Design/FlattenNestedListIterator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Question Link: https://leetcode.com/problems/flatten-nested-list-iterator/
* Primary idea I: Use a stack to store nested list reversely.
* Every time pop a element and push back until it is an integer.
* Time Complexity I: next - O(1), hasNext - O(n), Space Complexity I: O(n)
*
* Primary idea II: Flatten the nested list at very first.
* Time Complexity II: next - O(1), hasNext - O(1), Space Complexity II: O(n)
*
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* public func isInteger() -> Bool
*
* // Return the single integer that this NestedInteger holds, if it holds a single integer
* // The result is undefined if this NestedInteger holds a nested list
* public func getInteger() -> Int
*
* // Set this NestedInteger to hold a single integer.
* public func setInteger(value: Int)
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public func add(elem: NestedInteger)
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* public func getList() -> [NestedInteger]
* }
*/

// Option I
class NestedIteratorI {

private var stack: [NestedInteger]

init(_ nestedList: [NestedInteger]) {
stack = nestedList.reversed()
}

func next() -> Int {
return stack.removeLast().getInteger()
}

func hasNext() -> Bool {
while !stack.isEmpty {
var nestedInteger = stack.removeLast()

if nestedInteger.isInteger() {
stack.append(nestedInteger)
return true
} else {
for nestedInt in nestedInteger.getList().reversed() {
stack.append(nestedInt)
}
}
}

return false
}
}

// Option II
class NestedIteratorII {

private var list: [Int]
private var index: Int

init(_ nestedList: [NestedInteger]) {
list = [Int]()
index = 0

flatten(nestedList)
}

func next() -> Int {
let current = list[index]
index += 1
return current
}

func hasNext() -> Bool {
return index < list.count
}

private func flatten(_ nestedList: [NestedInteger]) {
for nestedInt in nestedList {
if nestedInt.isInteger() {
list.append(nestedInt.getInteger())
} else {
flatten(nestedInt.getList())
}
}
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 307 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 308 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -388,6 +388,7 @@
| ----- | -------- | ---------- | ---- | ----- |
[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)| [Swift](./Design/ShuffleAnArray.swift)| Easy| O(n)| O(1)|
[Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/)| [Swift](./Design/DesignTicTacToe.swift)| Medium| O(1)| O(n)|
[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator)| [Swift](./Design/FlattenNestedListIterator.swift)| Medium| O(n)| O(n)|
[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)| [Swift](./Design/Vector2D.swift)| Medium | O(n)| O(n)|
[Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)| [Swift](./Design/ImplementTrie.swift)| Medium | O(n)| O(n)|
[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)| [Swift](./Design/AddSearchWord.swift)| Medium | O(24^n)| O(n)|
Expand Down Expand Up @@ -562,7 +563,7 @@
| [Swift](./String/ReverseString.swift) | 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | Easy
| [Swift](./Math/IntegerBreak.swift) | 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | Medium
| | 342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | Easy
| | 341 | [Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/) | Medium
| [Swift](./Design/FlattenNestedListIterator.swift) | 341 | [Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/) | Medium
| [Swift](./String/LongestSubstringMostKDistinctCharacters.swift) | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | Hard
| [Swift](./DP/NestedListWeightSum.swift) | 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) &hearts; | Easy
| [Swift](./Math/CountingBits.swift) | 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | Medium
Expand Down

0 comments on commit 9dcedb2

Please sign in to comment.