{"id":24619417,"url":"https://github.com/emahtab/fruit-into-baskets","last_synced_at":"2025-03-18T23:20:14.353Z","repository":{"id":273093617,"uuid":"870754819","full_name":"eMahtab/fruit-into-baskets","owner":"eMahtab","description":"Find longest continuous sequence length with at most 2 distinct values","archived":false,"fork":false,"pushed_at":"2025-01-18T15:50:58.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-18T16:41:50.900Z","etag":null,"topics":["at-most-2-distinct","leetcode","sliding-window"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-10T15:53:46.000Z","updated_at":"2025-01-18T15:51:00.000Z","dependencies_parsed_at":"2025-01-18T16:41:58.828Z","dependency_job_id":"120ec0d3-a79d-4fb2-b930-adb6b1172641","html_url":"https://github.com/eMahtab/fruit-into-baskets","commit_stats":null,"previous_names":["emahtab/fruit-into-baskets"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffruit-into-baskets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffruit-into-baskets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffruit-into-baskets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffruit-into-baskets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/fruit-into-baskets/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244320429,"owners_count":20434092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["at-most-2-distinct","leetcode","sliding-window"],"created_at":"2025-01-25T00:52:28.872Z","updated_at":"2025-03-18T23:20:14.336Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fruit into baskets\n## https://leetcode.com/problems/fruit-into-baskets\nYou are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces.\n\nYou want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:\n\n1. You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.\n\n2. Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.\n\n3. Once you reach a tree with fruit that cannot fit in your baskets, you must stop.\n\nGiven the integer array fruits, return the maximum number of fruits you can pick.\n```java\nExample 1:\n\nInput: fruits = [1,2,1]\nOutput: 3\nExplanation: We can pick from all 3 trees.\n\nExample 2:\n\nInput: fruits = [0,1,2,2]\nOutput: 3\nExplanation: We can pick from trees [1,2,2].\nIf we had started at the first tree, we would only pick from trees [0,1].\n\nExample 3:\n\nInput: fruits = [1,2,3,2,2]\nOutput: 4\nExplanation: We can pick from trees [2,3,2,2].\nIf we had started at the first tree, we would only pick from trees [1,2].\n```\n\n## Constraints:\n1. 1 \u003c= fruits.length \u003c= 10^5\n2. 0 \u003c= fruits[i] \u003c fruits.length\n\n## Implementation : Sliding Window (Exact same as Longest Substring with at most 2 distinct characters)\n```java\nclass Solution {\n    public int totalFruit(int[] fruits) {\n        if(fruits.length \u003c= 2)\n         return fruits.length;\n\n        HashMap\u003cInteger,Integer\u003e treeTypeToIndexMap = new HashMap\u003c\u003e();\n        int left = 0;\n        int maxFruits = 2;\n        for(int i = 0; i \u003c fruits.length; i++) {\n            treeTypeToIndexMap.put(fruits[i], i);\n            if(treeTypeToIndexMap.size() \u003e 2) {\n                int indexOfTreeTypeToRemove = Collections.min(treeTypeToIndexMap.values());\n                treeTypeToIndexMap.remove(fruits[indexOfTreeTypeToRemove]);\n                left = indexOfTreeTypeToRemove + 1;\n            }\n            int fruitsCollected = i - left + 1;\n            maxFruits = Math.max(maxFruits, fruitsCollected);\n        } \n        return maxFruits;\n    }\n}\n```\n\n## Note :\nIn above implementation, when updating the left pointer, **make sure you are deleting the least recent character.**\n\nIt would be wrong to delete the recent occurrence of character pointed by left pointer, character at left may not be the least recent one.\n\ne.g. {1,2,1,2,1,3,3,3,3,3}, **when seeing value 3, we should delete value 2 (because that was the least recent from 3 values in the map), and we should move the left pointer to point to 1.**\n\nSo answer in this case would be 6.\n\n## References :\nSimilar Problem : https://github.com/eMahtab/longest-substring-with-at-most-two-distinct-characters\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffruit-into-baskets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Ffruit-into-baskets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffruit-into-baskets/lists"}