<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-eqawh8zrd" -->

---
title: How I Solved LeetCode 904: Fruit Into Baskets Using...
description: A walkthrough of solving LeetCode 904 (Fruit Into Baskets) by reframing it as finding the longest contiguous subarray with at most two distinct values. The...
canonical: https://daily.dev/posts/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-eqawh8zrd
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How I Solved LeetCode 904: Fruit Into Baskets Using Sliding Window | daily.dev
og:description: A walkthrough of solving LeetCode 904 (Fruit Into Baskets) by reframing it as finding the longest contiguous subarray with at most two distinct values. The...
og:url: https://daily.dev/posts/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-eqawh8zrd
og:image: https://api.daily.dev/og/posts/eqawh8ZRD.png
og:image:alt: How I Solved LeetCode 904: Fruit Into Baskets Using Sliding Window
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How I Solved LeetCode 904: Fruit Into Baskets Using Sliding Window

**[Medium](https://daily.dev/sources/medium_js)** · 8 min read · 0 upvotes · 0 comments

## Summary

A walkthrough of solving LeetCode 904 (Fruit Into Baskets) by reframing it as finding the longest contiguous subarray with at most two distinct values. The post covers the sliding window pattern step by step in Python, explaining pointer mechanics, why a dictionary is needed to track frequencies, common mistakes (using if vs while, forgetting to delete zero-count keys, off-by-one errors), and why the solution runs in O(n) time with O(1) space.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@everythingguy007/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-1cafcd7a6ee8>

## Similar posts on daily.dev

- [Medium](https://daily.dev/posts/medium-tjownsknq) · Medium · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#algorithms](https://daily.dev/tags/algorithms)

[View this post on daily.dev](https://daily.dev/posts/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-eqawh8zrd)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How I Solved LeetCode 904: Fruit Into Baskets Using Sliding Window","url":"https://daily.dev/posts/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-eqawh8zrd","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-eqawh8zrd"},"datePublished":"2026-07-26T04:56:51.388Z","dateModified":"2026-07-26T04:58:08.532Z","description":"A walkthrough of solving LeetCode 904 (Fruit Into Baskets) by reframing it as finding the longest contiguous subarray with at most two distinct values. The...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/d6ab0a9a223ee01bc82d647f54feb2d2?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/d6ab0a9a223ee01bc82d647f54feb2d2?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-eqawh8zrd","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,algorithms","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"How I Solved LeetCode 904: Fruit Into Baskets Using Sliding Window"}]}
```

