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

No.3020.Find the Maximum Number of Elements in Subset #2274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 63 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
aaf0c8d
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
99ad881
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
7c7155d
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
571d480
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
02111c6
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
4e5fb83
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
cb73474
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
9b50675
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
3a8732e
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
c676f1d
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
0449cce
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
d398420
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 24, 2024
8bfc267
Update Solution.py
yanglbme Jan 25, 2024
2afad64
Update Solution.go
yanglbme Jan 25, 2024
2e72815
Update README.md
yanglbme Jan 25, 2024
c6b8429
Update README_EN.md
yanglbme Jan 25, 2024
f881f46
Update README.md
yanglbme Jan 25, 2024
4f6e88c
Update README_EN.md
yanglbme Jan 25, 2024
f6f1e9b
Update Solution.py
yanglbme Jan 25, 2024
2f90e97
Merge branch 'doocs:main' into main
Nothing-avil Jan 25, 2024
480ad2d
Merge branch 'doocs:main' into main
Nothing-avil Jan 25, 2024
8662bc2
Merge branch 'doocs:main' into main
Nothing-avil Jan 26, 2024
ac79e26
Merge branch 'doocs:main' into main
Nothing-avil Jan 27, 2024
239a06d
Merge branch 'doocs:main' into main
Nothing-avil Jan 28, 2024
da7ffc2
eat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
9ac822e
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
0ca2e46
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
4cf83e9
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
6bb0080
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
39a4095
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
ed0081d
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
e4f5fef
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
ba933be
Delete solution/3000-3099/3019.Number of Changing Keys/README.md
Nothing-avil Jan 28, 2024
2dc22ed
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
f1f58e0
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
ee85f95
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
0a179b7
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
53d9d90
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
e5aa6d2
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
4fe133e
feat: add solutions to lc problem: No.3017
Nothing-avil Jan 28, 2024
8290a13
feat: add solutions to lc problem: No.3019
Nothing-avil Jan 28, 2024
39800ec
Update Solution.go
yanglbme Jan 29, 2024
df235d5
Update README.md
yanglbme Jan 29, 2024
68d03d2
Update README_EN.md
yanglbme Jan 29, 2024
0ca39f8
Merge branch 'doocs:main' into main
Nothing-avil Jan 29, 2024
6b8d642
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
c64ab7e
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
44d826c
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
2a82500
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
9bcac2a
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
31a2ebb
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
f567375
style: format code and docs with prettier
Nothing-avil Jan 29, 2024
0469499
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
b3d3866
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
20587c0
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
56209e1
style: format code and docs with prettier
Nothing-avil Jan 29, 2024
2c4fb11
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
5c1f605
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
370f4c3
feat: add solutions to lc problems: No.3020
Nothing-avil Jan 29, 2024
c8f4d7a
Merge branch 'main' into main
acbin Jan 29, 2024
9153355
Update Solution.go
acbin Jan 29, 2024
9be7654
Update README_EN.md
acbin Jan 29, 2024
ffd25ec
Update README.md
acbin Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,142 @@
<!-- tabs:start -->

```python
class Solution:
def maximumLength(self, nums: List[int]) -> int:
d = {}
for num in sorted(nums)[::-1]:
if num**2 in d and num in d and num != 1:
d[num] = d[num**2] + 2
else:
d[num] = 1
ones = nums.count(1)
return max(max(d.values()), ones - (ones % 2 == 0))

```

```java

class Solution {
public int maximumLength(int[] nums) {
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
int max = 0;

for (Map.Entry<Integer, Integer> i : map.entrySet()) {
System.out.println(i.getValue());
if (i.getValue() >= 2 && i.getKey() != 1) {
int x = i.getKey();
int c = 2;
while (map.containsKey(x * x)) {
if (map.get(x * x) == 1) {
max = Math.max(max, c + 1);
break;
} else if (map.get(x * x) >= 2) {
max = Math.max(max, c + 1);
x = x * x;
}
c += 2;
}
}
}
if (map.containsKey(1) && map.get(1) - 1 > max) {
return (map.get(1) % 2 != 0) ? map.get(1) : map.get(1) - 1;
}
return max == 0 ? 1 : max;
}
}
```

```cpp

class Solution {
public:
int maximumLength(vector<int>& nums) {
long long ans = 0;
map<int, int> freq;
for (auto n : nums) {
freq[n]++;
}
for (auto [k, f] : freq) {
long long t = k, count = 0;
if (t == 1) {
count += freq[t];
freq[t] = 0;
}
while (t < INT_MAX && freq[t] > 0) {
count += 2;
if (freq[t] == 1) {
break;
}
freq[t] = 0;
t = t * t;
}
if (count % 2 == 0) {
count--;
}
ans = max(ans, count);
}
return ans;
}
};
```

