Skip to content

Commit e32ff87

Browse files
author
Partho Biswas
committed
432_All_O_one_Data_Structure
1 parent 1e9db57 commit e32ff87

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ I have solved quite a number of problems from several topics. See the below tabl
290290
|25| **[460. LFU Cache](https://tinyurl.com/y7qokgr8)** | [Python](https://tinyurl.com/wu6rdaw/237_Delete_Node_in_a_Linked_List.py)| [Art 1](https://tinyurl.com/rtnhhys) | Easy | |
291291
|26| **[708. Insert into a Sorted Circular Linked List](https://tinyurl.com/yy5ga9gn)** | [Python](https://tinyurl.com/wu6rdaw/708_Insert_into_a_Sorted_Circular_Linked_List.py), [Swift](https://tinyurl.com/wuja3c4/708_Insert_into_a_Sorted_Circular_Linked_List.swift) | [Art 1](https://tinyurl.com/y4p9krj8) | Medium | |
292292
|27| **[203. Remove Linked List Elements](https://tinyurl.com/y6bmycyr)** | [Python](https://tinyurl.com/wu6rdaw/203_Remove_Linked_List_Elements.py), [Swift](https://tinyurl.com/wuja3c4/203_Remove_Linked_List_Elements.swift) | --- | Easy | |
293+
|28| **[432. All O`one Data Structure](https://tinyurl.com/zqkzen4)** | [Python](https://tinyurl.com/wu6rdaw/432_All_O_one_Data_Structure.py), [Swift](https://tinyurl.com/wuja3c4/432_All_O_one_Data_Structure.swift) | [Art 1](https://tinyurl.com/y2ame7dc) | Hard | Super hard and super important |
293294

294295

295296
</p>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import Foundation
2+
// Source: https://tinyurl.com/y2ame7dc
3+
public class DoublyLinkedListNode {
4+
public var counterValKeySet = Set<String>()
5+
public var next: DoublyLinkedListNode?
6+
public var prev: DoublyLinkedListNode?
7+
}
8+
9+
public class DoublyLinkedList {
10+
public var dummyHead = DoublyLinkedListNode()
11+
public var dummyTail = DoublyLinkedListNode()
12+
public init() {
13+
dummyHead.next = dummyTail
14+
dummyTail.prev = dummyHead
15+
}
16+
17+
public func insertAfter(_ node: DoublyLinkedListNode) -> DoublyLinkedListNode {
18+
var (nodeToReturn, tempNode) = (DoublyLinkedListNode(), node.next)
19+
node.next = nodeToReturn
20+
nodeToReturn.prev = node
21+
nodeToReturn.next = tempNode
22+
tempNode?.prev = nodeToReturn
23+
return nodeToReturn
24+
}
25+
26+
public func insertBefore(_ node: DoublyLinkedListNode) -> DoublyLinkedListNode {
27+
return insertAfter(node.prev!)
28+
}
29+
30+
public func removeNode(_ node: DoublyLinkedListNode) {
31+
node.prev?.next = node.next
32+
node.next?.prev = node.prev
33+
}
34+
}
35+
36+
class AllOne {
37+
var keyCounterToNodeMap = [Int: DoublyLinkedListNode]()
38+
var keyToCounterMap = [String: Int]()
39+
var dll = DoublyLinkedList()
40+
41+
/** Initialize your data structure here. */
42+
init() {
43+
keyCounterToNodeMap[0] = dll.dummyHead
44+
}
45+
46+
47+
private func removePreviousKeyAndNode(_ previousCount: Int, _ key: String) {
48+
if var previousNode = keyCounterToNodeMap[previousCount] {
49+
previousNode.counterValKeySet.remove(key)
50+
if previousNode.counterValKeySet.isEmpty {
51+
dll.removeNode(previousNode)
52+
keyCounterToNodeMap.removeValue(forKey: previousCount)
53+
}
54+
}
55+
}
56+
57+
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
58+
func inc(_ key: String) {
59+
keyToCounterMap[key, default:0] += 1
60+
let (currentCount, previousCount) = (keyToCounterMap[key]!, keyToCounterMap[key]! - 1)
61+
if keyCounterToNodeMap[currentCount] == nil {
62+
keyCounterToNodeMap[currentCount] = dll.insertAfter(keyCounterToNodeMap[previousCount]!)
63+
}
64+
keyCounterToNodeMap[currentCount]?.counterValKeySet.insert(key)
65+
if previousCount > 0 {
66+
removePreviousKeyAndNode(previousCount, key)
67+
}
68+
}
69+
70+
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
71+
func dec(_ key: String) {
72+
if let previousCount = keyToCounterMap[key] {
73+
keyToCounterMap[key] = previousCount - 1
74+
if let currentCounter = keyToCounterMap[key] {
75+
if currentCounter == 0 {
76+
keyToCounterMap.removeValue(forKey: key)
77+
} else {
78+
if keyCounterToNodeMap[currentCounter] == nil {
79+
keyCounterToNodeMap[currentCounter] = dll.insertBefore(keyCounterToNodeMap[previousCount]!)
80+
}
81+
keyCounterToNodeMap[currentCounter]?.counterValKeySet.insert(key)
82+
}
83+
}
84+
removePreviousKeyAndNode(previousCount, key)
85+
}
86+
}
87+
88+
/** Returns one of the keys with maximal value. */
89+
func getMaxKey() -> String {
90+
if dll.dummyTail.prev!.counterValKeySet.count > 0 {
91+
return dll.dummyTail.prev!.counterValKeySet.first ?? ""
92+
} else {
93+
return ""
94+
}
95+
}
96+
97+
/** Returns one of the keys with Minimal value. */
98+
func getMinKey() -> String {
99+
if dll.dummyHead.next!.counterValKeySet.count > 0 {
100+
return dll.dummyHead.next!.counterValKeySet.first ?? ""
101+
} else {
102+
return ""
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)