MCPcopy Create free account
hub / github.com/course-dasheng/fe-algorithm / solution

Function solution

interview/frog-jump.js:21–45  ·  view source on GitHub ↗
(blocks)

Source from the content-addressed store, hash-verified

19
20// 动态规划
21function solution(blocks) {
22 let ret = [] // 每一个block[i]作为起点的时候,最大的距离
23
24 for(let i=0;i<blocks.length;i++) {
25 // i分界线,左右两个青蛙开始跳
26 let j = k = i
27 for(j=i;j>=0;j--){
28 // 第一只青蛙从右向左跳
29 if(j===0 || blocks[j] > blocks[j-1]) {
30 break
31 }
32 }
33
34 for(k=i;k<blocks.length-1;k++){
35 // 第二只青蛙从右向左跳
36 if(blocks[k] > blocks[k+1]) {
37 break
38 }
39 }
40
41 // console.log(j,k)
42 ret.push(k-j+1)
43 }
44 return Math.max(...ret)
45}
46console.log(solution([2,6,8,5]))
47console.log(solution([1,5,5,2,6]))
48console.log(solution([0,1]))

Callers 1

frog-jump.jsFile · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected