解題報告-01

Intro

以後在掘金上放點本身作題目的解題報告,幫助本身回顧數組

題目

Alice and Bob work in a beautiful orchard. There are N apple trees in the orchard. The apple trees are arranged in a row and they are numbered from 1 to N. Alice is planning to collect all the apples from K consecutive trees and Bob is planning to collect all the apples from L consecutive trees. They want to choose two disjoint segments (one consisting of K trees for Alice and the other consisting of L trees for Bob) so as not to disturb each other. What is the maximum number of apples that they can collect? Write a function that given an array A consisting of N integers denating the number of apples on each apple tree in the row, and integers K and L denoting, respectively, thenumber of trees that Alice and Bob can choose when collecting, returns the maximum number of apples that can be collected by them, or -1 if there are no such intervals. For example, given A =[6, 1,4,6,3,2,7,4], K=3, L=2, your function should return 24, because Alice can choose trees 3 to 5 and collect 4 + 6 + 3 = 13 apples, and Bob can choose trees 7 to 8 and collect 7 + 4 = 11 apples. Thus, they will collect 13 + 11 = 24 apples in total, and that is the maximum number that can be achieved. Given A = [10, 19, 15], K = 2, L = 2, your function should return -1, because it is not possible for Alice and Bob to choose two disjoint intervals. Assume that: N is an integer within the range [2..600]; K and L are integers within the range [1 .. N-1]; each element of array A is an integer within the range [1..500] In your solution focus on correctness. The performance of your solution will not be the focus of the assessment.app

題目大意

Alice和Bob在果園摘蘋果,蘋果樹長成一橫排共N棵,Alice會連續摘K棵蘋果樹,Bob會連續摘L棵蘋果樹,求K+L棵樹上蘋果的最大值。不考慮性能,只考慮正確性。性能

解題思路

暴力

不考慮性能的狀況下,其實很快就有一種暴力的解法,O(n^3)的解法,遍歷整個數組,每K個數據計算和,而後在其右邊的剩下數組裏找L個最大的值。優化

優化

其實Alice和Bob的K個和L個求和的時候,只要求第一遍就好了,後面日後遍歷的時候,只要加上新的值,減去最左邊的值就能夠了。這樣能降到O(n^2)ui

陷阱 spa

寫代碼的時候很容易會遺忘一種狀況,好比數組[1,2,0,1,2,1],K=3,L=2,若是按上述解法,獲得的是6,可是最大值是7,由於能夠Bob摘最左邊的,Alice摘最右邊的,因此解的時候還要計算反向。code

fun solution(A: IntArray, K: Int, L: Int): Int {
        if (A.size < K + L) { //invalid
            return -1
        }

        if (A.size == K + L) { //only one solution
            return A.sum()
        }

        var sum = 0
        var alice = 0
        var bob = 0
        for (i in 0..A.size - K - L) {
            if (alice == 0) {
                for (k in i until i + K) {
                    alice += A[k]
                }
            } else {
                alice += A[i + K - 1]
                alice -= A[i - 1]
            }
            bob = 0
            for (j in i + K..A.size - L) {
                if (bob == 0) {
                    for (k in j until j + L) {
                        bob += A[k]
                    }
                } else {
                    bob += A[j + L - 1]
                    bob -= A[j - 1]
                }
                sum = max(sum, alice + bob)
            }

        }

        bob = 0
        for (i in 0..A.size - K - L) {
            if (bob == 0) {
                for (k in i until i + L) {
                    bob += A[k]
                }
            } else {
                bob += A[i + L - 1]
                bob -= A[i - 1]
            }
            alice = 0
            for (j in i + L..A.size - K) {
                if (alice == 0) {
                    for (k in j until j + K) {
                        alice += A[k]
                    }
                } else {
                    alice += A[j + K - 1]
                    alice -= A[j - 1]
                }
                sum = max(sum, alice + bob)
            }
        }

        return sum
    }
複製代碼
相關文章
相關標籤/搜索