```go

func minExp(x, c int) (int, int) {
d := math.Sqrt(float64(x))
if d < 2 || float64(int(d)) < d {
return x, c
}
return minExp(int(d), c+1)
}
func maximumLength(nums []int) int {
m := make(map[int][]int)
for i := range nums {
base, c := minExp(nums[i], 1)
m[base] = append(m[base], c)
}
max := 1
for _, v := range m {
v := matchPattern(v)
max = Max(max, v)
}
_, ok := m[1]
if ok {
if len(m[1])%2 > 0 {
max = Max(max, len(m[1]))
} else {
max = Max(max, len(m[1])-1)
}
}
return max
}
func Max(i, j int) int {
if i > j {
return i
}
return j
}
func matchPattern(arr []int) int {
sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
start := arr[0]
bin := 2
for i := range arr {
if bin == 0 {
start++
bin = 2
}
if arr[i] == start {
bin--
}
}
if bin == 1 {
return 2*(start-arr[0]) + 1
} else if bin == 0 {
return 2*(start-arr[0]) + 1
} else {
return 2*(start-arr[0]) - 1
}
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,142 @@
<!-- tabs:start -->

```python
class Solution:
def maximumLength(self, nums: List[int]) -> int:
d = {}
for num in sorted(nums)[::-1]:
if num**2 in d and num in d and num != 1:
d[num] = d[num**2] + 2
else:
d[num] = 1
ones = nums.count(1)
return max(max(d.values()), ones - (ones % 2 == 0))

```

```java

class Solution {
public int maximumLength(int[] nums) {
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
int max = 0;

for (Map.Entry<Integer, Integer> i : map.entrySet()) {
System.out.println(i.getValue());
if (i.getValue() >= 2 && i.getKey() != 1) {
int x = i.getKey();
int c = 2;
while (map.containsKey(x * x)) {
if (map.get(x * x) == 1) {
max = Math.max(max, c + 1);
break;
} else if (map.get(x * x) >= 2) {
max = Math.max(max, c + 1);
x = x * x;
}
c += 2;
}
}
}
if (map.containsKey(1) && map.get(1) - 1 > max) {
return (map.get(1) % 2 != 0) ? map.get(1) : map.get(1) - 1;
}
return max == 0 ? 1 : max;
}
}
```

```cpp

class Solution {
public:
int maximumLength(vector<int>& nums) {
long long ans = 0;
map<int, int> freq;
for (auto n : nums) {
freq[n]++;
}
for (auto [k, f] : freq) {
long long t = k, count = 0;
if (t == 1) {
count += freq[t];
freq[t] = 0;
}
while (t < INT_MAX && freq[t] > 0) {
count += 2;
if (freq[t] == 1) {
break;
}
freq[t] = 0;
t = t * t;
}
if (count % 2 == 0) {
count--;
}
ans = max(ans, count);
}
return ans;
}
};
```

```go

func minExp(x, c int) (int, int) {
d := math.Sqrt(float64(x))
if d < 2 || float64(int(d)) < d {
return x, c
}
return minExp(int(d), c+1)
}
func maximumLength(nums []int) int {
m := make(map[int][]int)
for i := range nums {
base, c := minExp(nums[i], 1)
m[base] = append(m[base], c)
}
max := 1
for _, v := range m {
v := matchPattern(v)
max = Max(max, v)
}
_, ok := m[1]
if ok {
if len(m[1])%2 > 0 {
max = Max(max, len(m[1]))
} else {
max = Max(max, len(m[1])-1)
}
}
return max
}
func Max(i, j int) int {
if i > j {
return i
}
return j
}
func matchPattern(arr []int) int {
sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
start := arr[0]
bin := 2
for i := range arr {
if bin == 0 {
start++
bin = 2
}
if arr[i] == start {
bin--
}
}
if bin == 1 {
return 2*(start-arr[0]) + 1
} else if bin == 0 {
return 2*(start-arr[0]) + 1
} else {
return 2*(start-arr[0]) - 1
}
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
int maximumLength(vector<int>& nums) {
long long ans = 0;
map<int, int> freq;
for (auto n : nums) {
freq[n]++;
}
for (auto [k, f] : freq) {
long long t = k, count = 0;
if (t == 1) {
count += freq[t];
freq[t] = 0;
}
while (t < INT_MAX && freq[t] > 0) {
count += 2;
if (freq[t] == 1) {
break;
}
freq[t] = 0;
t = t * t;
}
if (count % 2 == 0) {
count--;
}
ans = max(ans, count);
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
func minExp(x, c int) (int, int) {
d := math.Sqrt(float64(x))
if d < 2 || float64(int(d)) < d {
return x, c
}
return minExp(int(d), c+1)
}
func maximumLength(nums []int) int {
m := make(map[int][]int)
for i := range nums {
base, c := minExp(nums[i], 1)
m[base] = append(m[base], c)
}
max := 1
for _, v := range m {
v := matchPattern(v)
max = Max(max, v)
}
_, ok := m[1]
if ok {
if len(m[1])%2 > 0 {
max = Max(max, len(m[1]))
} else {
max = Max(max, len(m[1])-1)
}
}
return max
}
func Max(i, j int) int {
if i > j {
return i
}
return j
}
func matchPattern(arr []int) int {
sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
start := arr[0]
bin := 2
for i := range arr {
if bin == 0 {
start++
bin = 2
}
if arr[i] == start {
bin--
}
}
if bin == 1 {
return 2*(start-arr[0]) + 1
} else if bin == 0 {
return 2*(start-arr[0]) + 1
} else {
return 2*(start-arr[0]) - 1
}
}
Loading