转眼之间,研一生活快结束了,实验室的师兄也开始找工作了,想参加华为的软挑又什么也不会,挺伤感的,生活真的很艰难,不过再艰难也要努力,加油。
上次参加周赛还是第116次,中间过了快3个月,然后感觉基本不会做题了,规定时间内只做出一道5分的题,超时10分钟做出另一道,写出的代码也简直不能看,贴出来警示自己吧。
leetcode1005题
class Solution {
public int largestSumAfterKNegations(int[] A, int K) {
if(A.length == 1 && (K % 2 == 0))
return A[0];
if(A.length == 1 && (K % 2 != 0))
return -A[0];
Arrays.sort(A);
int cnt = 0;
int res = 0;
for(int a:A)
if(a < 0)
cnt++;
if(cnt == 0){
if(K % 2 == 0){
for(int a:A)
res += a;
return res;
}
else{
for(int i = 1;i<A.length;i++)
res += A[i];
return res - A[0];
}
}
if(cnt > K) {
for(int i = 0;i < K;i++)
res += Math.abs(A[i]);
for(int i = K;i < cnt;i++)
res += A[i];
for(int i = cnt;i < A.length;i++)
res += A[i];
}
else {
for(int i = 0;i < cnt;i++)
res += Math.abs(A[i]);
if((K - cnt) % 2 == 0) {
for(int i = cnt;i < A.length;i++)
res += A[i];
}
else {
for(int i = cnt + 1;i < A.length;i++)
res += A[i];
res = Math.max(res - A[cnt], res + A[cnt] + 2*A[cnt-1]);
}
}
return res;
}
}
leetcode1007题
class Solution {
public int minDominoRotations(int[] A, int[] B) {
int n = A.length;
if(n == 1)
return 0;
int res = 0;
HashMap<Integer, Integer> hashMap = new HashMap<>();
HashSet<Integer> hashSet = new HashSet<>();
boolean flag = false;
for(int a:A) {
if(hashMap.containsKey(a)) {
if(hashMap.get(a) == n)
{
res = a;
flag = true;
}
else {
hashMap.put(a, hashMap.get(a)+1);
}
}
else {
hashMap.put(a, 1);
}
if(flag)
break;
}
if(!flag) {
for(int b:B) {
if(hashMap.containsKey(b)) {
if(hashMap.get(b) == n)
{
res = b;
flag = true;
}
else {
hashMap.put(b, hashMap.get(b)+1);
}
}
else {
hashMap.put(b, 1);
}
if(flag)
break;
}
}
if(!flag)
return -1;
for(int i = 0;i < n;i++) {
if(A[i] == res || B[i] == res)
hashSet.add(i);
}
if(hashSet.size() < n)
return -1;
int cnt1 = 0;
int cnt2 = 0;
for(int i = 0;i < n;i++)
if(A[i] == res)
cnt1++;
for(int i = 0;i < n;i++)
if(B[i] == res)
cnt2++;
return Math.min(Math.min(cnt1, n-cnt1), Math.min(cnt2, n-cnt2));
}
}