【问题标题】:unable to understand codility fish无法理解 codility 鱼
【发布时间】:2020-05-29 10:30:31
【问题描述】:

https://app.codility.com/demo/results/trainingAQH33G-WVS/

Array.prototype.peek = function() {
    return this[this.length-1]
}

export const fish = (A = [4, 3, 2, 1, 5], B = [0, 1, 0, 0, 0]) => {

    console.log(` A ${A}, B ${B} `)
    let c = B[0]
    let s = []
    s.push(A[0])
    console.log(`init c ${c}, n , s ${s}, A[0] ${A[0]}, `)
    console.log(`starting with ${s.peek()}`)
    for (let i = 1; i < A.length; i++) {
        let n = B[i]
        console.log(` c ${c}, n ${n}, s ${s}, A[i] ${A[i]}, `)
        if ( c === n ) {
            s.push(A[i])
            console.log(`same direction ${n}, so append ${A[i]}`)
        }
        else if (c !== n) {
            if (s.peek() > A[i]) {
                console.log(`I am fish size ${A[i]} eaten by ${s.peek()}`)
            }
            while (s.peek() < A[i]) {
                console.log(`${A[i]} eating  ${s.peek()}, so pop ${s.peek()}  `)
                s.pop()
            }
            if (s.length === 0) {
                s.push(A[i])
                c = n
                console.log(`last big fish ${A[i]} on direction ${n} `)

            }

        }
    }
    console.log('result : ', s)
    return s.length

}

逻辑上大鱼在相反方向相遇时应该吃小鱼。 如果他们在同一个方向,他们将生存 我哪里出错了?

fish([ 4, 3, 2, 1, 5], [ 1, 0, 1, 0, 1] ) // 3
fish([ 4, 3, 2, 0, 5], [ 0, 1, 0, 0, 0] ) // expected 2, but getting 4
fish([ 4, 3, 2, 1, 5], [ 0, 1, 0, 0, 0] ) // expected 2, but getting 4
fish([ 4, 3, 2, 1, 5], [ 0, 1, 1, 0, 0] ) // expected 2, but getting 3
fish([ 4, 3, 2, 5, 6], [ 1, 0, 1, 0, 1] ) // expected 2, but getting 1
fish([ 7, 4, 3, 2, 5, 6 ], [ 0, 1, 1, 1, 0, 1 ] ) // expected 3, but getting 7
fish([ 3, 4, 2, 1, 5 ], [ 1, 0, 0, 0, 0 ] ) // expected 4
fish([ 3 ], [ 1 ] ) // 1
fish([ 3 ], [ 0 ] ) // 1

【问题讨论】:

    标签: javascript algorithm stack


    【解决方案1】:

    https://app.codility.com/demo/results/trainingFHX89J-JQ9/

    
    
    export const fish_count = (A = [4, 3, 2, 1, 5], B = [0, 1, 0, 0, 0]) => {
    
        console.log(` A ${A}, B ${B} `)
    
        const fish = (idx, siz, dir) => {  return { idx, siz, dir,
                    print () {
                        return  ` ${this.siz} ${this.dir} `
                    } } }
    
    
        const fishes = []
    
    
        for (let i = 0; i < A.length; i++) {
            let f = fish(i, A[i], B[i])
            fishes.push(f)
        }
    
        let f = fishes.shift()
    
        const survive = []
        survive.push(f)
    
        const print = () => {
    
            let str = survive.map((v) =>  v.print() ).join(' : ')
            return str
    
        }
        console.log(`${f.idx}, ${f.siz}, ${f.dir}`)
    
        const handle_move = (g) => {
    
            f = survive.peek()
            console.log(`${g.idx}, ${g.siz}, ${g.dir}, array ${print()}`)
    
            if (survive.length === 0) {
                console.log(` empty so append`)            
                survive.push(g)
            }
            else if (f.dir === g.dir) {
                console.log(` same direction `)            
                survive.push(g)
            }
            else if (f.dir === 0 && g.dir === 1) {
                console.log(` diamond direction, no eating `)            
                survive.push(g)
            }
            else if (f.dir === 1 && g.dir === 0) {
                if (f.siz > g.siz) {
                    console.log(` survive ${f.siz} > fishes ${g.siz}, so fishes ${g.print()} die`)
                    // do not push. let g die
                }
                else if (f.siz < g.siz) {
                    console.log(` survive ${f.siz} < fishes ${g.siz}, so survive ${f.print()} die`)
                    survive.pop()
                    return true // recursive
                }
            }
    
            return false
        }
    
    
        for ( let g of fishes) {
            while (handle_move(g)) {}
        }
        console.log(` array ${print()}`)
    
        console.log('result : ', survive.length)
        return survive.length
    
    }
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      相关资源
      最近更新 更多