forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problems: No.3309,3310 (doocs#3604)
* No.3309.Maximum Possible Number by Binary Concatenation * No.3310.Remove Methods From Project
- Loading branch information
Showing
14 changed files
with
963 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
solution/3300-3399/3309.Maximum Possible Number by Binary Concatenation/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Solution { | ||
public: | ||
int maxGoodNumber(vector<int>& nums) { | ||
int ans = 0; | ||
auto f = [&](vector<int>& nums) { | ||
int res = 0; | ||
vector<int> t; | ||
for (int x : nums) { | ||
for (; x; x >>= 1) { | ||
t.push_back(x & 1); | ||
} | ||
} | ||
while (t.size()) { | ||
res = res * 2 + t.back(); | ||
t.pop_back(); | ||
} | ||
return res; | ||
}; | ||
for (int i = 0; i < 6; ++i) { | ||
ans = max(ans, f(nums)); | ||
next_permutation(nums.begin(), nums.end()); | ||
} | ||
return ans; | ||
} | ||
}; |
16 changes: 16 additions & 0 deletions
16
solution/3300-3399/3309.Maximum Possible Number by Binary Concatenation/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
func maxGoodNumber(nums []int) int { | ||
f := func(i, j, k int) int { | ||
a := strconv.FormatInt(int64(nums[i]), 2) | ||
b := strconv.FormatInt(int64(nums[j]), 2) | ||
c := strconv.FormatInt(int64(nums[k]), 2) | ||
res, _ := strconv.ParseInt(a+b+c, 2, 64) | ||
return int(res) | ||
} | ||
ans := f(0, 1, 2) | ||
ans = max(ans, f(0, 2, 1)) | ||
ans = max(ans, f(1, 0, 2)) | ||
ans = max(ans, f(1, 2, 0)) | ||
ans = max(ans, f(2, 0, 1)) | ||
ans = max(ans, f(2, 1, 0)) | ||
return ans | ||
} |
21 changes: 21 additions & 0 deletions
21
solution/3300-3399/3309.Maximum Possible Number by Binary Concatenation/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
private int[] nums; | ||
|
||
public int maxGoodNumber(int[] nums) { | ||
this.nums = nums; | ||
int ans = f(0, 1, 2); | ||
ans = Math.max(ans, f(0, 2, 1)); | ||
ans = Math.max(ans, f(1, 0, 2)); | ||
ans = Math.max(ans, f(1, 2, 0)); | ||
ans = Math.max(ans, f(2, 0, 1)); | ||
ans = Math.max(ans, f(2, 1, 0)); | ||
return ans; | ||
} | ||
|
||
private int f(int i, int j, int k) { | ||
String a = Integer.toBinaryString(nums[i]); | ||
String b = Integer.toBinaryString(nums[j]); | ||
String c = Integer.toBinaryString(nums[k]); | ||
return Integer.parseInt(a + b + c, 2); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
solution/3300-3399/3309.Maximum Possible Number by Binary Concatenation/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Solution: | ||
def maxGoodNumber(self, nums: List[int]) -> int: | ||
ans = 0 | ||
for arr in permutations(nums): | ||
num = int("".join(bin(i)[2:] for i in arr), 2) | ||
ans = max(ans, num) | ||
return ans |
18 changes: 18 additions & 0 deletions
18
solution/3300-3399/3309.Maximum Possible Number by Binary Concatenation/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function maxGoodNumber(nums: number[]): number { | ||
const f = (i: number, j: number, k: number): number => { | ||
const a = nums[i].toString(2); | ||
const b = nums[j].toString(2); | ||
const c = nums[k].toString(2); | ||
const res = parseInt(a + b + c, 2); | ||
return res; | ||
}; | ||
|
||
let ans = f(0, 1, 2); | ||
ans = Math.max(ans, f(0, 2, 1)); | ||
ans = Math.max(ans, f(1, 0, 2)); | ||
ans = Math.max(ans, f(1, 2, 0)); | ||
ans = Math.max(ans, f(2, 0, 1)); | ||
ans = Math.max(ans, f(2, 1, 0)); | ||
|
||
return ans; | ||
} |
Oops, something went wrong.