[Swift]LeetCode432. 全O(1) 的数据结构 | All O`one Data Structure
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10801901.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Implement a data structure supporting the following operations:
- Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string.
- Dec(Key) - If Key's value is 1, remove it from the data structure. Otherwise decrements an existing key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a non-empty string.
- GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string
""
. - GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty string
""
.
Challenge: Perform all these in O(1) time complexity.
实现一个数据结构支持以下操作:
- Inc(key) - 插入一个新的值为 1 的 key。或者使一个存在的 key 增加一,保证 key 不为空字符串。
- Dec(key) - 如果这个 key 的值是 1,那么把他从数据结构中移除掉。否者使一个存在的 key 值减一。如果这个 key 不存在,这个函数不做任何事情。key 保证不为空字符串。
- GetMaxKey() - 返回 key 中值最大的任意一个。如果没有元素存在,返回一个空字符串
""
。 - GetMinKey() - 返回 key 中值最小的任意一个。如果没有元素存在,返回一个空字符串
""
。
挑战:以 O(1) 的时间复杂度实现所有操作。
Runtime: 192 ms
Memory Usage: 20.9 MB
1 class AllOne { 2 var obj:[String:Int] 3 /** Initialize your data structure here. */ 4 init() { 5 self.obj = [String:Int]() 6 } 7 8 /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ 9 func inc(_ key: String) { 10 if obj[key] != nil 11 { 12 obj[key,default:0] += 1 13 } 14 else 15 { 16 obj[key] = 1 17 } 18 19 } 20 21 /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ 22 func dec(_ key: String) { 23 if obj[key] != nil 24 { 25 obj[key,default:0] -= 1 26 } 27 28 if obj[key] == 0 29 { 30 obj[key] = nil 31 } 32 33 } 34 35 /** Returns one of the keys with maximal value. */ 36 func getMaxKey() -> String { 37 var val:Int = -1 38 var key:String = String() 39 40 for (keys,vals) in obj 41 { 42 if vals > val 43 { 44 val = vals 45 key = keys 46 } 47 } 48 return key 49 } 50 51 /** Returns one of the keys with Minimal value. */ 52 func getMinKey() -> String { 53 var val:Int = Int.max 54 var key:String = String() 55 56 for (keys,vals) in obj 57 { 58 if vals < val 59 { 60 val = vals 61 key = keys 62 } 63 } 64 return key 65 } 66 } 67 68 /** 69 * Your AllOne object will be instantiated and called as such: 70 * let obj = AllOne() 71 * obj.inc(key) 72 * obj.dec(key) 73 * let ret_3: String = obj.getMaxKey() 74 * let ret_4: String = obj.getMinKey() 75 */
192ms
1 class ListNode { 2 var val: Int 3 var key: String 4 var prev: ListNode? 5 var next: ListNode? 6 7 init(_ key: String, _ val: Int) { 8 self.key = key 9 self.val = val 10 self.prev = nil 11 self.next = nil 12 } 13 } 14 15 class AllOne { 16 var head: ListNode = ListNode("", Int.max) 17 var tail: ListNode = ListNode("", Int.min) 18 var map: [String: ListNode] = [:] 19 20 init() { 21 head.next = tail 22 tail.prev = head 23 } 24 25 func inc(_ key: String) { 26 if let node = map[key] { 27 node.val += 1 28 moveForward(node) 29 } 30 else { 31 let node = ListNode(key, 1) 32 map[key] = node 33 34 addNode(node) 35 } 36 } 37 38 func dec(_ key: String) { 39 if let node = map[key] { 40 if node.val == 1 { 41 removeNode(node) 42 map.removeValue(forKey: key) 43 } 44 else { 45 node.val -= 1 46 moveBackward(node) 47 } 48 } 49 } 50 51 func getMaxKey() -> String { 52 return head.next!.key 53 } 54 55 func getMinKey() -> String { 56 return tail.prev!.key 57 } 58 59 private func moveForward(_ node: ListNode) { 60 var insertNode = node.prev 61 62 var shouldMove = false 63 while insertNode != nil && insertNode!.val < node.val { 64 insertNode = insertNode?.prev 65 shouldMove = true 66 } 67 68 guard shouldMove else { 69 return 70 } 71 72 //remove node 73 node.prev?.next = node.next 74 node.next?.prev = node.prev 75 76 //insert 77 node.next = insertNode?.next 78 insertNode?.next?.prev = node 79 80 insertNode?.next = node 81 node.prev = insertNode 82 } 83 84 private func moveBackward(_ node: ListNode) { 85 var insertNode = node.next 86 87 var shouldMove = false 88 while insertNode != nil && insertNode!.val > node.val{ 89 insertNode = insertNode?.next 90 shouldMove = true 91 } 92 93 guard shouldMove else { 94 return 95 } 96 97 //remove node 98 node.prev?.next = node.next 99 node.next?.prev = node.prev 100 101 //insert 102 insertNode?.prev?.next = node 103 node.prev = insertNode?.prev 104 105 insertNode?.prev = node 106 node.next = insertNode 107 } 108 109 private func addNode(_ node: ListNode) { 110 let prevNode = tail.prev 111 112 prevNode?.next = node 113 node.prev = prevNode 114 115 node.next = tail 116 tail.prev = node 117 } 118 119 private func removeNode(_ node: ListNode) { 120 node.prev?.next = node.next 121 node.next?.prev = node.prev 122 123 node.next = nil 124 node.prev = nil 125 } 126 } 127 128 /** 129 * Your AllOne object will be instantiated and called as such: 130 * let obj = AllOne() 131 * obj.inc(key) 132 * obj.dec(key) 133 * let ret_3: String = obj.getMaxKey() 134 * let ret_4: String = obj.getMinKey() 135 */
212ms
1 class AllOne { 2 class Node { 3 var prev: Node? 4 var next: Node? 5 var value: Int 6 var keys: Set<String> 7 8 init(_ key: String, _ value: Int) { 9 self.prev = nil 10 self.next = nil 11 self.value = value 12 self.keys = Set<String>() 13 keys.insert(key) 14 } 15 } 16 17 var head: Node? 18 var tail: Node? 19 var map: [String : Node] 20 21 /** Initialize your data structure here. */ 22 init() { 23 self.head = nil 24 self.tail = nil 25 self.map = [:] 26 } 27 28 /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ 29 func inc(_ key: String) { 30 // modify the old node 31 if let old_home = map[key] { 32 let new_home: Node 33 if let n = old_home.next { 34 // upper neighbor exists and is new_home 35 if n.value == old_home.value + 1 { 36 new_home = n 37 new_home.keys.insert(key) 38 } else { 39 // upper neighbor exists but is not new_home, so create and make room 40 new_home = Node(key, old_home.value + 1) 41 new_home.next = n 42 new_home.prev = old_home 43 44 old_home.next = new_home 45 n.prev = new_home 46 } 47 } else { 48 // old_home was head, so update head and create node 49 new_home = Node(key, old_home.value + 1) 50 head?.next = new_home 51 new_home.prev = head 52 head = new_home 53 } 54 55 map[key] = new_home 56 old_home.keys.remove(key) 57 58 // If the old_home needs to be removed 59 if old_home.keys.count == 0 { 60 if let t = tail, old_home.value == t.value { 61 tail = tail?.next 62 tail?.prev = nil 63 } else { 64 old_home.prev?.next = old_home.next 65 old_home.next?.prev = old_home.prev 66 } 67 } 68 69 } else { 70 // Key not in map 71 if let old_tail = tail { 72 if old_tail.value == 1 { 73 old_tail.keys.insert(key) 74 map[key] = old_tail 75 } else { 76 let n = Node(key, 1) 77 old_tail.prev = n 78 n.next = old_tail 79 tail = n 80 map[key] = n 81 } 82 } else { 83 let n = Node(key, 1) 84 tail = n 85 head = n 86 map[key] = n 87 } 88 } 89 } 90 91 /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ 92 func dec(_ key: String) { 93 guard let old_home = map[key] else { 94 return 95 } 96 97 if old_home.value == 1 { 98 map.removeValue(forKey: key) 99 } else { 100 let new_home: Node 101 if let n = old_home.prev { 102 if n.value == old_home.value - 1 { 103 // left neighbor both exists and is new_home 104 new_home = n 105 new_home.keys.insert(key) 106 } else { 107 // left neighbor exists but is not new_home 108 new_home = Node(key, old_home.value - 1) 109 old_home.prev?.next = new_home 110 new_home.prev = old_home.prev 111 old_home.prev = new_home 112 new_home.next = old_home 113 } 114 } else { 115 // old_home is tail and not 1 116 new_home = Node(key, old_home.value - 1) 117 new_home.next = tail 118 tail?.prev = new_home 119 tail = new_home 120 } 121 map[key] = new_home 122 } 123 old_home.keys.remove(key) 124 125 // If the old_home needs to be removed 126 if old_home.keys.count == 0 { 127 if let t = tail, old_home.value == t.value { 128 tail = tail?.next 129 tail?.prev = nil 130 } else if let h = head, h.value == old_home.value { 131 head = head?.prev 132 head?.next = nil 133 } else { 134 old_home.prev?.next = old_home.next 135 old_home.next?.prev = old_home.prev 136 } 137 } 138 139 } 140 141 /** Returns one of the keys with maximal value. */ 142 func getMaxKey() -> String { 143 return head?.keys.first ?? "" 144 } 145 146 /** Returns one of the keys with Minimal value. */ 147 func getMinKey() -> String { 148 return tail?.keys.first ?? "" 149 150 } 151 }
216ms
1 class Node { 2 private(set) var val: Int 3 var keys = Set<String>() 4 var prev: Node? 5 var next: Node? 6 init(_ val: Int) { 7 self.val = val 8 } 9 } 10 class AllOne { 11 private var head: Node? 12 private var tail: Node? 13 private var keyToNodeMap = [String: Node]() 14 private var valToNodeMap = [Int: Node]() 15 /** Initialize your data structure here. */ 16 init() { 17 18 } 19 20 /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ 21 func inc(_ key: String) { 22 let node = keyToNodeMap[key] 23 let val = node?.val ?? 0 24 var newNode: Node 25 if let next = node?.next, 26 next.val == val + 1 { 27 newNode = next 28 } else { 29 newNode = valToNodeMap[val + 1] ?? Node(val + 1) 30 } 31 32 node?.keys.remove(key) 33 newNode.keys.insert(key) 34 keyToNodeMap[key] = newNode 35 valToNodeMap[val + 1] = newNode 36 37 if node == nil && newNode !== head { 38 add(newNode, before: head) 39 } else { 40 add(newNode, after: node) 41 } 42 if let n = node, n.keys.count == 0 { 43 remove(n) 44 } 45 } 46 47 /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ 48 func dec(_ key: String) { 49 guard let node = keyToNodeMap[key] else { return } 50 let val = node.val 51 var newNode: Node? 52 if let prev = node.prev, 53 prev.val == val - 1 { 54 newNode = prev 55 } else if val - 1 > 0 { 56 newNode = valToNodeMap[val + 1] ?? Node(val - 1) 57 } 58 59 node.keys.remove(key) 60 newNode?.keys.insert(key) 61 keyToNodeMap[key] = newNode 62 valToNodeMap[val - 1] = newNode 63 64 if let nn = newNode { 65 add(nn, before: node) 66 } 67 if node.keys.count == 0 { 68 remove(node) 69 } 70 } 71 72 private func remove(_ node: Node?) { 73 guard let node = node else { return } 74 node.prev?.next = node.next 75 node.next?.prev = node.prev 76 if head === node { 77 head = node.next 78 } 79 if tail === node { 80 tail = node.prev 81 } 82 } 83 84 private func add(_ node: Node, before nextNode: Node?) { 85 if nextNode?.prev !== node { 86 nextNode?.prev?.next = node 87 node.prev = nextNode?.prev 88 } 89 nextNode?.prev = node 90 node.next = nextNode 91 if head === nil { 92 head = node 93 tail = node 94 } 95 if head === nextNode { 96 head = node 97 } 98 } 99 100 private func add(_ node: Node, after prevNode: Node?) { 101 if prevNode?.next !== node { 102 prevNode?.next?.prev = node 103 node.next = prevNode?.next 104 } 105 prevNode?.next = node 106 node.prev = prevNode 107 if head === nil { 108 head = prevNode ?? node 109 tail = prevNode ?? node 110 } 111 if tail === prevNode { 112 tail = node 113 } 114 } 115 116 /** Returns one of the keys with maximal value. */ 117 func getMaxKey() -> String { 118 return tail?.keys.randomElement() ?? "" 119 } 120 121 /** Returns one of the keys with Minimal value. */ 122 func getMinKey() -> String { 123 return head?.keys.randomElement() ?? "" 124 } 125 } 126 127 /** 128 * Your AllOne object will be instantiated and called as such: 129 * let obj = AllOne() 130 * obj.inc(key) 131 * obj.dec(key) 132 * let ret_3: String = obj.getMaxKey() 133 * let ret_4: String = obj.getMinKey() 134 */
220ms
1 // each Bucket contains all the keys with the same count 2 class DoubleLinkedBucket { 3 var count: Int 4 var keySet: Set<String> 5 var prev: DoubleLinkedBucket? 6 var next: DoubleLinkedBucket? 7 8 init(_ count: Int) { 9 self.count = count 10 self.keySet = [] 11 } 12 } 13 14 class AllOne { 15 // for accessing a specific Bucket among the Bucket list in O(1) time 16 var countBucketMap: [Int: DoubleLinkedBucket] 17 // keep track of count of keys 18 var keyCountMap: [String: Int] 19 // maintain a doubly linked list of Buckets 20 var head: DoubleLinkedBucket 21 var tail: DoubleLinkedBucket 22 23 /** Initialize your data structure here. */ 24 init() { 25 self.countBucketMap = [:] 26 self.keyCountMap = [:] 27 self.head = DoubleLinkedBucket(Int.min) 28 self.tail = DoubleLinkedBucket(Int.max) 29 head.next = tail 30 tail.prev = head 31 } 32 33 /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ 34 func inc(_ key: String) { 35 if keyCountMap[key] != nil { 36 updateKey(key, 1); 37 } else { 38 keyCountMap[key] = 1 39 if head.next?.count != 1 { addBucketAfter(DoubleLinkedBucket(1), head) } 40 head.next?.keySet.insert(key) 41 countBucketMap[1] = head.next 42 } 43 } 44 45 /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ 46 func dec(_ key: String) { 47 if let count = keyCountMap[key] { 48 if count == 1 { 49 keyCountMap[key] = nil 50 removeKeyFromBucket(countBucketMap[count]!, key) 51 } else { 52 updateKey(key, -1); 53 } 54 } 55 } 56 57 /** Returns one of the keys with maximal value. */ 58 func getMaxKey() -> String { 59 return tail.prev! === head ? "" : tail.prev!.keySet.first! 60 } 61 62 /** Returns one of the keys with Minimal value. */ 63 func getMinKey() -> String { 64 return head.next! === tail ? "" : head.next!.keySet.first! 65 } 66 67 func updateKey(_ key: String, _ offset: Int) { 68 let curCount = keyCountMap[key]! 69 let newCount = curCount + offset 70 keyCountMap[key] = newCount 71 72 let curBucket = countBucketMap[curCount]! 73 74 if let newBucket = countBucketMap[newCount] { 75 newBucket.keySet.insert(key) 76 } else { 77 let newBucket = DoubleLinkedBucket(newCount) 78 countBucketMap[newCount] = newBucket 79 addBucketAfter(newBucket, offset == 1 ? curBucket : curBucket.prev!) 80 newBucket.keySet.insert(key) 81 } 82 removeKeyFromBucket(curBucket, key) 83 } 84 85 func removeKeyFromBucket(_ bucket: DoubleLinkedBucket, _ key: String) { 86 bucket.keySet.remove(key) 87 if (bucket.keySet.count == 0) { 88 removeBucketFromList(bucket) 89 countBucketMap[bucket.count] = nil 90 } 91 } 92 93 func removeBucketFromList(_ bucket: DoubleLinkedBucket) { 94 bucket.prev!.next = bucket.next 95 bucket.next!.prev = bucket.prev 96 bucket.next = nil 97 bucket.prev = nil 98 } 99 100 // add newBucket after preBucket 101 func addBucketAfter(_ newBucket: DoubleLinkedBucket, _ preBucket: DoubleLinkedBucket) { 102 newBucket.prev = preBucket 103 newBucket.next = preBucket.next 104 preBucket.next!.prev = newBucket 105 preBucket.next = newBucket 106 } 107 } 108 109 /** 110 * Your AllOne object will be instantiated and called as such: 111 * let obj = AllOne() 112 * obj.inc(key) 113 * obj.dec(key) 114 * let ret_3: String = obj.getMaxKey() 115 * let ret_4: String = obj.getMinKey() 116 */
240ms
1 class AllOne { 2 typealias Bucket = (Int, Set<String>) 3 var list = LinkedList<Bucket>() 4 var dict = [String: ListNode<Bucket>]() 5 6 // Inserts a new key <Key> with value 1. Or increments an existing key by 1. 7 func inc(_ key: String) { 8 if let node = dict[key] { 9 let count = node.val.0 + 1 10 if let pre = node.pre, pre.val.0 == count { 11 pre.val.1.insert(key) 12 dict[key] = pre 13 } else { 14 let newNode = ListNode((count, Set([key]))) 15 list.insert(newNode, before: node) 16 dict[key] = newNode 17 } 18 // update list if node doesn't contain any key 19 node.val.1.remove(key) 20 if node.val.1.count == 0 { 21 list.remove(node) 22 } 23 } else { 24 if let tail = list.tail, tail.val.0 == 1 { 25 tail.val.1.insert(key) 26 dict[key] = tail 27 } else { 28 let newNode = ListNode((1, Set([key]))) 29 list.addAfterTail(newNode) 30 dict[key] = newNode 31 } 32 } 33 } 34 35 // Decrements an existing key by 1. If Key's value is 1, remove it. 36 func dec(_ key: String) { 37 if let node = dict[key] { 38 let count = node.val.0 - 1 39 if count == 0 { 40 dict[key] = nil 41 } else { 42 if let next = node.next, next.val.0 == count { 43 next.val.1.insert(key) 44 dict[key] = next 45 } else { 46 let newNode = ListNode((count, Set([key]))) 47 list.insert(newNode, after: node) 48 dict[key] = newNode 49 } 50 } 51 52 // update list if node doesn't contain any key 53 node.val.1.remove(key) 54 if node.val.1.count == 0 { 55 list.remove(node) 56 } 57 } 58 } 59 60 // Returns one of the keys with maximal value. 61 func getMaxKey() -> String { 62 return list.head?.val.1.first ?? "" 63 } 64 65 // Returns one of the keys with maximal value. 66 func getMinKey() -> String { 67 return list.tail?.val.1.first ?? "" 68 } 69 } 70 71 class ListNode<T> { 72 var val: T 73 var next: ListNode<T>? 74 weak var pre: ListNode<T>? 75 76 init(_ val: T) { 77 self.val = val 78 } 79 } 80 81 class LinkedList<T> { 82 var head: ListNode<T>? 83 var tail: ListNode<T>? 84 85 func insert(_ node: ListNode<T>, before pre: ListNode<T>) { 86 pre.pre?.next = node 87 node.pre = pre.pre 88 pre.pre = node 89 node.next = pre 90 if pre === head { 91 head = node 92 } 93 } 94 95 func insert(_ node: ListNode<T>, after next: ListNode<T>) { 96 next.next?.pre = node 97 node.next = next.next 98 next.next = node 99 node.pre = next 100 if next === tail { 101 tail = node 102 } 103 } 104 105 func addAfterTail(_ node: ListNode<T>) { 106 if head == nil { 107 head = node 108 tail = node 109 } else { 110 tail?.next = node 111 node.pre = tail 112 tail = tail?.next 113 } 114 } 115 116 func remove(_ node: ListNode<T>) { 117 if node === head { 118 head = node.next 119 } 120 if node === tail { 121 tail = node.pre 122 } 123 node.pre?.next = node.next 124 node.next?.pre = node.pre 125 node.next = nil 126 node.pre = nil 127 } 128 } 129 130 /** 131 * Your AllOne object will be instantiated and called as such: 132 * let obj = AllOne() 133 * obj.inc(key) 134 * obj.dec(key) 135 * let ret_3: String = obj.getMaxKey() 136 * let ret_4: String = obj.getMinKey() 137 */ 138
252ms
1 class AllOne { 2 typealias Bucket = (Int, Set<String>) 3 var list = LinkedList<Bucket>() 4 var dict = [String: ListNode<Bucket>]() 5 6 // Inserts a new key <Key> with value 1. Or increments an existing key by 1. 7 func inc(_ key: String) { 8 if let node = dict[key] { 9 let count = node.val.0 + 1 10 if let pre = node.pre, pre.val.0 == count { 11 pre.val.1.insert(key) 12 // var set = pre.val.1 13 // set.insert(key) 14 // pre.val = (pre.val.0, set) 15 dict[key] = pre 16 } else { 17 let newNode = ListNode((count, Set([key]))) 18 list.insert(newNode, before: node) 19 dict[key] = newNode 20 } 21 // update list if node doesn't contain any key 22 var set = node.val.1 23 set.remove(key) 24 node.val = (node.val.0, set) 25 if set.count == 0 { 26 list.remove(node) 27 } 28 } else { 29 if let tail = list.tail, tail.val.0 == 1 { 30 tail.val.1.insert(key) 31 // var set = tail.val.1 32 // set.insert(key) 33 // tail.val = (tail.val.0, set) 34 dict[key] = tail 35 } else { 36 let newNode = ListNode((1, Set([key]))) 37 list.addAfterTail(newNode) 38 dict[key] = newNode 39 } 40 } 41 } 42 43 // Decrements an existing key by 1. If Key's value is 1, remove it. 44 func dec(_ key: String) { 45 if let node = dict[key] { 46 let count = node.val.0 - 1 47 if count == 0 { 48 dict[key] = nil 49 } else { 50 if let next = node.next, next.val.0 == count { 51 next.val.1.insert(key) 52 // var set = next.val.1 53 // set.insert(key) 54 // next.val = (next.val.0, set) 55 dict[key] = next 56 } else { 57 let newNode = ListNode((count, Set([key]))) 58 list.insert(newNode, after: node) 59 dict[key] = newNode 60 } 61 } 62 63 var set = node.val.1 64 set.remove(key) 65 node.val = (node.val.0, set) 66 if set.count == 0 { 67 list.remove(node) 68 } 69 } 70 } 71 72 // Returns one of the keys with maximal value. 73 func getMaxKey() -> String { 74 return list.head?.val.1.first ?? "" 75 } 76 77 // Returns one of the keys with maximal value. 78 func getMinKey() -> String { 79 return list.tail?.val.1.first ?? "" 80 } 81 } 82 83 class ListNode<T> { 84 var val: T 85 var next: ListNode<T>? 86 weak var pre: ListNode<T>? 87 88 init(_ val: T) { 89 self.val = val 90 } 91 } 92 93 class LinkedList<T> { 94 var head: ListNode<T>? 95 var tail: ListNode<T>? 96 97 func insert(_ node: ListNode<T>, before pre: ListNode<T>) { 98 pre.pre?.next = node 99 node.pre = pre.pre 100 pre.pre = node 101 node.next = pre 102 if pre === head { 103 head = node 104 } 105 } 106 107 func insert(_ node: ListNode<T>, after next: ListNode<T>) { 108 next.next?.pre = node 109 node.next = next.next 110 next.next = node 111 node.pre = next 112 if next === tail { 113 tail = node 114 } 115 } 116 117 func addAfterTail(_ node: ListNode<T>) { 118 if head == nil { 119 head = node 120 tail = node 121 } else { 122 tail?.next = node 123 node.pre = tail 124 tail = tail?.next 125 } 126 } 127 128 func remove(_ node: ListNode<T>) { 129 if node === head { 130 head = node.next 131 } 132 if node === tail { 133 tail = node.pre 134 } 135 node.pre?.next = node.next 136 node.next?.pre = node.pre 137 node.next = nil 138 node.pre = nil 139 } 140 } 141 142 /** 143 * Your AllOne object will be instantiated and called as such: 144 * let obj = AllOne() 145 * obj.inc(key) 146 * obj.dec(key) 147 * let ret_3: String = obj.getMaxKey() 148 * let ret_4: String = obj.getMinKey() 149 */ 150
256ms
1 class AllOne { 2 private var list = LinkedList<Bucket>() 3 private var dict = [String: ListNode<Bucket>]() 4 5 /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ 6 func inc(_ key: String) { 7 8 if let node = dict[key] { 9 10 // update dict and node 11 let newCount = node.val.count + 1 12 if let pre = node.pre, pre.val.count == newCount { 13 pre.val.keys.insert(key) 14 dict[key] = pre 15 } else { 16 // add new node 17 let newNode = ListNode(Bucket(newCount, Set([key]))) 18 list.insert(newNode, before: node) 19 dict[key] = newNode 20 } 21 22 // remove key from node 23 node.val.keys.remove(key) 24 if node.val.keys.count == 0 { 25 list.remove(node) 26 } 27 28 } else { 29 if let tail = list.tail, tail.val.count == 1 { 30 tail.val.keys.insert(key) 31 dict[key] = tail 32 } else { 33 let bucket = Bucket(1, Set([key])) 34 let node = ListNode(bucket) 35 list.addToTail(node) 36 dict[key] = node 37 } 38 } 39 } 40 41 /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ 42 func dec(_ key: String) { 43 44 if let node = dict[key] { 45 46 let newCount = node.val.count - 1 47 if newCount == 0 { 48 dict[key] = nil 49 } else { 50 if let next = node.next, next.val.count == newCount { 51 next.val.keys.insert(key) 52 dict[key] = next 53 } else { 54 // add new node 55 let newNode = ListNode(Bucket(newCount, Set([key]))) 56 list.insert(newNode, after: node) 57 dict[key] = newNode 58 } 59 } 60 61 // remove key from node 62 node.val.keys.remove(key) 63 if node.val.keys.count == 0 { 64 list.remove(node) 65 } 66 67 } 68 } 69 70 /** Returns one of the keys with maximal value. */ 71 func getMaxKey() -> String { 72 return list.head?.val.keys.first ?? "" 73 } 74 75 /** Returns one of the keys with Minimal value. */ 76 func getMinKey() -> String { 77 return list.tail?.val.keys.first ?? "" 78 } 79 } 80 81 class LinkedList<T> { 82 var head: ListNode<T>? 83 var tail: ListNode<T>? 84 85 func addToTail(_ node: ListNode<T>) { 86 if head == nil { 87 head = node 88 tail = node 89 } else { 90 tail?.next = node 91 node.pre = tail 92 tail = node 93 } 94 } 95 96 func remove(_ node: ListNode<T>) { 97 if head === node { 98 head = node.next 99 } 100 if tail === node { 101 tail = node.pre 102 } 103 node.pre?.next = node.next 104 node.next?.pre = node.pre 105 node.pre = nil 106 node.next = nil 107 } 108 109 func insert(_ node: ListNode<T>, before node2: ListNode<T>?) { 110 if head === node2 { 111 head = node 112 } 113 let pre = node2?.pre 114 node2?.pre = node 115 node.next = node2 116 pre?.next = node 117 node.pre = pre 118 } 119 120 func insert(_ node: ListNode<T>, after node2: ListNode<T>?) { 121 if node2 === tail { 122 tail = node 123 } 124 let next = node2?.next 125 node2?.next = node 126 node.pre = node2 127 node.next = next 128 next?.pre = node 129 } 130 } 131 132 class ListNode<T> { 133 var val: T 134 var next: ListNode<T>? 135 weak var pre: ListNode<T>? 136 137 init(_ val: T) { 138 self.val = val 139 } 140 } 141 142 class Bucket { 143 var count: Int 144 var keys: Set<String> 145 146 init(_ count: Int, _ keys: Set<String>) { 147 self.count = count 148 self.keys = keys 149 } 150 } 151 152 /** 153 * Your AllOne object will be instantiated and called as such: 154 * let obj = AllOne() 155 * obj.inc(key) 156 * obj.dec(key) 157 * let ret_3: String = obj.getMaxKey() 158 * let ret_4: String = obj.getMinKey() 159 */
260ms
1 class AllOne { 2 var head: Bucket 3 var tail: Bucket 4 var bucketMap: [Int: Bucket] 5 var countMap: [String: Int] 6 7 /** Initialize your data structure here. */ 8 init() { 9 self.head = Bucket() 10 self.tail = Bucket() 11 self.bucketMap = [Int: Bucket]() 12 self.countMap = [String: Int]() 13 self.head.next = self.tail 14 self.tail.prev = self.head 15 } 16 17 /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ 18 func inc(_ key: String) { 19 if let count = countMap[key] { 20 // if the key exists. 21 bucketMap[count]!.keys.remove(key) 22 if let nextBucket = bucketMap[count + 1] { 23 nextBucket.keys.insert(key) 24 } else { 25 let newBucket = Bucket() 26 newBucket.keys.insert(key) 27 // insert the new bucket 28 let prevBucket = bucketMap[count]! 29 newBucket.next = prevBucket.next 30 prevBucket.next = newBucket 31 newBucket.next!.prev = newBucket 32 newBucket.prev = prevBucket 33 bucketMap[count + 1] = newBucket 34 } 35 if bucketMap[count]!.keys.count == 0 { 36 remove(bucketMap[count]!) 37 bucketMap[count] = nil 38 } 39 } else { 40 if let firstBucket = bucketMap[1] { 41 firstBucket.keys.insert(key) 42 } else { 43 let newBucket = Bucket() 44 newBucket.keys.insert(key) 45 // insert the new bucket 46 newBucket.next = head.next 47 head.next = newBucket 48 newBucket.next!.prev = newBucket 49 newBucket.prev = head 50 bucketMap[1] = newBucket 51 } 52 } 53 countMap[key] = (countMap[key] ?? 0) + 1 54 } 55 56 /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ 57 func dec(_ key: String) { 58 if let count = countMap[key] { 59 // if the key exists. 60 bucketMap[count]!.keys.remove(key) 61 if count == 1 { } else if let pervBucket = bucketMap[count - 1] { 62 pervBucket.keys.insert(key) 63 } else { 64 let newBucket = Bucket() 65 newBucket.keys.insert(key) 66 // insert the new bucket 67 let nextBucket = bucketMap[count]! 68 newBucket.next = nextBucket 69 nextBucket.prev!.next = newBucket 70 newBucket.prev = nextBucket.prev 71 nextBucket.prev = newBucket 72 bucketMap[count - 1] = newBucket 73 } 74 if bucketMap[count]!.keys.count == 0 { 75 remove(bucketMap[count]!) 76 bucketMap[count] = nil 77 } 78 if countMap[key]! - 1 == 0 { 79 countMap[key] = nil 80 } else { 81 countMap[key] = countMap[key]! - 1 82 } 83 } 84 } 85 86 /** Returns one of the keys with maximal value. */ 87 func getMaxKey() -> String { 88 guard head.next! !== tail else { 89 return "" 90 } 91 // print(bucketMap) 92 // print(countMap) 93 return tail.prev!.keys.first! 94 } 95 96 /** Returns one of the keys with Minimal value. */ 97 func getMinKey() -> String { 98 guard head.next! !== tail else { 99 return "" 100 } 101 // print(bucketMap) 102 // print(countMap) 103 return head.next!.keys.first! 104 } 105 106 private func remove(_ bucket: Bucket) { 107 bucket.prev!.next = bucket.next 108 bucket.next!.prev = bucket.prev 109 } 110 } 111 112 class Bucket { 113 var keys: Set<String> 114 var prev: Bucket? 115 var next: Bucket? 116 117 init() { 118 self.keys = Set<String>() 119 } 120 } 121 122 /** 123 * Your AllOne object will be instantiated and called as such: 124 * let obj = AllOne() 125 * obj.inc(key) 126 * obj.dec(key) 127 * let ret_3: String = obj.getMaxKey() 128 * let ret_4: String = obj.getMinKey() 129 */
264ms
1 class AllOne { 2 private var list = LinkedList<Bucket>() 3 private var dict = [String: ListNode<Bucket>]() 4 5 /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ 6 func inc(_ key: String) { 7 8 if let node = dict[key] { 9 10 // update dict and node 11 let newCount = node.val.count + 1 12 if let pre = node.pre, pre.val.count == newCount { 13 pre.val.keys.insert(key) 14 dict[key] = pre 15 } else { 16 // add new node 17 let newNode = ListNode(Bucket(newCount, Set([key]))) 18 list.insert(newNode, before: node) 19 dict[key] = newNode 20 } 21 22 // remove key from node 23 node.val.keys.remove(key) 24 if node.val.keys.count == 0 { 25 list.remove(node) 26 } 27 28 } else { 29 if let tail = list.tail, tail.val.count == 1 { 30 tail.val.keys.insert(key) 31 dict[key] = tail 32 } else { 33 let bucket = Bucket(1, Set([key])) 34 let node = ListNode(bucket) 35 list.addToTail(node) 36 dict[key] = node 37 } 38 } 39 } 40 41 /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ 42 func dec(_ key: String) { 43 44 if let node = dict[key] { 45 46 let newCount = node.val.count - 1 47 if newCount == 0 { 48 dict[key] = nil 49 } else { 50 if let next = node.next, next.val.count == newCount { 51 next.val.keys.insert(key) 52 dict[key] = next 53 } else { 54 // add new node 55 let newNode = ListNode(Bucket(newCount, Set([key]))) 56 list.insert(newNode, after: node) 57 dict[key] = newNode 58 } 59 } 60 61 // remove key from node 62 node.val.keys.remove(key) 63 if node.val.keys.count == 0 { 64 list.remove(node) 65 } 66 67 } 68 } 69 70 /** Returns one of the keys with maximal value. */ 71 func getMaxKey() -> String { 72 return list.head?.val.keys.first ?? "" 73 } 74 75 /** Returns one of the keys with Minimal value. */ 76 func getMinKey() -> String { 77 return list.tail?.val.keys.first ?? "" 78 } 79 } 80 81 class LinkedList<T> { 82 var head: ListNode<T>? 83 var tail: ListNode<T>? 84 85 func addToTail(_ node: ListNode<T>) { 86 if head == nil { 87 head = node 88 tail = node 89 } 90 91 tail?.next = node 92 node.pre = tail 93 tail = node 94 } 95 96 func remove(_ node: ListNode<T>) { 97 if head === node { 98 head = node.next 99 } 100 if tail === node { 101 tail = node.pre 102 } 103 node.pre?.next = node.next 104 node.next?.pre = node.pre 105 node.pre = nil 106 node.next = nil 107 } 108 109 func insert(_ node: ListNode<T>, before node2: ListNode<T>?) { 110 if head === node2 { 111 head = node 112 } 113 let pre = node2?.pre 114 node2?.pre = node 115 node.next = node2 116 pre?.next = node 117 node.pre = pre 118 } 119 120 func insert(_ node: ListNode<T>, after node2: ListNode<T>?) { 121 if node2 === tail { 122 tail = node 123 } 124 let next = node2?.next 125 node2?.next = node 126 node.pre = node2 127 node.next = next 128 next?.pre = node 129 } 130 } 131 132 class ListNode<T> { 133 var val: T 134 var next: ListNode<T>? 135 weak var pre: ListNode<T>? 136 137 init(_ val: T) { 138 self.val = val 139 } 140 } 141 142 class Bucket { 143 var count: Int 144 var keys: Set<String> 145 146 init(_ count: Int, _ keys: Set<String>) { 147 self.count = count 148 self.keys = keys 149 } 150 } 151 152 /** 153 * Your AllOne object will be instantiated and called as such: 154 * let obj = AllOne() 155 * obj.inc(key) 156 * obj.dec(key) 157 * let ret_3: String = obj.getMaxKey() 158 * let ret_4: String = obj.getMinKey() 159 */
20376 kb
1 class AllOne { 2 3 var head: Bucket 4 var tail: Bucket 5 var bucketMap: [Int: Bucket] 6 var countMap: [String: Int] 7 8 /** Initialize your data structure here. */ 9 init() { 10 self.head = Bucket() 11 self.tail = Bucket() 12 self.bucketMap = [Int: Bucket]() 13 self.countMap = [String: Int]() 14 self.head.next = self.tail 15 self.tail.prev = self.head 16 } 17 18 /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */ 19 func inc(_ key: String) { 20 if let count = countMap[key] { 21 // if the key exists. 22 bucketMap[count]!.keys.remove(key) 23 if let nextBucket = bucketMap[count + 1] { 24 nextBucket.keys.insert(key) 25 } else { 26 let newBucket = Bucket() 27 newBucket.keys.insert(key) 28 // insert the new bucket 29 let prevBucket = bucketMap[count]! 30 newBucket.next = prevBucket.next 31 prevBucket.next = newBucket 32 newBucket.next!.prev = newBucket 33 newBucket.prev = prevBucket 34 bucketMap[count + 1] = newBucket 35 } 36 if bucketMap[count]!.keys.count == 0 { 37 remove(bucketMap[count]!) 38 bucketMap[count] = nil 39 } 40 } else { 41 if let firstBucket = bucketMap[1] { 42 firstBucket.keys.insert(key) 43 } else { 44 let newBucket = Bucket() 45 newBucket.keys.insert(key) 46 // insert the new bucket 47 newBucket.next = head.next 48 head.next = newBucket 49 newBucket.next!.prev = newBucket 50 newBucket.prev = head 51 bucketMap[1] = newBucket 52 } 53 } 54 countMap[key] = (countMap[key] ?? 0) + 1 55 } 56 57 /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */ 58 func dec(_ key: String) { 59 if let count = countMap[key] { 60 // if the key exists. 61 bucketMap[count]!.keys.remove(key) 62 if count == 1 { } else if let pervBucket = bucketMap[count - 1] { 63 pervBucket.keys.insert(key) 64 } else { 65 let newBucket = Bucket() 66 newBucket.keys.insert(key) 67 // insert the new bucket 68 let nextBucket = bucketMap[count]! 69 newBucket.next = nextBucket 70 nextBucket.prev!.next = newBucket 71 newBucket.prev = nextBucket.prev 72 nextBucket.prev = newBucket 73 bucketMap[count - 1] = newBucket 74 } 75 if bucketMap[count]!.keys.count == 0 { 76 remove(bucketMap[count]!) 77 bucketMap[count] = nil 78 } 79 if countMap[key]! - 1 == 0 { 80 countMap[key] = nil 81 } else { 82 countMap[key] = countMap[key]! - 1 83 } 84 } 85 } 86 87 /** Returns one of the keys with maximal value. */ 88 func getMaxKey() -> String { 89 guard head.next! !== tail else { 90 return "" 91 } 92 // print(bucketMap) 93 // print(countMap) 94 return tail.prev!.keys.first! 95 } 96 97 /** Returns one of the keys with Minimal value. */ 98 func getMinKey() -> String { 99 guard head.next! !== tail else { 100 return "" 101 } 102 // print(bucketMap) 103 // print(countMap) 104 return head.next!.keys.first! 105 } 106 107 private func remove(_ bucket: Bucket) { 108 bucket.prev!.next = bucket.next 109 bucket.next!.prev = bucket.prev 110 } 111 } 112 113 class Bucket { 114 var keys: Set<String> 115 var prev: Bucket? 116 var next: Bucket? 117 118 init() { 119 self.keys = Set<String>() 120 } 121 } 122 123 /** 124 * Your AllOne object will be instantiated and called as such: 125 * let obj = AllOne() 126 * obj.inc(key) 127 * obj.dec(key) 128 * let ret_3: String = obj.getMaxKey() 129 * let ret_4: String = obj.getMinKey() 130 */