邓心一 6 anni fa
parent
commit
fd84e2b809
1 ha cambiato i file con 77 aggiunte e 0 eliminazioni
  1. 77 0
      hard/432.all-oone-data-structure.go

+ 77 - 0
hard/432.all-oone-data-structure.go

@@ -0,0 +1,77 @@
+type none struct{}
+
+type pair {
+	key int
+	set map[string]none
+}
+
+type AllOne struct {
+	m  map[string]*list.Element
+	li *list.List
+}
+
+/** Initialize your data structure here. */
+func Constructor() AllOne {
+	return AllOne{
+		m:  make(map[string]*list.Element),
+		li: list.New(),
+	}
+}
+
+/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
+func (this *AllOne) Inc(key string) {
+	if val, ok := this.m[key]; ok { // Increments
+		next := val.Next()
+		if next == nil || val.(pair).key+1 != next.(pair).key { // Insert after
+			next = 
+		} else { // Increment after
+			
+		}
+		this.m[key] = next
+	} else { // Inserts
+		if this.li.Len() == 0 || this.li.Front().Value.(pair).key != 1 {
+			this.li.PushFront(pair{1, map[string]none{key: none{}})
+		} else {
+			this.li.Front().Value.(pair).set[key] = none{}
+		}
+		this.m[key] = this.li.Front()
+	}
+}
+
+/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
+func (this *AllOne) Dec(key string) {
+	if val, ok := this.m[key]; ok {
+		
+	}
+}
+
+/** Returns one of the keys with maximal value. */
+func (this *AllOne) GetMaxKey() string {
+	if this.li.Len() == 0 {
+		return ""
+	}
+	set := this.li.Back().Value.(pair).set
+	for k := range set {
+		return k
+	}
+}
+
+/** Returns one of the keys with Minimal value. */
+func (this *AllOne) GetMinKey() string {
+	if this.li.Len() == 0 {
+		return ""
+	}
+	set := this.li.Front().Value.(pair).set
+	for k := range set {
+		return k
+	}
+}
+
+/**
+ * Your AllOne object will be instantiated and called as such:
+ * obj := Constructor();
+ * obj.Inc(key);
+ * obj.Dec(key);
+ * param_3 := obj.GetMaxKey();
+ * param_4 := obj.GetMinKey();
+ */