题
思
今天可以说做的很烂了,只做了第三题。。。
第一题
我一看是图的题,就不想做了,直接跳过,最后一看答案,果然现在的我是做不出来的;
并查集,等回学校花时间了解一下吧;
第二题
我以为第二题又是脑瘫题,这不就一个大根堆的事么,结果超时,大部分时间都在做这题,真的恶心。
其实后来差不多有算差值这个思路了,但是模拟不出来啊,难受,看了下别人的题解,果然都是又臭又长的,真烦诶;
第三题
以前做过,就是一个贪心。我看了下以前的代码,和现在的应该也差不多。我原来是对一个数组排序,然后二分。现在的这版应该比起前的稍微快一点,拍了两次序,然后直接遍历就行了。
代码
第一题
以下代码来自官方题解
class Solution {
public int makeConnected(int n, int[][] connections) {
if (connections.length < n - 1) {
return -1;
}
UnionFind uf = new UnionFind(n);
for (int[] conn : connections) {
uf.unite(conn[0], conn[1]);
}
return uf.setCount - 1;
}
}
// 并查集模板
class UnionFind {
int[] parent;
int[] size;
int n;
// 当前连通分量数目
int setCount;
public UnionFind(int n) {
this.n = n;
this.setCount = n;
this.parent = new int[n];
this.size = new int[n];
Arrays.fill(size, 1);
for (int i = 0; i < n; ++i) {
parent[i] = i;
}
}
public int findset(int x) {
return parent[x] == x ? x : (parent[x] = findset(parent[x]));
}
public boolean unite(int x, int y) {
x = findset(x);
y = findset(y);
if (x == y) {
return false;
}
if (size[x] < size[y]) {
int temp = x;
x = y;
y = temp;
}
parent[y] = x;
size[x] += size[y];
--setCount;
return true;
}
public boolean connected(int x, int y) {
x = findset(x);
y = findset(y);
return x == y;
}
}
第二题
以下代码来自这个老哥的题解
class Solution {
public int maxProfit(int[] inventory, int orders) {
int mode = 1000000007;
int[] narr = new int[inventory.length + 1];
System.arraycopy(inventory, 0, narr, 1, inventory.length);
inventory[0] = 0;
Arrays.sort(narr);
long result = 0;
int remains = orders;
for (int i = narr.length - 1; i > 0 && remains > 0; i--) {
if ((narr[i] - narr[i - 1]) * (narr.length - i) <= remains) {
result += (long) (narr[i] - narr[i - 1]) * (narr[i] + narr[i - 1] + 1) * (narr.length - i) / 2;
remains -= (narr[i] - narr[i - 1]) * (narr.length - i);
} else {
int offset = remains / (narr.length - i);
int k = remains % (narr.length - i);
result += (long) offset * (narr[i] * 2 - offset + 1) * (narr.length - i) / 2;
result += (long) k * (narr[i] - offset);
remains = 0;
}
if (result >= mode) {
result %= mode;
}
}
return (int) result;
}
}
第三题
class Solution {
public int[] advantageCount(int[] A, int[] B) {
int[] copy = new int[A.length];
Arrays.fill(copy, Integer.MIN_VALUE);
TreeMap<Integer, Set<Integer>> map = new TreeMap<Integer, Set<Integer>>();
for (int i = 0; i < B.length; i++) {
if (map.containsKey(B[i])) {
map.get(B[i]).add(i);
} else {
Set<Integer> set = new HashSet<Integer>();
set.add(i);
map.put(B[i], set);
}
}
Arrays.sort(A);
int idx = 0;
Set<Integer> used = new HashSet<Integer>();
flag: for (Integer key : map.keySet()) {
if (idx == A.length) {
break flag;
}
while (A[idx] <= key) {
idx++;
if (idx == A.length) {
break flag;
}
}
for (Integer pos : map.get(key)) {
if (idx < A.length) {
copy[pos] = A[idx];
used.add(idx);
idx++;
} else {
break flag;
}
}
}
idx = 0;
for (int i = 0; i < copy.length; i++) {
if (copy[i] == Integer.MIN_VALUE) {
while (idx < A.length) {
if (used.contains(idx)) {
idx++;
} else {
copy[i] = A[idx];
idx++;
break;
}
}
}
}
return copy;
}
}