今天非酋脸(第51名)
不过把这几个题可以存一下
1.最大非负子序和
给定一个整数数组 A,找到一个具有最大和的连续子数组(子数组最少包含一个元素,并且 每个元素都必须是非负整数),返回其最大和
public class Solution {
/**
* @param A: an integer array
* @return: return maxium contiguous non-negative subarray sum
*/
public int maxNonNegativeSubArray(int[] A) {
// write your code here
int n = A.length;
int lastIndexSubArraySum = -1;
if(A[0]>=0) {
lastIndexSubArraySum = A[0];
}
int maxSubArraySumAnswer = -1;
maxSubArraySumAnswer = lastIndexSubArraySum;
for(int i = 1; i < n; i++) {
int nowIndexSubArraySum = -1;
if(A[i] >=0) {
if(lastIndexSubArraySum == -1){
nowIndexSubArraySum = A[i];
}
else{
nowIndexSubArraySum = lastIndexSubArraySum +A[i];
}
}
maxSubArraySumAnswer = Math.max(maxSubArraySumAnswer, nowIndexSubArraySum);
lastIndexSubArraySum = nowIndexSubArraySum;
}
return maxSubArraySumAnswer;
}
}
2.第二直径
public class Solution {
class Edge {
int to;
int value;
int next;
public Edge(int to, int value, int next) {
this.to = to;
this.value = value;
this.next = next;
}
}
void bfs(int begin, int n, long[]distance, Edge []pointEdge, int []head) {
Queue<Integer> queue = new LinkedList<Integer>();
boolean []visited = new boolean[n];
for(int i = 0; i < n; i++) {
visited[i] = false;
}
visited[begin] = true;
queue.add(begin);
distance[begin] = 0L;
while(!queue.isEmpty()) {
int now = queue.poll();
long nowValue = distance[now];
for(int i = head[now]; i != 0; i = pointEdge[i].next) {
int to = pointEdge[i].to;
int value = pointEdge[i].value;
if(!visited[to]) {
visited[to] = true;
distance[to] = value + nowValue;
queue.add(to);
}
}
}
}
int findMaxDistanceIndex(long[]distance) {
long maxDistance = 0;
int index = 0;
int size = distance.length;
for(int i = 0; i < size; i++) {
if(distance[i] > maxDistance) {
maxDistance = distance[i];
index = i;
}
}
return index;
}
/**
* @param edge: edge[i][0] [1] [2] start point,end point,value
* @return: return the second diameter length of the tree
*/
public long getSecondDiameter(int[][] edge) {
int edgeNumber = edge.length;
int n = edgeNumber + 1;
long []distance = new long[n];
long []distanceOne = new long[n];
long []distanceTwo = new long[n];
int []head = new int[n];
Edge []pointEdge = new Edge [2 * n];
int edgeCount = 0;
for(int i = 0; i < edgeNumber; i++) {
++edgeCount;
pointEdge[edgeCount] = new Edge(edge[i][1], edge[i][2], head[edge[i][0]]);
head[edge[i][0]] = edgeCount;
++edgeCount;
pointEdge[edgeCount] = new Edge(edge[i][0], edge[i][2], head[edge[i][1]]);
head[edge[i][1]] = edgeCount;
}
bfs(0, n, distance, pointEdge, head);
int diameterFirstPointIndex = findMaxDistanceIndex(distance);
bfs(diameterFirstPointIndex, n, distanceOne, pointEdge, head);
int diameterSecondPointIndex = findMaxDistanceIndex(distanceOne);
bfs(diameterSecondPointIndex, n, distanceTwo, pointEdge, head);
long secondDiameter = 0;
for(int i = 0; i < n; i++) {
if(i != diameterFirstPointIndex && i != diameterSecondPointIndex) {
secondDiameter = Math.max(secondDiameter, Math.max(distanceOne[i], distanceTwo[i]));
}
}
return secondDiameter;
}
}
3.排序方案
public class Solution {
/**
* @param nums: the array of elements to be inserted.
* @return: return the least operation number.
*/
public long sortedArrangement(int[] nums) {
int n = nums.length;
// 树状数组
int[] binaryIndexTree = new int[n + 1];
long result = 0;
for (int i = 0; i < n; i++) {
int number = nums[i];
// 先获取前缀中比当前数小的个数
int prefixSmallerCount = query(number - 1, binaryIndexTree);
// 计算前缀中比当前数大的个数
int prefixLargerCount = i - prefixSmallerCount;
// 计算结果,采取更小的方案
result += 2 * Math.min(prefixSmallerCount, prefixLargerCount) + 1;
// 将当前数也加入树状数组中, 在number位置上+1,代表number出现过
update(number, 1, binaryIndexTree);
}
return result;
}
public int lowbit(int x) {
return x & (-x);
}
// 更新position位置的值,使其加上value
void update(int position, int value, int[] binaryIndexTree) {
while (position < binaryIndexTree.length) {
binaryIndexTree[position] += value;
position += lowbit(position);
}
}
// 查询[1, position]的所有值的和
int query(int position, int[] binaryIndexTree) {
int prefixSum = 0;
while (position > 0) {
prefixSum += binaryIndexTree[position];
position -= lowbit(position);
}
return prefixSum;
}
}
4.f粉刷天花板,这个没有搞出来(看见的朋友会的话可以私我或者评论什么的帮忙补充一下)