Skip to content

Commit 6006ac3

Browse files
committed
update
1 parent f8bf19a commit 6006ac3

11 files changed

+271
-28
lines changed

leetcode_solved/[editing]leetcode_0239_Sliding_Window_Maximum.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0295_Find_Median_from_Data_Stream.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

leetcode_solved/[editing]leetcode_0954_Array_of_Doubled_Pairs.cpp

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Solution1: treemap
2+
// AC: Runtime: 700 ms, faster than 5.00% of Java online submissions for Sliding Window Maximum.
3+
// Memory Usage: 128.4 MB, less than 8.80% of Java online submissions for Sliding Window Maximum.
4+
// using treemap of size k, to get the highest element
5+
// T:O(nlogk), S:O(k)
6+
//
7+
class Solution {
8+
public int[] maxSlidingWindow(int[] nums, int k) {
9+
int size = nums.length;
10+
int[] ret = new int[size - k + 1];
11+
12+
TreeMap<Integer, Integer> record = new TreeMap<>(new Comparator<Integer>() {
13+
@Override
14+
public int compare(Integer o1, Integer o2) {
15+
return o2 - o1;
16+
}
17+
});
18+
19+
for (int i = 0; i < k; i++) {
20+
record.merge(nums[i], 1, Integer::sum);
21+
}
22+
ret[0] = record.firstKey();
23+
24+
for (int i = k; i < size; i++) {
25+
int time = record.get(nums[i - k]);
26+
if (time <= 1) {
27+
record.remove(nums[i - k]);
28+
} else {
29+
record.merge(nums[i - k], -1, Integer::sum);
30+
}
31+
record.merge(nums[i], 1, Integer::sum);
32+
33+
ret[i - k + 1] = record.firstKey();
34+
}
35+
36+
return ret;
37+
}
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// AC: Runtime: 210 ms, faster than 9.73% of Java online submissions for Find Median from Data Stream.
2+
// Memory Usage: 123.5 MB, less than 6.09% of Java online submissions for Find Median from Data Stream.
3+
// two heap, one is minimum heap, one is maximum heap, keep the size balance, so the top of the two heap makeup median number.
4+
// add: T:O(logn), find: T:O(1), S:O(n)
5+
//
6+
class MedianFinder {
7+
private PriorityQueue<Integer> record1;
8+
private PriorityQueue<Integer> record2;
9+
private boolean isEvenSize = true;
10+
11+
/** initialize your data structure here. */
12+
public MedianFinder() {
13+
record1 = new PriorityQueue<>(new Comparator<Integer>() {
14+
@Override
15+
public int compare(Integer o1, Integer o2) {
16+
return o2 - o1;
17+
}
18+
});
19+
record2 = new PriorityQueue<>();
20+
}
21+
22+
public void addNum(int num) {
23+
if (isEvenSize) {
24+
record2.offer(num);
25+
record1.offer(record2.poll());
26+
} else {
27+
record1.offer(num);
28+
record2.offer(record1.poll());
29+
}
30+
isEvenSize = !isEvenSize;
31+
}
32+
33+
public double findMedian() {
34+
if (record2.isEmpty()) {
35+
return record1.peek();
36+
}
37+
if (isEvenSize) {
38+
return (record1.peek() + record2.peek()) / 2.0;
39+
} else {
40+
return record1.peek();
41+
}
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// AC: Runtime: 34 ms, faster than 66.99% of Java online submissions for Array of Doubled Pairs.
2+
// Memory Usage: 46.7 MB, less than 63.61% of Java online submissions for Array of Doubled Pairs.
3+
// sort and judge from max, if positive, check max is even and max/2 is never used, is negative, check max * 2 is never used.
4+
// T:O(nlogn), S:O(n)
5+
//
6+
class Solution {
7+
public boolean canReorderDoubled(int[] arr) {
8+
int size = arr.length;
9+
Arrays.sort(arr);
10+
11+
HashMap<Integer, List<Integer>> valueToIndex = new HashMap<>();
12+
int[] used = new int[size];
13+
for (int i = 0; i < size; i++) {
14+
if (!valueToIndex.containsKey(arr[i])) {
15+
List<Integer> tempList = new LinkedList<>();
16+
tempList.add(i);
17+
valueToIndex.put(arr[i], tempList);
18+
} else {
19+
valueToIndex.get(arr[i]).add(i);
20+
}
21+
}
22+
23+
for (int i = size - 1; i >= 0; i--) {
24+
if (used[i] == 1) {
25+
continue;
26+
}
27+
if (arr[i] >= 0) {
28+
if (arr[i] % 2 == 1 || !valueToIndex.containsKey(arr[i] / 2) || valueToIndex.get(arr[i] / 2).size() == 0) {
29+
return false;
30+
}
31+
List<Integer> tempList = valueToIndex.get(arr[i] / 2);
32+
int index = tempList.get(tempList.size() - 1);
33+
tempList.remove(tempList.size() - 1);
34+
used[index] = 1;
35+
used[i] = 1;
36+
} else {
37+
if (!valueToIndex.containsKey(arr[i] * 2) || valueToIndex.get(arr[i] * 2).size() == 0) {
38+
return false;
39+
}
40+
List<Integer> tempList = valueToIndex.get(arr[i] * 2);
41+
int index = tempList.get(tempList.size() - 1);
42+
tempList.remove(tempList.size() - 1);
43+
used[index] = 1;
44+
used[i] = 1;
45+
}
46+
}
47+
48+
return true;
49+
}
50+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for The kth Factor of n.
2+
// Memory Usage: 35.9 MB, less than 55.27% of Java online submissions for The kth Factor of n.
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int kthFactor(int n, int k) {
8+
int temp = 1, count = 0, step = 1;
9+
if (n % 2 == 1) {
10+
step = 2;
11+
}
12+
while (temp <= n) {
13+
if (n % temp == 0) {
14+
count++;
15+
if (count == k) {
16+
return temp;
17+
}
18+
}
19+
temp += step;
20+
}
21+
22+
return -1;
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int numOfStrings(String[] patterns, String word) {
3+
int ret = 0;
4+
for (String str: patterns) {
5+
if (word.contains(str)) {
6+
ret++;
7+
}
8+
}
9+
10+
return ret;
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int[] rearrangeArray(int[] nums) {
3+
int size = nums.length;
4+
List<Integer> temp = new ArrayList<>(size);
5+
for (int i: nums) {
6+
temp.add(i);
7+
}
8+
while (!check(temp)) {
9+
Collections.shuffle(temp);
10+
}
11+
int[] ret = new int[size];
12+
for (int i = 0; i < temp.size(); i++) {
13+
ret[i] = temp.get(i);
14+
}
15+
16+
return ret;
17+
}
18+
19+
private boolean check(List<Integer> nums) {
20+
for (int i = 1; i < nums.size() - 1; i++) {
21+
if (nums.get(i) * 2 == nums.get(i - 1) + nums.get(i + 1)) {
22+
return false;
23+
}
24+
}
25+
26+
return true;
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Minimum Non-Zero Product of the Array Elements.
2+
// Memory Usage: 35.4 MB, less than 100.00% of Java online submissions for Minimum Non-Zero Product of the Array Elements.
3+
// 思路:观察规律可以猜测,最小乘积时,除了 2^p - 1 外,剩下的最大值和最小值两两配对,形成 2^p - 2, 共 (2^p - 2) / 2 对。再计算 pow(x, y), 递归计算,复杂度降为 O(n), n 为原指数
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int minNonZeroProduct(int p) {
8+
long mod = 1000_000_000 + 7, divider = (1L << p) - 1;
9+
long powResult = myPow(divider - 1, (divider - 1) / 2, mod);
10+
int ret = (int) (((divider % mod) * powResult) % mod);
11+
return ret;
12+
}
13+
14+
private long myPow(long x, long y, long mod) {
15+
if (y == 0) {
16+
return 1;
17+
}
18+
long temp = myPow(x, y / 2, mod);
19+
long p = (temp * temp) % mod;
20+
21+
return y % 2 == 1 ? (p * (x % mod)) % mod : p;
22+
}
23+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// AC: Runtime: 81 ms, faster than 33.33% of Java online submissions for Last Day Where You Can Still Cross.
2+
// Memory Usage: 48 MB, less than 100.00% of Java online submissions for Last Day Where You Can Still Cross.
3+
// binary search & bfs search of graph
4+
// T:(nlogn), S:O(n), n = row * col
5+
//
6+
class Solution {
7+
public int latestDayToCross(int row, int col, int[][] cells) {
8+
int left = 0, right = row * col - 1, ret = -1;
9+
while (left <= right) {
10+
int mid = (left + right) / 2;
11+
if (check(cells, row, col, mid)) {
12+
ret = mid;
13+
left = mid + 1;
14+
} else {
15+
right = mid - 1;
16+
}
17+
}
18+
19+
return ret;
20+
}
21+
22+
private boolean check(int[][] cells, int row, int col, int day) {
23+
int[][] grid = new int[row][col];
24+
for (int i = 0; i < day; i++) {
25+
grid[cells[i][0] - 1][cells[i][1] - 1] = 1;
26+
}
27+
Queue<int[]> bfs = new ArrayDeque<>();
28+
for (int i = 0; i < col; i++) {
29+
if (grid[0][i] == 0) {
30+
bfs.offer(new int[]{0, i});
31+
grid[0][i] = 1;
32+
}
33+
}
34+
int[] dir = new int[]{0, 1, 0, -1, 0};
35+
while (!bfs.isEmpty()) {
36+
int[] curPoint = bfs.poll();
37+
int curRow = curPoint[0], curCol = curPoint[1];
38+
if (curRow == row - 1) {
39+
return true;
40+
}
41+
for (int i = 0; i < 4; i++) {
42+
int tempRow = curRow + dir[i], tempCol = curCol + dir[i + 1];
43+
if (tempRow < 0 || tempRow >= row || tempCol < 0 || tempCol >= col || grid[tempRow][tempCol] == 1) {
44+
continue;
45+
}
46+
grid[tempRow][tempCol] = 1;
47+
bfs.offer(new int[]{tempRow, tempCol});
48+
}
49+
}
50+
51+
return false;
52+
}
53+
}

0 commit comments

Comments
 (0)