Merge branch 'master' of https://github.com/xiongraorao/Interview-Notebook
This commit is contained in:
commit
f134d10ad8
69
code/src/main/java/com/raorao/interview/beike/Main.java
Normal file
69
code/src/main/java/com/raorao/interview/beike/Main.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package com.raorao.interview.beike;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* .
|
||||||
|
*
|
||||||
|
* @author Xiong Raorao
|
||||||
|
* @since 2018-08-18-19:53
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
String line1 = scanner.nextLine();
|
||||||
|
String[] items = line1.split(" ");
|
||||||
|
int n = Integer.parseInt(items[0]);
|
||||||
|
int p1 = Integer.parseInt(items[1]);
|
||||||
|
int p2 = Integer.parseInt(items[2]);
|
||||||
|
int p3 = Integer.parseInt(items[3]);
|
||||||
|
int t1 = Integer.parseInt(items[4]);
|
||||||
|
int t2 = Integer.parseInt(items[5]);
|
||||||
|
Interval[] intervals = new Interval[n];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
String[] temp = scanner.nextLine().split(" ");
|
||||||
|
intervals[i] = new Interval(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]));
|
||||||
|
}
|
||||||
|
System.out.println(compute(n, p1, p2, p3, t1, t2, intervals));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int compute(int n, int p1, int p2, int p3, int t1, int t2, Interval[] intervals) {
|
||||||
|
int count = 0;
|
||||||
|
Arrays.sort(intervals, Comparator.comparingInt(e -> e.end));
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
count += p1 * (intervals[i].end - intervals[i].start);
|
||||||
|
if (i < n - 1) {
|
||||||
|
count += intevalSum(p1, p2, p3,t1, t2, intervals[i + 1].start - intervals[i].end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int intevalSum(int p1, int p2, int p3, int t1, int t2, int interval) {
|
||||||
|
if (interval <= 0) {
|
||||||
|
return 0;
|
||||||
|
} else if (interval <= t1) {
|
||||||
|
return p1 * interval;
|
||||||
|
} else if (interval <= t1 + t2) {
|
||||||
|
return p1 * t1 + p2 * (interval - t1);
|
||||||
|
} else {
|
||||||
|
return p1 * t1 + p2 * t2 + p3 * (interval - t1 - t2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Interval {
|
||||||
|
|
||||||
|
int end;
|
||||||
|
private int start;
|
||||||
|
|
||||||
|
public Interval(int start, int end) {
|
||||||
|
this.start = start;
|
||||||
|
this.end = end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
code/src/main/java/com/raorao/interview/beike/t2/Main.java
Normal file
30
code/src/main/java/com/raorao/interview/beike/t2/Main.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package com.raorao.interview.beike.t2;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* .
|
||||||
|
*
|
||||||
|
* @author Xiong Raorao
|
||||||
|
* @since 2018-08-18-20:35
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int n = Integer.parseInt(scanner.nextLine());
|
||||||
|
String[] line = scanner.nextLine().split(" ");
|
||||||
|
int[] data = new int[n];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
data[i] = Integer.parseInt(line[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
for (int j = 0; j < 5; j++) {
|
||||||
|
count += Math.abs(data[i] - data[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(count);
|
||||||
|
}
|
||||||
|
}
|
37
code/src/main/java/com/raorao/interview/beike/t3/Main.java
Normal file
37
code/src/main/java/com/raorao/interview/beike/t3/Main.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package com.raorao.interview.beike.t3;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* .
|
||||||
|
*
|
||||||
|
* @author Xiong Raorao
|
||||||
|
* @since 2018-08-18-20:54
|
||||||
|
*/
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String test = "8K67A65K27T59K346AK2";
|
||||||
|
// out = 4;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* /*
|
||||||
|
* 样例解释
|
||||||
|
* 4轮出牌顺序为:
|
||||||
|
* A234567
|
||||||
|
* 56789T
|
||||||
|
* KKKKA2
|
||||||
|
* 6
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
|
||||||
|
public static void process(String input) {
|
||||||
|
char[] chars = input.toCharArray();
|
||||||
|
Map<String, Integer> map = new HashMap<>();
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,10 @@
|
|||||||
package com.raorao.interview.bytedance;
|
package com.raorao.interview.bytedance;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* .
|
* .
|
||||||
*
|
*
|
||||||
@ -8,4 +13,9 @@ package com.raorao.interview.bytedance;
|
|||||||
*/
|
*/
|
||||||
public class Test {
|
public class Test {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ArrayList a;
|
||||||
|
LinkedList b;
|
||||||
|
Vector v;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,38 @@
|
|||||||
package com.raorao.leetcode.q406;
|
package com.raorao.leetcode.q406;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据身高和序号重组队列.
|
* 根据身高和序号重组队列.
|
||||||
*
|
*
|
||||||
|
* 题目描述:一个学生用两个分量 (h, k) 描述,h 表示身高,k 表示排在前面的有 k 个学生的身高比他高或者和他一样高。\
|
||||||
|
*
|
||||||
|
* 思路:贪心算法,为了在每次插入操作时不影响后续的操作,身高较高的学生应该先做插入操作,否则身高较小的学生原先正确插入第 k 个位置可能会变成第 k+1 个位置。
|
||||||
|
*
|
||||||
|
* 身高降序、k 值升序,然后按排好序的顺序插入队列的第 k 个位置中。
|
||||||
|
*
|
||||||
* @author Xiong Raorao
|
* @author Xiong Raorao
|
||||||
* @since 2018-08-17-15:22
|
* @since 2018-08-17-15:22
|
||||||
*/
|
*/
|
||||||
public class Solution {
|
public class Solution {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
int[][] input = new int[][] {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[][] reconstructQueue(int[][] people) {
|
public static int[][] reconstructQueue(int[][] people) {
|
||||||
return null;
|
if (people == null || people.length == 0 || people[0].length == 0) {
|
||||||
|
return new int[0][0];
|
||||||
|
}
|
||||||
|
Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]); //升序排列就是小的放前面,大的放后面;
|
||||||
|
List<int[]> queue = new ArrayList<>();
|
||||||
|
for (int[] p : people) {
|
||||||
|
queue.add(p[1], p);
|
||||||
|
}
|
||||||
|
|
||||||
|
return queue.toArray(new int[queue.size()][]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user