30 天刷题挑战(十)

题目来源: LeetCode 75 30 天 JavaScript 挑战

2637. 有时间限制的 Promise 对象

思路

fn 和定时器比较执行时间,如果超出限制时间 treject

代码

ts 复制代码
type Fn = (...params: any[]) => Promise<any>;

function timeLimit(fn: Fn, t: number): Fn {
  return async function(...args) {
    return new Promise((resolve, reject) => {
      fn(...args).then(resolve, reject)
      setTimeout(() => reject("Time Limit Exceeded"), t)
    })
  }
};

2622. 有时间限制的缓存

思路

Map 对象存储数据,给每一项添加时间限制,超时的项目直接删除

代码

ts 复制代码
interface Record {
  time: number,
  data: number
}

class TimeLimitedCache {
    store: Map<number, Record>
    
    constructor() {
      this.store = new Map()
    }
    
    set(key: number, value: number, duration: number): boolean {
      const now = Date.now()
      
      if (this.store.has(key)) {
        const val = this.store.get(key)
        val.time = now + duration
        val.data = value
        this.store.set(key, val) 

        return true
      }

      this.store.set(key, {
        data: value,
        time: now + duration
      })
       
      return false
    }

    get(key: number): number {
      if (!this.store.has(key)) {
        return -1
      }

      const val = this.store.get(key)
      const now = Date.now()
      const time = val.time

      if (now > time) {
        return -1
      }
        
      return val.data
    }

    count(): number {
      const now = Date.now()
      let res = 0

      for (let item of this.store) {
        const [key, val] = item

        if (now > val.time) {
          this.store.delete(key)
        } else {
          res++
        }

      }   
      return res
    }
}

933. 最近的请求次数

思路

每次调用 ping 就向 list 添加这次请求,然后返回数组中小于 t - 3000 的请求数

代码

ts 复制代码
class RecentCounter {
    list: Array<number>

    constructor() {
      this.list = []
    }

    ping(t: number): number {
      this.list.push(t)
      
      while(this.list[0] < t - 3000) {
        this.list.shift()
      }

      return this.list.length
    }
}

649. Dota2 参议院

思路

使用一个队列和一个栈,每次从队列开头拿出一项跟栈最后一项比较,相同就推进栈,不同就把栈顶推出,添加到队列尾

代码

ts 复制代码
function predictPartyVictory(senate: string): string {
  const stack = []
  const queue = senate.split("")

  while(queue.length) {
    const c = queue.shift()

    if (stack.length === 0 || stack[stack.length - 1] === c) {
      stack.push(c)
    } else {
      queue.push(stack.pop())
    }
  }

  return stack.pop() === 'R' ? 'Radiant' : 'Dire'
};

本文完,感谢阅读 🌹

相关推荐
不爱吃饭爱吃菜8 分钟前
uniapp微信小程序一键授权登录
前端·javascript·vue.js·微信小程序·uni-app
90后小陈老师1 小时前
3D个人简历网站 5.天空、鸟、飞机
前端·javascript·3d
Hello World......1 小时前
互联网大厂Java面试:从Spring到微服务的全面探讨
java·spring boot·spring cloud·微服务·面试·技术栈·互联网大厂
远瞻。2 小时前
【论文阅读】人脸修复(face restoration ) 不同先验代表算法整理2
论文阅读·算法
不爱吃糖的程序媛5 小时前
浅谈前端架构设计与工程化
前端·前端架构设计
先做个垃圾出来………5 小时前
哈夫曼树(Huffman Tree)
数据结构·算法
郝YH是人间理想7 小时前
系统架构设计师案例分析题——web篇
前端·软件工程
Evaporator Core7 小时前
深入探索:Core Web Vitals 进阶优化与新兴指标
前端·windows
phoenix@Capricornus7 小时前
反向传播算法——矩阵形式递推公式——ReLU传递函数
算法·机器学习·矩阵
初遇你时动了情7 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html