thảo luận Leetcode mỗi ngày

Python:
class Solution:
    def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
        sumInRange, sumWithGrumpy, totalSum = 0, 0, 0
        left, maxSofar = 0, 0
        n = len(customers)
        for right in range(n):
            sumInRange += customers[right]
            target = 0 if grumpy[right] == 1 else customers[right]
            sumWithGrumpy += target
            totalSum += target
            maxSofar = max(maxSofar, sumInRange - sumWithGrumpy)
            if right - left + 1 == minutes:
                sumInRange -= customers[left]
                sumWithGrumpy -= 0 if grumpy[left] == 1 else customers[left]
                left += 1
        return totalSum + maxSofar
 
Tính ra bài hôm nay đơn giản, chỉ cần dùng sliding window để tính max thôi, còn cộng với đống 0 còn lại là xong. Không thấy edge case
JavaScript:
function maxSatisfied(c: number[], g: number[], m: number): number {
    const n = c.length;
    let cur = 0;
    for (let i = 0; i < m; i++) cur+= g[i] * c[i]
    let res = cur;
    for (let i = m; i < n; i++) {
        cur+= g[i] * c[i] - g[i-m] * c[i-m]
        res = Math.max(res, cur)
    }

    for (let i = 0; i < n; i++) {
        res+= g[i] ? 0 : c[i]
    }
    return res;
};
 
Java:
class Solution {
    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int max = 0;
        int res =0 ;
        int n = customers.length;
        for(int i=0 ; i<n;i++){
            max+= grumpy[i]==0?customers[i]:0;
        }
        int temp =0;
        for(int i= 0 ; i < minutes;i++){
            temp+= grumpy[i]==1?customers[i]:0;
        }
        
        res = max+temp;
        for(int i =minutes ; i < n;i++){
            temp-=grumpy[i-minutes]==1?customers[i-minutes]:0;
            temp+=grumpy[i]==1?customers[i]:0;
            res = Math.max(max+temp, res);
        }
        return res;
    }
}
 
Java:
class Solution {
    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int max = 0;
        int res =0 ;
        int n = customers.length;
        for(int i=0 ; i<n;i++){
            max+= grumpy[i]==0?customers[i]:0;
        }
        int temp =0;
        for(int i= 0 ; i < minutes;i++){
            temp+= grumpy[i]==1?customers[i]:0;
        }
       
        res = max+temp;
        for(int i =minutes ; i < n;i++){
            temp-=grumpy[i-minutes]==1?customers[i-minutes]:0;
            temp+=grumpy[i]==1?customers[i]:0;
            res = Math.max(max+temp, res);
        }
        return res;
    }
}
fen đặt tên biến như mứt v :doubt:
 
fen đặt tên biến như mứt v :doubt:
Vl clone tự đăng tự chửi :canny: mà trượt 3 lần còn hơi non, trượt 1 lần mới ok
zFNuZTA.gif


via theNEXTvoz for iPhone
 
JavaScript:
var maxSatisfied = function(customers, grumpy, minutes) {
    let res = 0;
    
    // 1st pass to calculate total number of customers satisfied without using secret technique for minutes
    for (let i = 0; i < customers.length; i++) {
        if (grumpy[i] === 0) {
            res += customers[i];
        }
    }

    // 2nd pass to check on grumpy minutes with sliding window to add and maximize
    let l = 0;
    let maxSub = 0;
    let curSub = 0;
    for (let r = 0; r < customers.length; r++) {
        if (grumpy[r] === 1) {
            curSub += customers[r];
        }
        while (r - l + 1 > minutes) {
            curSub -= (grumpy[l] === 1 ? customers[l] : 0);
            l++;
        }

        maxSub = Math.max(maxSub, curSub);
    }

    return res + maxSub;
};
 
Python:
class Solution:
    def maxSatisfied(self, c: List[int], g: List[int], m: int) -> int:   
        max_secret, curr, res =  0 ,0 ,0 
        for i in range(len(c)):
            if g[i] == 0:
                res += c[i]
            else: 
                curr += c[i]
            if i >= m:
                curr -= c[i - m] if g[i - m] == 1 else 0
            max_secret = max(max_secret, curr)
        return res + max_secret
 
Java:
class Solution {
    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int max = 0;
        int res =0 ;
        int n = customers.length;
        for(int i=0 ; i<n;i++){
            max+= grumpy[i]==0?customers[i]:0;
        }
        int temp =0;
        for(int i= 0 ; i < minutes;i++){
            temp+= grumpy[i]==1?customers[i]:0;
        }
       
        res = max+temp;
        for(int i =minutes ; i < n;i++){
            temp-=grumpy[i-minutes]==1?customers[i-minutes]:0;
            temp+=grumpy[i]==1?customers[i]:0;
            res = Math.max(max+temp, res);
        }
        return res;
    }
}

