训练营周赛2
This commit is contained in:
parent
118eef14fb
commit
4f0d6cdc0b
20
src/main/java/com/code/leet/contest2/Solution1.java
Normal file
20
src/main/java/com/code/leet/contest2/Solution1.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.code.leet.contest2;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Solution1 {
|
||||
public static void main(String[] args) {
|
||||
Solution1 solution = new Solution1();
|
||||
}
|
||||
|
||||
public int minimumWaitingTime(int[] w) {
|
||||
int[] wait = new int[w.length];
|
||||
Arrays.sort(w);
|
||||
int sum = 0;
|
||||
for (int i = 1; i < w.length; i++) {
|
||||
wait[i] = wait[i - 1] + w[i - 1];
|
||||
sum += wait[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
27
src/main/java/com/code/leet/contest2/Solution2.java
Normal file
27
src/main/java/com/code/leet/contest2/Solution2.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.code.leet.contest2;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Solution2 {
|
||||
public static void main(String[] args) {
|
||||
Solution2 solution = new Solution2();
|
||||
}
|
||||
|
||||
public int countOccurrences(int[] nums, int target) {
|
||||
int start = 0;
|
||||
int end = nums.length - 1;
|
||||
while (start <= end) {
|
||||
if (nums[start] == target && nums[end] == target) {
|
||||
return end - start + 1;
|
||||
} else if (nums[start] == target && nums[end] > target) {
|
||||
end--;
|
||||
} else if (nums[end] == target && nums[start] < target) {
|
||||
start++;
|
||||
} else {
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
43
src/main/java/com/code/leet/contest2/Solution3.java
Normal file
43
src/main/java/com/code/leet/contest2/Solution3.java
Normal file
@ -0,0 +1,43 @@
|
||||
package com.code.leet.contest2;
|
||||
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Solution3 {
|
||||
public static void main(String[] args) {
|
||||
Solution3 solution = new Solution3();
|
||||
solution.previousPermutation(new int[]{3, 0, 4, 2, 1});
|
||||
}
|
||||
|
||||
public int[] previousPermutation(int[] permutation) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
int i = permutation.length - 2;
|
||||
list.add(permutation[permutation.length - 1]);
|
||||
for (; i >= 0; i--) {
|
||||
list.add(permutation[i]);
|
||||
if (permutation[i] > permutation[i + 1]) {
|
||||
for (int j = 0; j < list.size(); j++) {
|
||||
if (list.get(j) < permutation[i]) {
|
||||
int temp = list.get(j);
|
||||
list.set(j, permutation[i]);
|
||||
permutation[i] = temp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == -1) {
|
||||
for (int j = 0; j < permutation.length; j++) {
|
||||
permutation[j] = list.get(j);
|
||||
}
|
||||
} else {
|
||||
for (int j = 0; j < permutation.length - 1 - i; j++) {
|
||||
permutation[i + j + 1] = list.get(j);
|
||||
}
|
||||
}
|
||||
return permutation;
|
||||
}
|
||||
}
|
90
src/main/java/com/code/leet/contest2/Solution4.java
Normal file
90
src/main/java/com/code/leet/contest2/Solution4.java
Normal file
@ -0,0 +1,90 @@
|
||||
package com.code.leet.contest2;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Solution4 {
|
||||
public static void main(String[] args) {
|
||||
Solution4 solution = new Solution4();
|
||||
System.out.println(solution.maximumValue(new int[]{1, 4, 5, 8, 2}, new int[]{3, 5, 4}));
|
||||
}
|
||||
|
||||
public int maximumValue(int[] nums, int[] arr) {
|
||||
// Arrays.sort(nums);
|
||||
// Map<Integer, int[]> map = new HashMap<>();
|
||||
// for (int i = 0; i < nums.length; i++) {
|
||||
// int num = nums[i];
|
||||
// int[] indexs = map.getOrDefault(num, new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE});
|
||||
// indexs[0] = Math.min(indexs[0], i);
|
||||
// indexs[1] = Math.min(indexs[1], nums.length - 1 - i);
|
||||
// map.put(num, indexs);
|
||||
// }
|
||||
//
|
||||
// long result = 0;
|
||||
// for (int key : map.keySet()) {
|
||||
// int[] indexs = map.get(key);
|
||||
// result = Math.max(result, (long) indexs[0] * indexs[1]);
|
||||
// }
|
||||
// return (int) (result % 100000007);
|
||||
Arrays.sort(nums);
|
||||
int size = nums.length;
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if (size % 2 == 0) {
|
||||
list.add(nums[size / 2 - 1]);
|
||||
}
|
||||
list.add(nums[size / 2]);
|
||||
int min = Integer.MAX_VALUE;
|
||||
int index = 0;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int sub = Integer.MAX_VALUE;
|
||||
for (int mid : list) {
|
||||
sub = Math.min(Math.abs(arr[i] - mid), sub);
|
||||
}
|
||||
if (sub < min) {
|
||||
min = sub;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
long result;
|
||||
int mid = size / 2;
|
||||
if (size % 2 == 1) {
|
||||
if (arr[index] < nums[mid]) {
|
||||
mid--;
|
||||
while (arr[index] <= nums[mid]) {
|
||||
if(arr[index] == nums[mid]){
|
||||
size--;
|
||||
}
|
||||
mid--;
|
||||
}
|
||||
} else if (arr[index] >= nums[mid]) {
|
||||
if(arr[index] == nums[mid]){
|
||||
size--;
|
||||
}
|
||||
mid++;
|
||||
while (arr[index] >= nums[mid]) {
|
||||
if(arr[index] == nums[mid]){
|
||||
size--;
|
||||
}
|
||||
mid++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int left = mid - 1;
|
||||
int right = mid;
|
||||
int count = 0;
|
||||
while (arr[index] <= nums[left] || arr[index] >= nums[right]) {
|
||||
if(arr[index] == nums[left]){
|
||||
size--;
|
||||
}
|
||||
if(arr[index] == nums[right]){
|
||||
size--;
|
||||
}
|
||||
left--;
|
||||
right++;
|
||||
count++;
|
||||
}
|
||||
mid = mid - 1 - count;
|
||||
}
|
||||
result = (long) mid * (size - mid);
|
||||
return (int) (result % 100000007);
|
||||
}
|
||||
}
|
34
src/main/java/com/code/leet/contest2/Solution5.java
Normal file
34
src/main/java/com/code/leet/contest2/Solution5.java
Normal file
@ -0,0 +1,34 @@
|
||||
package com.code.leet.contest2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Solution5 {
|
||||
public static void main(String[] args) {
|
||||
Solution5 solution = new Solution5();
|
||||
solution.maximumScore(2, new int[][]{{4, 3}, {2, 1}, {0, 5}, {4, 1}});
|
||||
}
|
||||
|
||||
private int max = -1;
|
||||
|
||||
public int maximumScore(int atk, int[][] monsters) {
|
||||
attack(atk, monsters, 0, new ArrayList<>());
|
||||
return max;
|
||||
}
|
||||
|
||||
private void attack(int atk, int[][] monsters, int score, List<Integer> use) {
|
||||
if (use.size() == monsters.length) {
|
||||
max = Math.max(max, score);
|
||||
}
|
||||
for (int i = 0; i < monsters.length; i++) {
|
||||
if (use.contains(i)) {
|
||||
continue;
|
||||
}
|
||||
if (monsters[i][0] <= atk) {
|
||||
use.add(i);
|
||||
attack(atk + monsters[i][1], monsters, score + atk - monsters[i][0], use);
|
||||
use.remove(use.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
36
src/main/java/com/code/leet/contest2/Solution6.java
Normal file
36
src/main/java/com/code/leet/contest2/Solution6.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.code.leet.contest2;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Solution6 {
|
||||
public static void main(String[] args) {
|
||||
Solution6 solution = new Solution6();
|
||||
solution.solve(new int[]{-3, -1, 2, -3, -2, -5}, new int[][]{{2, 5}, {1, 1}, {2, 3}, {4, 5}, {0, 5}});
|
||||
}
|
||||
|
||||
public int[] solve(int[] nums, int[][] queries) {
|
||||
// int[][] dp = new int[nums.length][nums.length];
|
||||
// for (int i = 0; i < nums.length; i++) {
|
||||
// for (int j = i; j < nums.length; j++) {
|
||||
// if(i==j){
|
||||
// dp[i][j] = nums[j];
|
||||
// }else{
|
||||
// dp[i][j] = Math.max(dp[i][j-1],nums[j]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// int[] result = new int[queries.length];
|
||||
// for (int i = 0; i < queries.length; i++) {
|
||||
// result[i] = dp[queries[i][0]][queries[i][1]];
|
||||
// }
|
||||
// return result;
|
||||
int[] result = new int[queries.length];
|
||||
for (int i = 0; i < queries.length; i++) {
|
||||
int[] arrays = Arrays.copyOfRange(nums, queries[i][0], queries[i][1] + 1);
|
||||
Arrays.sort(arrays);
|
||||
result[i] = arrays[arrays.length - 1];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
57
src/main/java/leetcode/editor/cn/SingleNumberIii.java
Normal file
57
src/main/java/leetcode/editor/cn/SingleNumberIii.java
Normal file
@ -0,0 +1,57 @@
|
||||
//给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。你可以按 任意顺序 返回答案。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1,2,1,3,2,5]
|
||||
//输出:[3,5]
|
||||
//解释:[5, 3] 也是有效的答案。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums = [-1,0]
|
||||
//输出:[-1,0]
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:nums = [0,1]
|
||||
//输出:[1,0]
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 2 <= nums.length <= 3 * 104
|
||||
// -231 <= nums[i] <= 231 - 1
|
||||
// 除两个只出现一次的整数外,nums 中的其他数字都出现两次
|
||||
//
|
||||
// Related Topics 位运算 数组
|
||||
// 👍 416 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
//260:只出现一次的数字 III
|
||||
class SingleNumberIii{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new SingleNumberIii().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] singleNumber(int[] nums) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
38
src/main/java/leetcode/editor/cn/SingleNumberIii.md
Normal file
38
src/main/java/leetcode/editor/cn/SingleNumberIii.md
Normal file
@ -0,0 +1,38 @@
|
||||
<p>给定一个整数数组 <code>nums</code>,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。你可以按 <strong>任意顺序</strong> 返回答案。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,3,2,5]
|
||||
<strong>输出:</strong>[3,5]
|
||||
<strong>解释:</strong>[5, 3] 也是有效的答案。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-1,0]
|
||||
<strong>输出:</strong>[-1,0]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,1]
|
||||
<strong>输出:</strong>[1,0]
|
||||
</pre>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
<li>除两个只出现一次的整数外,<code>nums</code> 中的其他数字都出现两次</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>位运算</li><li>数组</li></div></div>\n<div><li>👍 416</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user