这次周赛排名:6750 / 12542
疲惫。这次的赛感觉是真的难,也看到评论里说的了,但是Lee215寒神的评论是异常简单。第二题就卡住在大二位数上面了。学到了BigInteger的用法。第三题更是没有思路。
第一题 Q1403
Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence.
If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.
public List<Integer> minSubsequence(int[] nums) {
int n = nums.length;
List<Integer> res = new ArrayList<>();
Arrays.sort(nums);
int total = 0;
for (int a : nums)
total += a;
int temp = 0;
int left = total;
for (int i = n - 1; i >= 0; i--) {
res.add(nums[i]);
temp += nums[i];
left -= nums[i];
if (temp > left)
return res;
}
return res;
}
> 第二题 Q1404 > Given a number s in their binary representation. Return the number of steps to reduce it to 1 under the following rules: > If the current number is even, you have to divide it by 2. If the current number is odd, you have to add 1 to it.
import java.math.BigInteger;
class Solution {
public int numSteps(String s) {
BigInteger i = new BigInteger(s, 2);
int res = 0;
while (!BigInteger.ONE.equals(i)) {
if (i.testBit(0))
i = i.add(BigInteger.ONE); // add 1
else
i = i.shiftRight(1); // divide 2
res++;
}
return res;
}
}
这个题学到了BIgInteger。当然这个可以用位操作的方法。
第三题第四题有空回来再补吧。累了。
第四题是minimax的题。看起来很有意思。