JavaScript:
var maxSatisfied = function(customers, grumpy, minutes) {
    let res = 0;
   
    // 1st pass to calculate total number of customers satisfied without using secret technique for minutes
    for (let i = 0; i < customers.length; i++) {
        if (grumpy[i] === 0) {
            res += customers[i];
        }
    }

    // 2nd pass to check on grumpy minutes with sliding window to add and maximize
    let l = 0;
    let maxSub = 0;
    let curSub = 0;
    for (let r = 0; r < customers.length; r++) {
        if (grumpy[r] === 1) {
            curSub += customers[r];
        }
        while (r - l + 1 > minutes) {
            curSub -= (grumpy[l] === 1 ? customers[l] : 0);
            l++;
        }

        maxSub = Math.max(maxSub, curSub);
    }

    return res + maxSub;
};

Mấy người anh em này cửa bị hỏng rồi, kéo đi kéo lại nhiều lần quá. Bôi thêm dầu để trượt 1 lần thôi cho mượt :D
 
C++:
class Solution {
   public:
    int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) {
        int res = 0, n = grumpy.size();
        for (int i = 0; i < n; ++i) res += customers[i] * (1 - grumpy[i]);

        int tmp = res;
        for (int i = 0; i < n; ++i) {
            if (grumpy[i] == 1) tmp += customers[i];
            if (i >= minutes && grumpy[i - minutes] == 1)
                tmp -= customers[i - minutes];
            res = max(res, tmp);
        }

        return res;
    }
};
 
Cộng sub prefix phần tử của mảng xong + -

Swift:
class Solution {
    func maxSatisfied(_ customers: [Int], _ grumpy: [Int], _ minutes: Int) -> Int {

        var happies = 0
        var alls = 0

        var sumHappy:[Int] = [happies]
        var sumAll:[Int] = [alls]

        for (index, customer) in customers.enumerated() {
            let isHappy = grumpy[index] == 0
            if isHappy {
                happies += customer
            }
            alls += customer
            sumHappy.append(happies)
            sumAll.append(alls)
        }

        var result = 0
        let lastHappy = sumHappy.last!
        for index in minutes..<sumHappy.count {
            var current = lastHappy
            current -= sumHappy[index] - sumHappy[index - minutes]
            current += sumAll[index] - sumAll[index - minutes]

            result = max(result, current)
        }

        return result
    }
}
 
Python:
class Solution:
    def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
        satisfied = 0
        maxSatisfiable = 0
        curSatisfiable = 0
        for right in range(len(customers)):
            satisfied += (grumpy[right] ^ 1) * customers[right]
            curSatisfiable += grumpy[right] * customers[right]
            if right >= minutes:
                curSatisfiable -= grumpy[right - minutes] * customers[right - minutes]
            maxSatisfiable = max(maxSatisfiable, curSatisfiable)
        
        return satisfied + maxSatisfiable
 
Code rác, làm cho đủ ngày.

Java:
class Solution {
    fun maxSatisfied(customers: IntArray, grumpy: IntArray, minutes: Int): Int {
        var s = customers.foldIndexed(0
        ) { i, v, a -> if (i < minutes || grumpy[i] == 0) (v + customers[i]) else v }

        var m = s
        for (i in minutes..<customers.size) {
            val l = i - minutes
            if (grumpy[l] == 1) {
                s -= customers[l]
            }
            if (grumpy[i] == 1) {
                s += customers[i]
            }

            if (m < s) m = s
        }

        return m
    }
}
 
vẫn bình dị ước mơ được làm Võ Tòng đấm sếp :(


Code:
impl Solution {
    pub fn max_satisfied(customers: Vec<i32>, grumpy: Vec<i32>, minutes: i32) -> i32 {
        let happy_count =
            customers.iter().zip(grumpy.iter()).
                fold(0, |mut acc, (&c, &g)| {
                    if g == 0 {
                        acc += c;
                    }

                    acc
                });

        let mut lo = 0;
        let mut window_count = 0;
        let mut max_window_count = 0;
        let um = minutes as usize;
        let n = customers.len();

        for i in 0..n {
            if grumpy[i] == 1 {
                window_count += customers[i];
            }

            if i - lo + 1 == um {
                max_window_count = max_window_count.max(window_count);

                if grumpy[lo] == 1 {
                    window_count -= customers[lo];
                }

                lo += 1;
            }
        }

        happy_count + max_window_count
    }
}
 
Java:
class Solution {
    public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int n = customers.length;
        int satisfied = 0;
        int notSatisfiedInMinutes = 0;
        int maxNotsatisfiedInMinutes = 0;

        for (int i = 0; i < n; i++) {
            satisfied += (grumpy[i] == 0? customers[i] : 0);
        }

        for (int i = 0; i <= n - minutes; i++) {
            if (i == 0) {
                for (int j = 0; j < minutes; j++) {
                    notSatisfiedInMinutes += grumpy[j] * customers[j];
                }
            } else {
                notSatisfiedInMinutes += (grumpy[i + minutes - 1] * customers[i + minutes - 1] - grumpy[i - 1] * customers[i - 1]);
            }

            maxNotsatisfiedInMinutes = Math.max(maxNotsatisfiedInMinutes, notSatisfiedInMinutes);
        }

        return satisfied + maxNotsatisfiedInMinutes;
    }
}
 
Back
Top