Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungLandlock LSM: Sandbox-Linux ohne Root-Rechte sichern(22.09.2026 um 02:00 Uhr)
Sichere ProgrammierungQuarkus + GraalVM Advanced Obfuscation(22.09.2026 um 02:00 Uhr)
Sichere Programmierung06 — Chat Works. Does the Agent Actually Retrieve Memory?(22.09.2026 um 02:03 Uhr)
Sichere ProgrammierungMCP Debate: Token Tax, Context Bloat, and What Devs Can Do(22.09.2026 um 02:03 Uhr)
Sichere ProgrammierungI gave my AI agent a kill switch tied to its own bank balance(22.09.2026 um 02:08 Uhr)
Sichere ProgrammierungLandlock LSM: Sandbox-Linux ohne Root-Rechte sichern(22.09.2026 um 02:00 Uhr)
Sichere ProgrammierungQuarkus + GraalVM Advanced Obfuscation(22.09.2026 um 02:00 Uhr)
Sichere Programmierung06 — Chat Works. Does the Agent Actually Retrieve Memory?(22.09.2026 um 02:03 Uhr)
Sichere ProgrammierungMCP Debate: Token Tax, Context Bloat, and What Devs Can Do(22.09.2026 um 02:03 Uhr)
Sichere ProgrammierungI gave my AI agent a kill switch tied to its own bank balance(22.09.2026 um 02:08 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Sliding Window in Action — Solving “Grumpy Bookstore Owner” Step by Step

Sliding Window makes sense only when you see it working in a real problem. This problem is a great example of how to separate logic and avoid recalculation. Problem Idea We are given: c[i] → number of customers at minute i…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!



Sliding Window makes sense only when you see it working in a real problem.

This problem is a great example of how to separate logic and avoid recalculation.





Problem Idea



We are given:




c[i] → number of customers at minute i
g[i] → owner’s mood






0 → not grumpy (customers already happy)

1 → grumpy (customers unhappy)



The owner can use a secret technique for minutes time to make grumpy customers happy.



Goal:



Maximize the total number of happy customers.






Key Observation



Split the solution into two independent parts:



1) Base Happiness



Customers who are already happy (g[i] == 0)

→ These are always counted.



2) Extra Happiness (Sliding Window)



Customers who are grumpy (g[i] == 1)

→ We use a sliding window to choose the best time interval.



Final Answer:



Base + Maximum Window Gain






C++ Implementation






#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
vector<int> c = {1,0,1,2,1,1,7,5};
vector<int> g = {0,1,0,1,0,1,0,1};

int windowSize = 3;
int base = 0, winSum = 0, maxSum = 0;

// Step 1: Calculate base happiness
for (int i = 0; i < g.size(); ++i) {
if (g[i] == 0)
base += c[i];
}

// Step 2: First window
for (int i = 0; i < windowSize; ++i) {
if (g[i] == 1)
winSum += c[i];
}

maxSum = winSum;

// Step 3: Slide the window
for (int i = windowSize; i < g.size(); ++i) {
if (g[i - windowSize] == 1)
winSum -= c[i - windowSize];

if (g[i] == 1)
winSum += c[i];

maxSum = max(maxSum, winSum);
}

cout << base + maxSum;
return 0;
}









Sliding Window Logic (Plain English)




  • Calculate the first window

  • Slide right by one step

  • Remove the left element

  • Add the new right element

  • Update the maximum extra gain

  • Only grumpy minutes are handled inside the window.






Complexity



Time O(N)

Space O(1)



Efficient and interview-ready.






What This Problem Teaches



Sliding window avoids full recalculation. Not all elements are equal — filter first. Fixed-size windows are predictable and clean logic becomes simple when you separate concerns






Final Thought



Sliding window isn’t about moving pointers randomly. It’s about controlling change:




  1. what leaves

  2. what enters

  3. what stays constant



Is there any other method to solve the Grumpy Bookstore Owner problem apart from this approach? Please leave it in the comments. 👇

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Sliding Window in Action — Solving “Grumpy Bookstore Owner” Step by Step

Thematisch verwandte Begriffe: Sliding, Window, Action, Solving · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-49449 | Joplin is an open source note-taking and to-do application that organise…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick