432.all-oone-data-structure.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. type none struct{}
  2. type pair struct {
  3. key int
  4. set map[string]none
  5. }
  6. type AllOne struct {
  7. m map[string]*list.Element
  8. li *list.List
  9. }
  10. /** Initialize your data structure here. */
  11. func Constructor() AllOne {
  12. return AllOne{
  13. m: make(map[string]*list.Element),
  14. li: list.New(),
  15. }
  16. }
  17. /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
  18. func (this *AllOne) Inc(key string) {
  19. if val, ok := this.m[key]; ok { // Increments
  20. next := val.Next()
  21. if next == nil || val.Value.(pair).key+1 < next.Value.(pair).key { // Insert after
  22. next = this.li.InsertAfter(pair{val.Value.(pair).key + 1, map[string]none{key: none{}}}, val)
  23. } else { // Increment after
  24. next.Value.(pair).set[key] = none{}
  25. }
  26. this.m[key] = next
  27. if len(val.Value.(pair).set) == 1 { // Remove current element
  28. this.li.Remove(val)
  29. } else { // Remove from set
  30. delete(val.Value.(pair).set, key)
  31. }
  32. } else { // Inserts
  33. if this.li.Len() == 0 || 1 < this.li.Front().Value.(pair).key { // Push front
  34. this.li.PushFront(pair{1, map[string]none{key: none{}}})
  35. } else { // Move to ones set
  36. this.li.Front().Value.(pair).set[key] = none{}
  37. }
  38. this.m[key] = this.li.Front()
  39. }
  40. }
  41. /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
  42. func (this *AllOne) Dec(key string) {
  43. if val, ok := this.m[key]; ok {
  44. if val.Value.(pair).key == 1 { // Removes
  45. if len(val.Value.(pair).set) == 1 { // Remove whole element
  46. this.li.Remove(val)
  47. } else { // Remove from set
  48. delete(val.Value.(pair).set, key)
  49. }
  50. delete(this.m, key) // Remove from map
  51. } else { // Decrements
  52. prev := val.Prev()
  53. if prev == nil || prev.Value.(pair).key < val.Value.(pair).key-1 { // Insert before
  54. prev = this.li.InsertBefore(pair{val.Value.(pair).key - 1, map[string]none{key: none{}}}, val)
  55. } else { // Move to prev set
  56. prev.Value.(pair).set[key] = none{}
  57. }
  58. this.m[key] = prev
  59. if len(val.Value.(pair).set) == 1 { // Remove current element
  60. this.li.Remove(val)
  61. } else {
  62. delete(val.Value.(pair).set, key) // Remove from set
  63. }
  64. }
  65. }
  66. }
  67. /** Returns one of the keys with maximal value. */
  68. func (this *AllOne) GetMaxKey() string {
  69. if this.li.Len() != 0 {
  70. set := this.li.Back().Value.(pair).set
  71. for k := range set {
  72. return k
  73. }
  74. }
  75. return ""
  76. }
  77. /** Returns one of the keys with Minimal value. */
  78. func (this *AllOne) GetMinKey() string {
  79. if this.li.Len() != 0 {
  80. set := this.li.Front().Value.(pair).set
  81. for k := range set {
  82. return k
  83. }
  84. }
  85. return ""
  86. }
  87. /**
  88. * Your AllOne object will be instantiated and called as such:
  89. * obj := Constructor();
  90. * obj.Inc(key);
  91. * obj.Dec(key);
  92. * param_3 := obj.GetMaxKey();
  93. * param_4 := obj.GetMinKey();
  94. */