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))