-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk296.java
136 lines (110 loc) · 3.59 KB
/
wk296.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package weekly;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class wk296 {
//ranking: 109 / 5721
//简单题,模拟
public int minMaxGame(int[] nums) {
while (nums.length > 1) {
int[] newNum = new int[nums.length / 2];
for (int i = 0; i < newNum.length; i++) {
if (i % 2 == 0) {
newNum[i] = Math.min(nums[2 * i], nums[2 * i + 1]);
} else {
newNum[i] = Math.max(nums[2 * i], nums[2 * i + 1]);
}
}
nums = newNum;
}
return nums[0];
}
//中等题,排序,然后间距k为一组
public int partitionArray(int[] nums, int k) {
Arrays.sort(nums);
int ans = 0;
int pre = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] - nums[pre] <= k) {
continue;
} else {
pre = i;
ans++;
}
}
ans++;
return ans;
}
//中等题,map记录数字的index,保证不会重复
//或者先将operation操作连成一条链,只需要记录头尾两个操作,中间都是无关操作
public int[] arrayChange(int[] nums, int[][] operations) {
Map<Integer, Integer> map = new HashMap<>();
for (int[] operation : operations) {
if (!map.containsKey(operation[0])) {
map.put(operation[1], operation[0]);
} else {
Integer begin = map.get(operation[0]);
map.remove(operation[0]);
map.put(operation[1], begin);
}
}
Map<Integer, Integer> mmap = new HashMap<>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
mmap.put(entry.getValue(), entry.getKey());
}
for (int i = 0; i < nums.length; i++) {
if (mmap.containsKey(nums[i])) {
nums[i] = mmap.get(nums[i]);
}
}
return nums;
}
//map保存num,index
/* public int[] arrayChange(int[] nums, int[][] operations) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int[] operation : operations) {
Integer index = map.get(operation[0]);
nums[index] = operation[1];
map.remove(operation[0]);
map.put(operation[1], index);
}
return nums;
}
*/
//困难题,直接用StringBuilder模拟,没想到一次就过了..
//还可以用双向链表、对顶栈等
class TextEditor {
StringBuilder sb = new StringBuilder();
int cur = 0;
public TextEditor() {
}
public void addText(String text) {
sb.insert(cur, text);
cur += text.length();
}
public int deleteText(int k) {
int ans = Math.min(k, cur);
int begin = cur - ans;
sb.delete(begin, begin + ans);
cur -= ans;
return ans;
}
public String cursorLeft(int k) {
int ans = Math.min(k, cur);
cur -= ans;
ans = Math.min(cur, 10);
int begin = cur - ans;
return sb.substring(begin, begin + ans);
}
public String cursorRight(int k) {
int ans = Math.min(k, sb.length() - cur);
cur += ans;
ans = Math.min(cur, 10);
int begin = cur - ans;
return sb.substring(begin, begin + ans);
}
}
}