Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bb0d262

Browse files
authored
feat: add solutions to lc problem: No.3020 (doocs#2274)
No.3020.Find the Maximum Number of Elements in Subset
1 parent db256f1 commit bb0d262

File tree

6 files changed

+378
-6
lines changed

6 files changed

+378
-6
lines changed

solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README.md

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,142 @@
5050
<!-- tabs:start -->
5151

5252
```python
53+
class Solution:
54+
def maximumLength(self, nums: List[int]) -> int:
55+
d = {}
56+
for num in sorted(nums)[::-1]:
57+
if num**2 in d and num in d and num != 1:
58+
d[num] = d[num**2] + 2
59+
else:
60+
d[num] = 1
61+
ones = nums.count(1)
62+
return max(max(d.values()), ones - (ones % 2 == 0))
5363

5464
```
5565

5666
```java
57-
67+
class Solution {
68+
public int maximumLength(int[] nums) {
69+
TreeMap<Integer, Integer> map = new TreeMap<>();
70+
for (int i : nums) {
71+
map.put(i, map.getOrDefault(i, 0) + 1);
72+
}
73+
int max = 0;
74+
75+
for (Map.Entry<Integer, Integer> i : map.entrySet()) {
76+
System.out.println(i.getValue());
77+
if (i.getValue() >= 2 && i.getKey() != 1) {
78+
int x = i.getKey();
79+
int c = 2;
80+
while (map.containsKey(x * x)) {
81+
if (map.get(x * x) == 1) {
82+
max = Math.max(max, c + 1);
83+
break;
84+
} else if (map.get(x * x) >= 2) {
85+
max = Math.max(max, c + 1);
86+
x = x * x;
87+
}
88+
c += 2;
89+
}
90+
}
91+
}
92+
if (map.containsKey(1) && map.get(1) - 1 > max) {
93+
return (map.get(1) % 2 != 0) ? map.get(1) : map.get(1) - 1;
94+
}
95+
return max == 0 ? 1 : max;
96+
}
97+
}
5898
```
5999

60100
```cpp
61-
101+
class Solution {
102+
public:
103+
int maximumLength(vector<int>& nums) {
104+
long long ans = 0;
105+
map<int, int> freq;
106+
for (auto n : nums) {
107+
freq[n]++;
108+
}
109+
for (auto [k, f] : freq) {
110+
long long t = k, count = 0;
111+
if (t == 1) {
112+
count += freq[t];
113+
freq[t] = 0;
114+
}
115+
while (t < INT_MAX && freq[t] > 0) {
116+
count += 2;
117+
if (freq[t] == 1) {
118+
break;
119+
}
120+
freq[t] = 0;
121+
t = t * t;
122+
}
123+
if (count % 2 == 0) {
124+
count--;
125+
}
126+
ans = max(ans, count);
127+
}
128+
return ans;
129+
}
130+
};
62131
```
63132
64133
```go
65-
134+
func minExp(x, c int) (int, int) {
135+
d := math.Sqrt(float64(x))
136+
if d < 2 || float64(int(d)) < d {
137+
return x, c
138+
}
139+
return minExp(int(d), c+1)
140+
}
141+
func maximumLength(nums []int) int {
142+
m := make(map[int][]int)
143+
for i := range nums {
144+
base, c := minExp(nums[i], 1)
145+
m[base] = append(m[base], c)
146+
}
147+
max := 1
148+
for _, v := range m {
149+
v := matchPattern(v)
150+
max = Max(max, v)
151+
}
152+
_, ok := m[1]
153+
if ok {
154+
if len(m[1])%2 > 0 {
155+
max = Max(max, len(m[1]))
156+
} else {
157+
max = Max(max, len(m[1])-1)
158+
}
159+
}
160+
return max
161+
}
162+
func Max(i, j int) int {
163+
if i > j {
164+
return i
165+
}
166+
return j
167+
}
168+
func matchPattern(arr []int) int {
169+
sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
170+
start := arr[0]
171+
bin := 2
172+
for i := range arr {
173+
if bin == 0 {
174+
start++
175+
bin = 2
176+
}
177+
if arr[i] == start {
178+
bin--
179+
}
180+
}
181+
if bin == 1 {
182+
return 2*(start-arr[0]) + 1
183+
} else if bin == 0 {
184+
return 2*(start-arr[0]) + 1
185+
} else {
186+
return 2*(start-arr[0]) - 1
187+
}
188+
}
66189
```
67190

68191
<!-- tabs:end -->

solution/3000-3099/3020.Find the Maximum Number of Elements in Subset/README_EN.md

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,142 @@
4646
<!-- tabs:start -->
4747

4848
```python
49+
class Solution:
50+
def maximumLength(self, nums: List[int]) -> int:
51+
d = {}
52+
for num in sorted(nums)[::-1]:
53+
if num**2 in d and num in d and num != 1:
54+
d[num] = d[num**2] + 2
55+
else:
56+
d[num] = 1
57+
ones = nums.count(1)
58+
return max(max(d.values()), ones - (ones % 2 == 0))
4959

5060
```
5161

5262
```java
53-
63+
class Solution {
64+
public int maximumLength(int[] nums) {
65+
TreeMap<Integer, Integer> map = new TreeMap<>();
66+
for (int i : nums) {
67+
map.put(i, map.getOrDefault(i, 0) + 1);
68+
}
69+
int max = 0;
70+
71+
for (Map.Entry<Integer, Integer> i : map.entrySet()) {
72+
System.out.println(i.getValue());
73+
if (i.getValue() >= 2 && i.getKey() != 1) {
74+
int x = i.getKey();
75+
int c = 2;
76+
while (map.containsKey(x * x)) {
77+
if (map.get(x * x) == 1) {
78+
max = Math.max(max, c + 1);
79+
break;
80+
} else if (map.get(x * x) >= 2) {
81+
max = Math.max(max, c + 1);
82+
x = x * x;
83+
}
84+
c += 2;
85+
}
86+
}
87+
}
88+
if (map.containsKey(1) && map.get(1) - 1 > max) {
89+
return (map.get(1) % 2 != 0) ? map.get(1) : map.get(1) - 1;
90+
}
91+
return max == 0 ? 1 : max;
92+
}
93+
}
5494
```
5595

5696
```cpp
57-
97+
class Solution {
98+
public:
99+
int maximumLength(vector<int>& nums) {
100+
long long ans = 0;
101+
map<int, int> freq;
102+
for (auto n : nums) {
103+
freq[n]++;
104+
}
105+
for (auto [k, f] : freq) {
106+
long long t = k, count = 0;
107+
if (t == 1) {
108+
count += freq[t];
109+
freq[t] = 0;
110+
}
111+
while (t < INT_MAX && freq[t] > 0) {
112+
count += 2;
113+
if (freq[t] == 1) {
114+
break;
115+
}
116+
freq[t] = 0;
117+
t = t * t;
118+
}
119+
if (count % 2 == 0) {
120+
count--;
121+
}
122+
ans = max(ans, count);
123+
}
124+
return ans;
125+
}
126+
};
58127
```
59128
60129
```go
61-
130+
func minExp(x, c int) (int, int) {
131+
d := math.Sqrt(float64(x))
132+
if d < 2 || float64(int(d)) < d {
133+
return x, c
134+
}
135+
return minExp(int(d), c+1)
136+
}
137+
func maximumLength(nums []int) int {
138+
m := make(map[int][]int)
139+
for i := range nums {
140+
base, c := minExp(nums[i], 1)
141+
m[base] = append(m[base], c)
142+
}
143+
max := 1
144+
for _, v := range m {
145+
v := matchPattern(v)
146+
max = Max(max, v)
147+
}
148+
_, ok := m[1]
149+
if ok {
150+
if len(m[1])%2 > 0 {
151+
max = Max(max, len(m[1]))
152+
} else {
153+
max = Max(max, len(m[1])-1)
154+
}
155+
}
156+
return max
157+
}
158+
func Max(i, j int) int {
159+
if i > j {
160+
return i
161+
}
162+
return j
163+
}
164+
func matchPattern(arr []int) int {
165+
sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
166+
start := arr[0]
167+
bin := 2
168+
for i := range arr {
169+
if bin == 0 {
170+
start++
171+
bin = 2
172+
}
173+
if arr[i] == start {
174+
bin--
175+
}
176+
}
177+
if bin == 1 {
178+
return 2*(start-arr[0]) + 1
179+
} else if bin == 0 {
180+
return 2*(start-arr[0]) + 1
181+
} else {
182+
return 2*(start-arr[0]) - 1
183+
}
184+
}
62185
```
63186

64187
<!-- tabs:end -->
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int maximumLength(vector<int>& nums) {
4+
long long ans = 0;
5+
map<int, int> freq;
6+
for (auto n : nums) {
7+
freq[n]++;
8+
}
9+
for (auto [k, f] : freq) {
10+
long long t = k, count = 0;
11+
if (t == 1) {
12+
count += freq[t];
13+
freq[t] = 0;
14+
}
15+
while (t < INT_MAX && freq[t] > 0) {
16+
count += 2;
17+
if (freq[t] == 1) {
18+
break;
19+
}
20+
freq[t] = 0;
21+
t = t * t;
22+
}
23+
if (count % 2 == 0) {
24+
count--;
25+
}
26+
ans = max(ans, count);
27+
}
28+
return ans;
29+
}
30+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
func minExp(x, c int) (int, int) {
2+
d := math.Sqrt(float64(x))
3+
if d < 2 || float64(int(d)) < d {
4+
return x, c
5+
}
6+
return minExp(int(d), c+1)
7+
}
8+
func maximumLength(nums []int) int {
9+
m := make(map[int][]int)
10+
for i := range nums {
11+
base, c := minExp(nums[i], 1)
12+
m[base] = append(m[base], c)
13+
}
14+
max := 1
15+
for _, v := range m {
16+
v := matchPattern(v)
17+
max = Max(max, v)
18+
}
19+
_, ok := m[1]
20+
if ok {
21+
if len(m[1])%2 > 0 {
22+
max = Max(max, len(m[1]))
23+
} else {
24+
max = Max(max, len(m[1])-1)
25+
}
26+
}
27+
return max
28+
}
29+
func Max(i, j int) int {
30+
if i > j {
31+
return i
32+
}
33+
return j
34+
}
35+
func matchPattern(arr []int) int {
36+
sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
37+
start := arr[0]
38+
bin := 2
39+
for i := range arr {
40+
if bin == 0 {
41+
start++
42+
bin = 2
43+
}
44+
if arr[i] == start {
45+
bin--
46+
}
47+
}
48+
if bin == 1 {
49+
return 2*(start-arr[0]) + 1
50+
} else if bin == 0 {
51+
return 2*(start-arr[0]) + 1
52+
} else {
53+
return 2*(start-arr[0]) - 1
54+
}
55+
}

0 commit comments

Comments
 (0)