aboutsummaryrefslogtreecommitdiff
path: root/other/fruit_into_baskets.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/fruit_into_baskets.cpp')
-rw-r--r--other/fruit_into_baskets.cpp41
1 files changed, 0 insertions, 41 deletions
diff --git a/other/fruit_into_baskets.cpp b/other/fruit_into_baskets.cpp
deleted file mode 100644
index e613659..0000000
--- a/other/fruit_into_baskets.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
-// 904. Fruit Into Baskets
-#include "leetcode.h"
-
-/*
- * you are visiting a farm that has a single row of fruit trees arranged from
- * left t oright. the trees are represented by an integer array 'fruits' where
- * 'fruits[i]' is the type of fruit the i'th tree produces. you want to collect
- * as much fruit as possible. however, the owner has some strict rules that you
- * must follow: you can only have two baskets and each can only hoold a single
- * type of fruit (unlimited quantity). starting from any tree, you must pick
- * exacty one fruit from every tree while moving to the right. once you reach a
- * tree with fruit that cannot fit in your baskets, you must stop. given the int
- * array 'fruits', return the maximum number of fruits you can pick
- */
-
-class Solution {
-public:
- int totalFruit(vector<int> &fruits) {
- int n = fruits.size(), ans = 0;
- unordered_map<int, int> cnt;
- for (int l = 0, r = 0; r < n; ++r) {
- cnt[fruits[r]]++;
- while (cnt.size() > 2) {
- if (--cnt[fruits[l]] == 0)
- cnt.erase(fruits[l]);
- ++l;
- }
- ans = max(ans, r - l + 1);
- }
- return ans;
- }
-};
-
-int main() {
- Solution obj;
- vector<int> fruits1 = {1, 2, 1}, fruits2 = {0, 1, 2, 2},
- fruits3 = {1, 2, 3, 2, 2};
- cout << obj.totalFruit(fruits1) << endl; // expect: 3
- cout << obj.totalFruit(fruits2) << endl; // expect: 3
- cout << obj.totalFruit(fruits3) << endl; // expect: 4
-}