[算法] 刷题-数据结构

ε祈祈猫儿з 提交于 2020-04-08 12:57:40

索引

  • 最小栈
  • 全O1结构
  • LRU

题解

  • 最小栈
class MinStack:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack = list()
        self.t_stack = list()


    def push(self, x: int) -> None:
        if len(self.stack) > 0 and x <= self.t_stack[-1]:
            self.t_stack.append(x)
        if len(self.stack) ==0:
            self.t_stack.append(x)
        self.stack.append(x)
        

    def pop(self) -> None:
        if len(self.stack) == 0:
            return None
        val = self.stack[-1]
        self.stack = self.stack[:-1]
        if val == self.t_stack[-1]:
            self.t_stack = self.t_stack[:-1]
        return val


    def top(self) -> int:
        if len(self.stack) == 0:
            return None
        return self.stack[-1]

    def getMin(self) -> int:
        if len(self.stack) == 0:
            return None
        return self.t_stack[-1]
  • 全O1结构
class AllOne:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.map = dict()
        self.counts = list()
        

    def inc(self, key: str) -> None:
        """
        Inserts a new key <Key> with value 1. Or increments an existing key by 1.
        """
        if key not in self.map:
            self.counts.append((key,1))
            self.map[key] = len(self.counts) - 1
            return 
        
        idx = self.map[key]
        old_count = self.counts[idx][1]
        
        swap_idx = idx
        for i in range(idx,-1,-1):
            item = self.counts[i]
            c = item[1]
            if c >= old_count + 1:
                break
            else:
                swap_idx = i
        
        if swap_idx != idx:
            item = self.counts[swap_idx]
            k = item[0]
            self.map[k] = idx
            self.counts[swap_idx],self.counts[idx] = self.counts[idx],self.counts[swap_idx]
            idx = swap_idx
            
        self.counts[idx] = (key,old_count + 1)
        self.map[key] = idx
    

    def dec(self, key: str) -> None:
        """
        Decrements an existing key by 1. If Key's value is 1, remove it from the data structure.
        """
        if key not in self.map:
            return
        
        idx = self.map[key]
        old_count = self.counts[idx][1]
        
        swap_idx = idx
        for i in range(idx,len(self.counts)):
            item = self.counts[i]
            c = item[1]
            if c<= old_count - 1:
                break
            else:
                swap_idx = i
        
        if swap_idx != idx:
            item = self.counts[swap_idx]
            k = item[0]
            self.map[k] = idx
            self.counts[swap_idx], self.counts[idx] = self.counts[idx], self.counts[swap_idx]
            idx = swap_idx
        
        if old_count -1 ==0:
            self.counts = self.counts[:-1]
            del(self.map[key])
        else:
            self.counts[idx] = (key, old_count -1)
            self.map[key] = idx
        

    def getMaxKey(self) -> str:
        """
        Returns one of the keys with maximal value.
        """
        if len(self.counts) == 0:
            return ''
        m = self.counts[0]
        return m[0]
        

    def getMinKey(self) -> str:
        """
        Returns one of the keys with Minimal value.
        """
        if len(self.counts) == 0:
            return ''
        
        m = self.counts[-1]
        return m[0]
  • LRU
class LRUCache:

    def __init__(self, capacity: int):
        self.stack = list()
        self.map = dict()
        self.cap = capacity


    def get(self, key: int) -> int:
        if key not in self.map:
            return -1
        
        idx = self.map[key]
        for i in range(idx+1, len(self.stack)):
            val = self.stack[i]
            k,v = val[0], val[1]
            self.map[k] -= 1
        
        top = self.stack[idx]
        self.stack[idx:-1] = self.stack[idx+1:]
        self.stack[-1] = top
        
        k,v = top[0],top[1]
        self.map[k] = len(self.stack) -1
        return v
        

    def put(self, key: int, value: int) -> None:
        if len(self.stack) <self.cap:
            if key not in self.map:
                self.stack.append((key,value))
                self.map[key] = len(self.stack) - 1
            else:
                idx = self.map[key]
                self.stack[idx] = (key,value)
            return 
        
        if key not in self.map:
            bottom = self.stack[0]
            k = bottom[0]
            del(self.map[k])

            for k in self.map:
                self.map[k] -= 1
            self.stack[0:-1] = self.stack[1:]

            self.stack[-1] = (key,value)
            self.map[key] = len(self.stack) - 1
        else:
            idx = self.map[key]
            for i in range(idx+1,len(self.stack)):
                val = self.stack[i]
                k,v = val[0],val[1]
                self.map[k] -=1
            
            self.stack[idx:-1] = self.stack[idx+1:]
            self.stack[-1] = (key,value)
            self.map[key] = len(self.stack) - 1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!