{ "cells": [ { "cell_type": "markdown", "source": [ "Today, the bookstore owner has a store open for `customers.length` minutes.\n", "Every minute, some number of customers (`customers[i]`) enter the store, and\n", "all those customers leave after the end of that minute.\n", "\n", "On some minutes, the bookstore owner is grumpy. If the bookstore owner is\n", "grumpy on the i-th minute, `grumpy[i] = 1`, otherwise `grumpy[i] = 0`. When\n", "the bookstore owner is grumpy, the customers of that minute are not satisfied,\n", "otherwise they are satisfied.\n", "\n", "The bookstore owner knows a secret technique to keep themselves not grumpy for\n", "`x` minutes straight, but can only use it once.\n", "\n", "Return the maximum number of customers that can be satisfied throughout the\n", "day.\n", "\n", "\n", "\n", "**Example 1:**\n", "\n", "\n", "\n", " Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], x = 3\n", " Output: 16\n", " Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.\n", " The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.\n", "\n", "\n", "\n", "\n", "**Note:**\n", "\n", " * `1 <= x <= customers.length == grumpy.length <= 20000`\n", " * `0 <= customers[i] <= 1000`\n", " * `0 <= grumpy[i] <= 1`" ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "max_satisfied (generic function with 1 method)" }, "metadata": {}, "execution_count": 1 } ], "cell_type": "code", "source": [ "# @lc code=start\n", "using LeetCode\n", "\n", "\n", "function max_satisfied(customers::Vector{Int}, grumpy::Vector{Int}, x::Int)\n", " n = length(customers)\n", " satisfied = sum(customers[1:x])\n", "\n", " for i = (x+1):n\n", " (grumpy[i] == 0) && (satisfied += customers[i])\n", " end\n", "\n", " current_satisfied = satisfied\n", " left, right = 1, x + 1\n", " while right <= n\n", " (grumpy[left] == 1) && (current_satisfied -= customers[left])\n", " (grumpy[right] == 1) && (current_satisfied += customers[right])\n", "\n", " satisfied = max(current_satisfied, satisfied)\n", " left, right = left + 1, right + 1\n", " end\n", "\n", " satisfied\n", "end\n", "\n", "# @lc code=end" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.1" }, "kernelspec": { "name": "julia-1.10", "display_name": "Julia 1.10.1", "language": "julia" } }, "nbformat": 4 }