Advertisement
HXXXXJ

432. All O`one Data Structure

Mar 24th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.09 KB | None | 0 0
  1. class Node{
  2.     var keys = Set<String>()
  3.     var pre: Node?
  4.     var next: Node?
  5.     var count: Int
  6.     init(_ count: Int){
  7.         self.count = count
  8.     }
  9. }
  10.  
  11.  
  12. class AllOne {
  13.     var keyMap = [String: Int]()
  14.     var nodeMap = [Int: Node]()
  15.     var head : Node
  16.     var tail : Node
  17.     var nodeCount = 0
  18.     /** Initialize your data structure here. */
  19.     init() {
  20.         head = Node(-1)
  21.         tail = Node(-1)
  22.         tail.pre = head
  23.         head.next = tail
  24.     }
  25.     func addNodeAfter(_ preNode: Node, _ node: Node){
  26.          // print(" addd \(node.count)" )
  27.         let nextNode = preNode.next!
  28.         nextNode.pre = node
  29.         node.next = nextNode
  30.        
  31.         preNode.next = node
  32.         node.pre = preNode
  33.         nodeCount += 1
  34.     }
  35.     func removeNode(_ node: Node){
  36.         // print(" remove \(node.count)" )
  37.         let pre = node.pre!
  38.         let next = node.next!
  39.         pre.next = next
  40.         next.pre = pre
  41.         node.pre = nil
  42.         node.next = nil
  43.         nodeCount -= 1
  44.     }
  45.    
  46.     /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
  47.     func inc(_ key: String) {
  48.         if let count = keyMap[key]{
  49.             //remove this key from previous Node
  50.             let node = nodeMap[count]!
  51.             node.keys.remove(key)
  52.            
  53.             //add to new node
  54.             if let node_next = nodeMap[count + 1]{
  55.                 node_next.keys.insert(key)
  56.             }else{
  57.                 let node_next = Node(count + 1)
  58.                 node_next.keys.insert(key)
  59.                
  60.                 addNodeAfter(node, node_next)
  61.                 nodeMap[count + 1] = node_next
  62.             }
  63.             keyMap[key] = count + 1
  64.            
  65.            
  66.             if node.keys.count == 0 {
  67.                 removeNode(node)
  68.                 nodeMap[count] = nil
  69.             }
  70.            
  71.         }else{
  72.             if let node_next = nodeMap[1]{
  73.                 node_next.keys.insert(key)
  74.             }else{
  75.                 let node_next = Node(1)
  76.                 addNodeAfter(head, node_next)
  77.                 node_next.keys.insert(key)
  78.                
  79.                 nodeMap[1] = node_next
  80.             }
  81.             keyMap[key] = 1
  82.         }
  83.        
  84.  
  85.     }
  86.    
  87.     /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
  88.     func dec(_ key: String) {
  89.         if let count = keyMap[key], let node = nodeMap[count]{
  90.             node.keys.remove(key)
  91.  
  92.             //add to new node
  93.             if count > 1 {
  94.                 if let node_next = nodeMap[count - 1]{
  95.                     node_next.keys.insert(key)
  96.                 }else{
  97.                     let node_next = Node(count - 1)
  98.                     node_next.keys.insert(key)
  99.                
  100.                     addNodeAfter(node.pre!, node_next)
  101.                     nodeMap[count - 1] = node_next
  102.                 }
  103.                 keyMap[key] = count - 1
  104.             }
  105.                
  106.             if node.keys.count == 0 {
  107.                 removeNode(node)
  108.                 nodeMap[count] = nil
  109.             }
  110.         }
  111.  
  112.     }
  113.    
  114.     /** Returns one of the keys with maximal value. */
  115.     func getMaxKey() -> String {
  116.         if nodeCount == 0 {return ""}
  117.         // print(tail.pre!.count)
  118.         for key in tail.pre!.keys{
  119.             return key
  120.         }
  121.                            return ""
  122.         // return Array(tail.pre!.keys)[0]
  123.     }
  124.    
  125.     /** Returns one of the keys with Minimal value. */
  126.     func getMinKey() -> String {
  127.         if nodeCount == 0 {return ""}
  128.         // print(head.next!.count)
  129.         for key in head.next!.keys{
  130.             return key
  131.         }
  132.         return ""
  133.     }
  134.        
  135.     func printLink(){
  136.         var node = head
  137.         while node !== tail{
  138.             print(node.count)
  139.             node = node.next!
  140.         }
  141.     }
  142. }
  143.  
  144. /**
  145.  * Your AllOne object will be instantiated and called as such:
  146.  * let obj = AllOne()
  147.  * obj.inc(key)
  148.  * obj.dec(key)
  149.  * let ret_3: String = obj.getMaxKey()
  150.  * let ret_4: String = obj.getMinKey()
  151.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement