9.8
This commit is contained in:
parent
5494c1d488
commit
6ebf23bad1
@ -1,5 +1,7 @@
|
|||||||
package com.raorao;
|
package com.raorao;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* .
|
* .
|
||||||
*
|
*
|
||||||
@ -12,6 +14,7 @@ public class Test {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
44
code/src/main/java/com/raorao/interview/netease/t1/Main.java
Normal file
44
code/src/main/java/com/raorao/interview/netease/t1/Main.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package com.raorao.interview.netease.t1;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* .
|
||||||
|
*
|
||||||
|
* @author Xiong Raorao
|
||||||
|
* @since 2018-09-08-15:52
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int t = scanner.nextInt();
|
||||||
|
List<Integer[]> list = new ArrayList<>();
|
||||||
|
for (int i = 0; i < t; i++) {
|
||||||
|
Integer[] ret = compute(scanner.nextInt(), scanner.nextInt());
|
||||||
|
list.add(ret);
|
||||||
|
}
|
||||||
|
for (Integer[] i : list) {
|
||||||
|
System.out.println(i[0] + " " + i[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer[] compute(int n, int k) {
|
||||||
|
Integer[] ret = new Integer[2];
|
||||||
|
ret[0] = 0;
|
||||||
|
ret[1] = 0;
|
||||||
|
if (k <= 1) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
ret[0] = 0;
|
||||||
|
if (k <= n / 2) {
|
||||||
|
ret[1] = k - 1;
|
||||||
|
} else {
|
||||||
|
ret[1] = n - k;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
17
code/src/main/java/com/raorao/interview/netease/t2/Main.java
Normal file
17
code/src/main/java/com/raorao/interview/netease/t2/Main.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.raorao.interview.netease.t2;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* .
|
||||||
|
*
|
||||||
|
* @author Xiong Raorao
|
||||||
|
* @since 2018-09-08-16:54
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
System.out.println(scanner.nextLine().length());
|
||||||
|
}
|
||||||
|
}
|
79
code/src/main/java/com/raorao/interview/netease/t3/Main.java
Normal file
79
code/src/main/java/com/raorao/interview/netease/t3/Main.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package com.raorao.interview.netease.t3;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* .
|
||||||
|
*
|
||||||
|
* @author Xiong Raorao
|
||||||
|
* @since 2018-09-08-16:18
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
private static ArrayList<Vote> votes = new ArrayList<>();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
int m = scanner.nextInt();
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
votes.add(new Vote(scanner.nextInt(), scanner.nextInt()));
|
||||||
|
}
|
||||||
|
|
||||||
|
votes.sort(Comparator.comparingInt(e -> e.yy));
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < votes.size(); i++) {
|
||||||
|
if (check(m)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int x = votes.get(i).xx;
|
||||||
|
int y = votes.get(i).yy;
|
||||||
|
if (x != 1) {
|
||||||
|
x = 1;
|
||||||
|
votes.set(i, new Vote(x, y));
|
||||||
|
count += y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(count);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean check(int m) {
|
||||||
|
Map<Integer, Integer> map = new HashMap<>();
|
||||||
|
for (Vote v : votes) {
|
||||||
|
map.put(v.xx, map.getOrDefault(v.xx, 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int xxMax = Integer.MIN_VALUE;
|
||||||
|
int yyMax = Integer.MIN_VALUE;
|
||||||
|
for (Entry<Integer, Integer> entry : map.entrySet()) {
|
||||||
|
if (entry.getValue() > yyMax) {
|
||||||
|
xxMax = entry.getKey();
|
||||||
|
yyMax = entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (map.entrySet().size() == m) {// 一人一票
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return xxMax == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Vote {
|
||||||
|
|
||||||
|
int xx;
|
||||||
|
int yy;
|
||||||
|
|
||||||
|
public Vote(int xx, int yy) {
|
||||||
|
this.xx = xx;
|
||||||
|
this.yy = yy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vote() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
47
code/src/main/java/com/raorao/sword/MinStack.java
Normal file
47
code/src/main/java/com/raorao/sword/MinStack.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package com.raorao.sword;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 包含Min方法的栈.
|
||||||
|
*
|
||||||
|
* @author Xiong Raorao
|
||||||
|
* @since 2018-09-08-23:26
|
||||||
|
*/
|
||||||
|
public class MinStack {
|
||||||
|
|
||||||
|
private List<Integer> data = new ArrayList<>();
|
||||||
|
|
||||||
|
private List<Integer> min = new ArrayList<>();
|
||||||
|
|
||||||
|
public void push(int node) {
|
||||||
|
data.add(node);
|
||||||
|
if (min.size() == 0) {
|
||||||
|
min.add(0);
|
||||||
|
} else {
|
||||||
|
int m = min();
|
||||||
|
if (node < m) {
|
||||||
|
min.add(data.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pop() {
|
||||||
|
int popIndex = data.size() - 1;
|
||||||
|
int minIndex = min.get(min.size() - 1);
|
||||||
|
if (popIndex == minIndex) {
|
||||||
|
min.remove(min.size() - 1);
|
||||||
|
}
|
||||||
|
data.remove(data.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int top() {
|
||||||
|
return data.get(data.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int min() {
|
||||||
|
int minIndex = min.get(min.size() - 1);
|
||||||
|
return data.get(minIndex);
|
||||||
|
}
|
||||||
|
}
|
31
code/src/main/java/com/raorao/sword/Solution07.java
Normal file
31
code/src/main/java/com/raorao/sword/Solution07.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package com.raorao.sword;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 斐波那契数列.
|
||||||
|
*
|
||||||
|
* @author Xiong Raorao
|
||||||
|
* @since 2018-09-08-22:03
|
||||||
|
*/
|
||||||
|
public class Solution07 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int re = new Solution07().Fibonacci(5);
|
||||||
|
System.out.println(re);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Fibonacci(int n) {
|
||||||
|
int pre = 1;
|
||||||
|
int cur = 1;
|
||||||
|
if (n == 0) {
|
||||||
|
return 0;
|
||||||
|
} else if (n <= 2) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (int i = 3; i <= n; i++) {
|
||||||
|
int temp = pre + cur;
|
||||||
|
pre = cur;
|
||||||
|
cur = temp;
|
||||||
|
}
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ package com.raorao.sword;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* .
|
* .
|
||||||
|
Loading…
x
Reference in New Issue
Block a user