js贪心算法

 

 

 

 

 

 

 

 

export default (prices)=>{
  //用来保存利润
  let count=0
  for(let i=0,len=prices.length;i<len;i++){
     for(let j=i;j<len-1;j++){
       if(prices[j+1]>prices[j]){
         count+=prices[j+1]-prices[j]
         i=j
       }else{
         i=j
         break
       }
     }
  }
  return count
}

  

 

 

export default(intput)=>{
  //表示自己的钱箱(用于存储零钱)
  let hand=[]
  //判断是否有顾客还在
  while(Input.length){
    //取出当前排在最前面顾客的钱
    let money=inpuy.shift()
    if(money===5){
      hand.push(money)
    }else{
      //零钱降序排列
      hand.sort((a,b)=>b-a)
      let change=money-5
      for(let i=0,len=hand.length;i<len;i++){
        if(hand[i]<=change){
          change-=hand[i]
          hand.splice(i,1)
          //删除了元素数组的长度发生了变化,要维持刚才的i不变
          i--
        }
        if(change===0){
          break
        }
      }
      //没有足够的零钱
      if(change!==0) return false
      else hand.push(money)  //顾客钱存起来
    }
  }
  return true
}

  动态规划:

export default(arr,m,n)=>{
  let dp=(m,n)=>{
    //,=2,n=2
    if(m===2&&n===2){
       return(arr[1][1]===1||arr[1][0]+arr[0][1]===2)?0:(arr[1][0]===1||arr[0][1]===1)?1:2
    }else if(m<2||n<2){
      if(m<2){
        //单行有1就返回0,没有1返回0
       return arr[m-1].includes(1)?0:1
      }else{
        //单列中不能有障碍物(1) 有返回0 没有返回1
        for(let i=0;i<m;i++){
          if(arr[i][0]===1){
            return 0
          }
        }
        return 1
      }
    }
    else{
      return dp(m-1,n)+dp(m,n-1)
    }
  }
  return dp(m,n)
}

  查找两地最短距离

export default(src,dst,k)=>{
    //对n个城市m个航班做飞行说明
    let fights=[
         [0,1,100],
         [1,2,100],
         [0,2,500]
    ]
    let cheap=(src,dst,k)=>{
        let prev=fights.filter(item=>item[1]===dst)
        let min=Math.min.apply(null,prev.map(item=>{
            //从dst往前找,找到了起始城市
            if(item[0]===src&&k>-1){
                return item[2]
            }else if(k===0&&item[0]!==src){
                return Number.MAX_SAFE_INTEGER
            }else{
                return item[2]+cheap(src,item[0],k-1)
            }
        }))
        return min
    }
    return cheap(src,dst,k)||-1
}

  数组的优缺点:

 

 

 

 

 

 

 

 优先队列:

 

 

 

 

 

 

 

前缀树:

 

 

栈封装:

function Stack(){
    //栈中的属性
   this.items=[]
   //栈的相关操作 1.将元素添加栈  2.从栈中取出元素 3.查看一下栈顶元素  4.判断栈是否为空  5.获取栈中元素的个数 6.toString方法
   Stack.prototype.push=function(element){
       this.items.push(element)
   }
   Stack.prototype.pop=function(){
       return this.items.pop()
   }
   Stack.prototype.peek=function(){
	   return this.items[this.items.length-1]
   }
   Stack.prototype.isEmpty=function(){
       return this.items.length===0
   }
   Stack.prototype.size=function(){
       return this.items.length
   }
   Stack.prototype.toString=function(){
       var resultString=''
       for(var i=0;i<this.items.length;i++){
        resultString+=this.items[i]+' '
       }
       return resultString
   }
}

  

//函数:十进制转二进制
function dec2bin(decNumber){
     //1.定义一个栈对象
     var stack=new Stack()

	 //2.循环操作
	 while(decNumber>0){
		 //2.1获取余数,并放入到栈中
		 stack.push(decNumber % 2)

		 //2.获取整除后的结果,作为下一次运算的数字
		 decNumber=Math.floor(decNumber/2)
	 }
	 //3.从栈中取出0和1
	 var binaryString=''
	 while(!stack.isEmpty()){
		binaryString+=stack.pop()
	 }
	 return binaryString
}

  封装队列

//分装队列类
function Queue(){
	//1.属性
     this.items=[]
	//2.方法  1.将元素添加到队列中 2.从队列中删除前端元素  3.查看前端的元素  4.查看队列是否为空  5.查看队列中元素的个数  6.toString()方法
    Queue.prototype.enqueue=function(element){
		 this.items.push(element)
	}

	Queue.prototype.dequeue=function(){
	   return this.items.shift()
	}

	Queue.prototype.front=function(){
		return this.items[0]
	}

	Queue.prototype.isEmpty=function(){
		return this.items.length===0
	}

	Queue.prototype.size=function(){
		return this.items.length
	}

	Queue.prototype.toString=function(){
		var resultString=''
		for(var i=0;i<this.items.length;i++){
			resultString+=this.items[i]+''
		}
		return resultString
	}
}

  丢手帕:

function passGame(nameList,num){
        var queue=new Queue()
        for(var i=0;i<nameList.length;i++){
            queue.enqueue(nameList[i])
        }

        while(queue.size()>1){
            for(var i=0;i<num-1;i++){
               queue.enqueue(queue.dequeue())
            }

            queue.dequeue()
        }
        alert(queue.size())
        var endName=queue.front()
        alert('最后剩下的人'+endName)
        return nameList.indexOf(endName)
        
    }
    names=['Lily','Lucy','Tom','Lilei','Why']
    alert(passGame(names,3))

  

posted @ 2020-09-06 22:11  石shi  阅读(894)  评论(0编辑  收藏  举报