Skip to content

Commit e2d924b

Browse files
committed
leetcode-biweek-52
1 parent 19026ac commit e2d924b

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// AC:
2+
// Runtime: 5 ms, faster than 100.00% of Java online submissions for Sorting the Sentence.
3+
// Memory Usage: 38.5 MB, less than 100.00% of Java online submissions for Sorting the Sentence.
4+
// Using tree map to sort key.
5+
// T:O(n), S:O(n), n = len(s)
6+
//
7+
class Solution {
8+
public String sortSentence(String s) {
9+
TreeMap<Integer, String> record = new TreeMap<>();
10+
String[] sArr = s.split(" ");
11+
for (String item: sArr) {
12+
Integer index = Integer.parseInt(String.valueOf(item.charAt(item.length() - 1)));
13+
record.put(index, item.substring(0, item.length() - 1));
14+
}
15+
StringBuilder ret = new StringBuilder();
16+
for (Integer i: record.keySet()) {
17+
ret.append(record.get(i));
18+
ret.append(" ");
19+
}
20+
return ret.substring(0, ret.length() - 1);
21+
}
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// AC:
2+
// Runtime: 8 ms, faster than 100.00% of Java online submissions for Incremental Memory Leak.
3+
// Memory Usage: 38.3 MB, less than 100.00% of Java online submissions for Incremental Memory Leak.
4+
// simulation... it seems that brute-force can pass. Any way I haven't thought out better solution...
5+
// complexity: T: O(sqrt(max(m1, m2))), S: O(1)
6+
//
7+
class Solution {
8+
public int[] memLeak(int memory1, int memory2) {
9+
int second = 1;
10+
while (memory1 >= second || memory2 >= second) {
11+
if (memory1 >= memory2) {
12+
if (memory1 >= second) {
13+
memory1 -= second;
14+
} else {
15+
break;
16+
}
17+
} else {
18+
if (memory2 >= second) {
19+
memory2 -= second;
20+
} else {
21+
break;
22+
}
23+
}
24+
second++;
25+
}
26+
int[] ret = new int[]{second, memory1, memory2};
27+
return ret;
28+
}
29+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// AC:
2+
// Runtime: 2056 ms, faster than 50.00% of Java online submissions for Rotating the Box.
3+
// Memory Usage: 64.7 MB, less than 100.00% of Java online submissions for Rotating the Box.
4+
// my thought: using string split(obstacle), and combine every part to make stone fall down.
5+
// complexity: T:O(m * n), S:O(m * n)
6+
//
7+
class Solution {
8+
public char[][] rotateTheBox(char[][] box) {
9+
int row = box.length, col = box[0].length;
10+
char[][] ret = new char[col][row];
11+
List<List<Character>> record = new LinkedList<>();
12+
for (int i = 0; i < row; i++) {
13+
// 逐行下落
14+
List<Character> temp = new LinkedList<>();
15+
StringBuilder tempStr = new StringBuilder();
16+
for (int j = 0; j <= col - 1; j++) {
17+
tempStr.append(box[i][j]);
18+
}
19+
tempStr.append("#");
20+
String[] tempArr = tempStr.toString().split("\\*");
21+
if (tempArr.length == 0) {
22+
for (int t = 0; t < tempStr.length(); t++) {
23+
temp.add('*');
24+
}
25+
} else {
26+
for (String part : tempArr) {
27+
if (part.isEmpty()) {
28+
temp.add('*');
29+
continue;
30+
}
31+
int countEmpty = 0, countStone = 0;
32+
for (char item : part.toCharArray()) {
33+
if (item == '#') {
34+
countStone++;
35+
}
36+
if (item == '.') {
37+
countEmpty++;
38+
}
39+
}
40+
for (int k = 0; k < countEmpty; k++) {
41+
temp.add('.');
42+
}
43+
for (int k = 0; k < countStone; k++) {
44+
temp.add('#');
45+
}
46+
47+
temp.add('*');
48+
}
49+
// 去除末尾 *
50+
if (temp.get(temp.size() - 1) == '*') {
51+
temp.remove(temp.size() - 1);
52+
}
53+
}
54+
temp.remove(temp.size() - 1);
55+
record.add(temp);
56+
}
57+
// rotate
58+
for (int i = 0; i < col; i++) {
59+
for (int j = 0; j < row; j++) {
60+
ret[i][j] = record.get(row - 1 - j).get(i);
61+
}
62+
}
63+
64+
return ret;
65+
}
66+
}

0 commit comments

Comments
 (0)