Skip to content

Commit 8377ff0

Browse files
committed
rename
1 parent dbf6c19 commit 8377ff0

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

318.maximum_product_of_word_length.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@ Program: maximum product of word lengths
44
Description:
55
66
Date: 2016-08-29 16:31:13
7-
Last modified: 2016-08-29 16:31:48
7+
Last modified: 2016-08-29 16:49:38
88
GCC version: 4.9.3
99
*****************************************/
1010

11+
// The idea is using a value vector to store a bit representation, which represents the characters of each word in origin vector
1112
#include <vector>
13+
#include <string>
14+
using namespace std;
1215

1316
class Solution {
1417
public:
1518
int maxProduct(vector<string>& words) {
16-
19+
vector<int> value(words.size(), 0);
20+
for(int i = 0; i < words.size(); ++i)
21+
for(auto c: words[i])
22+
value[i] |= 1 << (c - 'a');
23+
24+
int ret = 0;
25+
for(int i = 1; i < words.size(); ++i)
26+
for(int j = 0; j < i; ++j)
27+
if((value[i] & value[j]) == 0 && words[i].length() * words[j].length() > ret)
28+
ret = words[i].length() * words[j].length();
29+
return ret;
1730
}
1831
};

391.perfect_rectangle.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//coding:utf-8
2+
/**********************************************
3+
Program: Perfect Rectangle
4+
Description:
5+
6+
Date: 2016-08-29 09:32:37
7+
Last modified: 2016-08-29 10:11:33
8+
**********************************************/
9+
#include <vector>
10+
#include <algorithm>
11+
#include <iostream>
12+
using namespace std;
13+
14+
void print(vector<vector<int>> vec) {
15+
for(auto v: vec) {
16+
for(auto i: v)
17+
cout << i << " ";
18+
cout << endl;
19+
}
20+
}
21+
22+
class Solution {
23+
bool static compare(vector<int> v1, vector<int> v2) {
24+
return v1[0] <= v2[0] && v1[1] <= v2[1];
25+
}
26+
27+
int calc_area(vector<int>& vec) {
28+
return (vec[2] - vec[0]) * (vec[3] - vec[1]);
29+
}
30+
31+
bool overlap(vector<int> v1, vector<int> v2) {
32+
return v2[1] < v1[3] && v2[0] < v1[3];
33+
}
34+
35+
public:
36+
37+
bool isRectangleCover(vector<vector<int>>& rectangles) {
38+
sort(rectangles.begin(), rectangles.end(), compare);
39+
print(rectangles);
40+
int area = calc_area(rectangles[0]);
41+
for(int i = 1; i < rectangles.size(); ++i) {
42+
if(overlap(rectangles[i - 1], rectangles[i]))
43+
return false;
44+
area += calc_area(rectangles[i]);
45+
}
46+
vector<int> bigone{rectangles[0][0], rectangles[0][1], rectangles[rectangles.size() - 1][2], rectangles[rectangles.size() - 1][3]};
47+
return area == calc_area(bigone);
48+
}
49+
50+
};
51+
52+
int main() {
53+
vector<vector<int>> vec{{1, 1, 3, 3}, {3, 1, 4, 2}, {3, 2, 4, 4}, {1, 3, 2, 4}, {2, 3, 3, 4}};
54+
Solution s;
55+
s.isRectangleCover(vec);
56+
}

0 commit comments

Comments
 (0)