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

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;
    }
}
 
Lúc nào cũng beat 100% nhỉ
Java:
object Solution {
  def maxSatisfied(customers: Array[Int], grumpy: Array[Int], minutes: Int): Int = {
    val n = customers.length
   
    val satisfied = (customers zip grumpy).collect { case (c, g) if g == 0 => c }.sum
    val initialWindowSatisfaction = (0 until minutes).map(i => grumpy(i) * customers(i)).sum
    val maxAdditionalSatisfaction = (1 to (n - minutes)).foldLeft((initialWindowSatisfaction, initialWindowSatisfaction)) {
      case ((currentMax, prevWindowSatisfaction), i) =>
        val newWindowSatisfaction = prevWindowSatisfaction + grumpy(i + minutes - 1) * customers(i + minutes - 1) - grumpy(i - 1) * customers(i - 1)
        (math.max(currentMax, newWindowSatisfaction), newWindowSatisfaction)
    }._1
    satisfied + maxAdditionalSatisfaction
  }
}
 
Python:
class Solution:
    def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
        if len(grumpy) == minutes:
            return sum(customers)

        res = 0
        if grumpy[0] == 0:
            res = customers[0]
      
        count = 0
        for i in range(0, minutes):
            if grumpy[I] == 1:
                count += customers
               dp = [count]
        
        maxAddedCusomters = count

        for i in range(1, len(grumpy) - minutes +1):
            currentDp = dp[i-1]
            if grumpy[i+minutes-1] == 1:
                currentDp += customers[i+minutes-1]

            if grumpy[i-1] == 1:
                currentDp -= customers[i-1]
            
            if grumpy[I] == 0:
                res += customers

               dp.append(currentDp)
            if currentDp > maxAddedCusomters:
                maxAddedCusomters = currentDp
        
        for i in range(len(grumpy) - minutes+1, len(grumpy)):
            if grumpy[I] == 0:
                res += customers

           return res + maxAddedCusomters

code dài quá, k được ngắn như các bác :beat_brick:
 
Last edited:
class Solution:
def maxSatisfied(self, customers: List[int], grumpy: List[int], minutes: int) -> int:
if len(grumpy) == minutes:
return sum(customers)

res = 0
if grumpy[0] == 0:
res = customers[0]

count = 0
for i in range(0, minutes):
if grumpy == 1:
count += customers
dp = [count]

maxAddedCusomters = count

for i in range(1, len(grumpy) - minutes +1):
currentDp = dp[i-1]
if grumpy[i+minutes-1] == 1:
currentDp += customers[i+minutes-1]

if grumpy[i-1] == 1:
currentDp -= customers[i-1]

if grumpy == 0:
res += customers

dp.append(currentDp)
if currentDp > maxAddedCusomters:
maxAddedCusomters = currentDp

for i in range(len(grumpy) - minutes+1, len(grumpy)):
if grumpy == 0:
res += customers

return res + maxAddedCusomters

code dài quá, k được ngắn như các bác :beat_brick:
Nhét thêm vô tag code đi cho đỡ đau mắt fen
 
Đức tiếp
geTVTupq_400x400.jpg
 

Thread statistics

Created
_Gia_Cat_Luong_,
Last reply from
Mắt Xích Yếu,
Replies
17,750
Views
818,179
Back
Top