Skip to content

Commit

Permalink
0904 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 16, 2018
1 parent 9817dfa commit fa41b64
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
7 changes: 7 additions & 0 deletions 0904-Fruit-Into-Baskets/cpp-0904/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(B)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main2.cpp)
add_executable(B ${SOURCE_FILES})
65 changes: 65 additions & 0 deletions 0904-Fruit-Into-Baskets/cpp-0904/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/// Source : https://leetcode.com/problems/fruit-into-baskets/description/
/// Author : liuyubobobo
/// Time : 2018-09-15

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;


/// Segment the array into blocks
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int totalFruit(vector<int>& tree) {

vector<pair<int, int>> blocks;
int start = 0;
for(int i = start + 1; i <= tree.size(); i ++)
if(i == tree.size() || tree[i] != tree[start]){
blocks.push_back(make_pair(tree[start], i - start));
start = i;
i = start;
}
// for(const pair<int, int>& p: blocks)
// cout << "(" << p.first << "," << p.second << ") ";
// cout << endl;

int res = 0;
unordered_set<int> fruits;
int sum = 0;
for(int i = 0; i <= blocks.size();)
if(i == blocks.size() || (fruits.size() == 2 && fruits.count(blocks[i].first) == 0)){
res = max(res, sum);

if(i < blocks.size()){
sum = 0;
i --;
fruits.clear();
}
else
break;
}
else{
fruits.insert(blocks[i].first);
sum += blocks[i].second;
i ++;
}
return res;
}
};


int main() {

vector<int> nums1 = {1, 2, 1};
cout << Solution().totalFruit(nums1) << endl;

vector<int> nums2 = {0, 1, 2, 2};
cout << Solution().totalFruit(nums2) << endl;

return 0;
}
52 changes: 52 additions & 0 deletions 0904-Fruit-Into-Baskets/cpp-0904/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/// Source : https://leetcode.com/problems/fruit-into-baskets/description/
/// Author : liuyubobobo
/// Time : 2018-09-15

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;


/// Sliding Window
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int totalFruit(vector<int>& tree) {

unordered_map<int, int> freq;
int l = 0, r = -1, res = 0;
while(l < tree.size()){
if(freq.size() <= 2 && r + 1 < tree.size()){
r ++;
freq[tree[r]] ++;
}
else{
freq[tree[l]] --;
if(freq[tree[l]] == 0)
freq.erase(tree[l]);
l ++;
}

if(freq.size() <= 2)
res = max(res, getFruits(freq));
}
return res;
}

private:
int getFruits(const unordered_map<int, int>& freq){
int res = 0;
for(const pair<int, int>& p: freq)
res += p.second;
return res;
}
};


int main() {

return 0;
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,6 @@ email: [[email protected]](mailto:[email protected])
| 901 | [Online Stock Span](https://leetcode.com/problems/online-stock-span/description/) | [solution](https://leetcode.com/problems/online-stock-span/solution/) | [C++](0901-Online-Stock-Span/cpp-0901/) | | |
| 902 | [Numbers At Most N Given Digit Set](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/description/) | [solution](https://leetcode.com/problems/numbers-at-most-n-given-digit-set/solution/) | [C++](0902-Numbers-At-Most-N-Given-Digit-Set/cpp-0902/) | | |
| 903 | [Valid Permutations for DI Sequence](https://leetcode.com/problems/valid-permutations-for-di-sequence/description/) | [solution](https://leetcode.com/problems/valid-permutations-for-di-sequence/solution/)<br/>[缺:O(n^2) DP] | [C++](0903-Valid-Permutations-for-DI-Sequence/cpp-0903/) | | |
| | | | | | |
| 904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/description/) | [solution](https://leetcode.com/problems/fruit-into-baskets/solution/) | [C++](0904-Fruit-Into-Baskets/cpp-0904/) | | |
| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/description/) | [solution](https://leetcode.com/problems/sort-array-by-parity/solution/) | [C++](0905-Sort-Array-By-Parity/cpp-0905/) | | |
| | | | | | |

0 comments on commit fa41b64

Please sign in to comment.