Skip to content

Commit ff8773e

Browse files
committed
2
1 parent 4ec491a commit ff8773e

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
252252
#### [349. Intersection of Two Arrays](https://github.com/hitzzc/go-leetcode/tree/master/intersection_of_two_arrays)
253253
#### [350. Intersection of Two Arrays II](https://github.com/hitzzc/go-leetcode/tree/master/intersection_of_two_arrays_II)
254254
#### [354. Russian Doll Envelopes](https://github.com/hitzzc/go-leetcode/tree/master/russian_doll_envelopes)
255+
#### [367. Valid Perfect Square](https://github.com/hitzzc/go-leetcode/tree/master/valid_perfect_square)
256+
#### [368. Largest Divisible Subset](https://github.com/hitzzc/go-leetcode/tree/master/largest_divisible_subset)
255257

256258

257259

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<int> largestDivisibleSubset(vector<int>& nums) {
4+
if (nums.size() == 0) return {};
5+
sort(nums.begin(), nums.end());
6+
vector<int> dp(nums.size(), 1), pre(nums.size());
7+
for (int i = 0; i < pre.size(); ++i) {
8+
pre[i] = i;
9+
}
10+
int max_v = 1, k = 0;
11+
for (int i = 1; i < dp.size(); ++i) {
12+
for (int j = i-1; j >= 0; --j) {
13+
if (nums[i] % nums[j] != 0) continue;
14+
if (dp[i] < dp[j]+1) {
15+
pre[i] = j;
16+
dp[i] = dp[j]+1;
17+
}
18+
if (dp[i] > max_v) {
19+
max_v = dp[i];
20+
k = i;
21+
}
22+
}
23+
}
24+
vector<int> result;
25+
while (k != pre[k]) {
26+
result.push_back(nums[k]);
27+
k = pre[k];
28+
}
29+
result.push_back(nums[k]);
30+
return result;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool isPerfectSquare(int num) {
4+
long i = 1, j = num;
5+
while (i <= j) {
6+
long mid = i + (j-i)/2;
7+
if (mid*mid == num) return true;
8+
if (mid*mid < num) {
9+
i = mid + 1;
10+
}else {
11+
j = mid - 1;
12+
}
13+
}
14+
return false;
15+
}
16+
};

0 commit comments

Comments
 (0)