Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/java' into java
Browse files Browse the repository at this point in the history
  • Loading branch information
baowj-678 committed Nov 26, 2023
2 parents d469d7b + fcc0ea5 commit 1007ec0
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 1 deletion.
1 change: 1 addition & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
### 拓扑排序
* [剑指 Offer II 115. 重建序列.java](src/main/java/com/leetcode/剑指%20Offer%20II%20115.%20重建序列.java)【难,重要】

### 单调队列
* [1410. HTML 实体解析器](src/main/java/com/leetcode/2736.java)【难,栈】

## 知识点总结
* HashMap遍历无顺序,TreeMap遍历按照Key字典序升序;
54 changes: 54 additions & 0 deletions src/main/java/com/leetcode/1334.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.leetcode;

class Solution1334 {
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
int[][] dist = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = -1;
}
dist[i][i] = 0;
}
for (int[] edge: edges) {
dist[edge[0]][edge[1]] = edge[2];
dist[edge[1]][edge[0]] = edge[2];
}

for (int k = 0; k < n; k++) {
for (int start = 0; start < n; start++) {
for (int end = start+1; end < n; end++) {
if (dist[start][k] != -1 && dist[k][end] != -1) {
if (dist[start][end] == -1) {
dist[start][end] = dist[start][k] + dist[k][end];
} else {
dist[start][end] = Math.min(dist[start][end], dist[start][k] + dist[k][end]);
}
dist[end][start] = dist[start][end];
}
}
}
}

int res = 0, min = n;
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (dist[i][j] != -1 && dist[i][j] <= distanceThreshold) {
cnt++;
}
}
if (cnt <= min) {
min = cnt;
res = i;
}
}
return res;
}


public static void main(String[] args) {
Solution1334 so = new Solution1334();
int[][] next = new int[][]{{0,1,2},{0,4,8},{1,2,3},{1,4,2},{2,3,1},{3,4,1}};
System.out.println(so.findTheCity(5, next, 2));
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/leetcode/2216.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.leetcode;

class Solution2216 {
public int minDeletion(int[] nums) {
int last = -1, res = 0;
boolean isEven = true;
for (int num: nums) {
if (isEven) {
last = num;
isEven = false;
} else {
if (last == num) {
res++;
} else {
isEven = true;
}
}
}
if (!isEven) {
res++;
}
return res;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/leetcode/2300.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.leetcode;

import java.util.Arrays;

class Solution2300 {
public int[] successfulPairs(int[] spells, int[] potions, long success) {
Arrays.sort(potions);
System.out.println(Arrays.toString(potions));
for (int i = 0; i < spells.length; i++) {
long v = (long) spells[i];
int l = 0, r = potions.length;
while (l < r) {
int mid = (l + r) / 2;
if (v * potions[mid] < success) {
l = mid + 1;
} else {
r = mid;
}
}
spells[i] = potions.length - l;
}
return spells;
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/leetcode/2304.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.leetcode;

import java.util.Arrays;

class Solution2304 {
public int minPathCost(int[][] grid, int[][] moveCost) {
int len = grid[0].length;
int[] dp = new int[len];
// for (int i = 0; i < len; i++) {
// dp[i] = Integer.MAX_VALUE;
// }
for (int t = 0; t < grid.length; t++) {
int[] row = grid[t];
int[] tmp = new int[len];
for (int i = 0; i < len; i++) {
tmp[i] = Integer.MAX_VALUE;
}

for (int i = 0; i < len; i++) {
int v = row[i];
if (t < grid.length - 1) {
for (int j = 0; j < len; j++) {
int cost = moveCost[v][j];
if (tmp[j] > dp[i] + v + cost) {
tmp[j] = dp[i] + v + cost;
}
}
} else {
tmp[i] = dp[i] + v;
}
}

for (int i = 0; i < len; i++) {
dp[i] = tmp[i];
}
}
return Arrays.stream(dp).min().getAsInt();
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/leetcode/2342.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.leetcode;

import java.util.HashMap;
import java.util.Map;

class Solution2342 {
public int maximumSum(int[] nums) {
HashMap<Integer, Integer[]> map = new HashMap<>();
for (int num: nums) {
int s = 0, tmp = num;
while (tmp > 0) {
s += tmp % 10;
tmp /= 10;
}

if (map.containsKey(s)) {
Integer[] integers = map.get(s);
if (num > integers[1]) {
integers[0] = integers[1];
integers[1] = num;
} else if (num > integers[0]) {
integers[0] = num;
}
map.put(s, integers);
} else {
map.put(s, new Integer[]{0, num});
}
}

int res = -1;
for (Map.Entry<Integer, Integer[]> entry: map.entrySet()) {
if (entry.getValue()[0] != 0) {
int s = entry.getValue()[0] + entry.getValue()[1];
if (s > res) {
res = s;
}
}
}
return res;
}

public static void main(String[] args) {
int[] nums = new int[]{18,43,36,13,7};
Solution2342 so = new Solution2342();
System.out.println(so.maximumSum(nums));
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/leetcode/2656.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.leetcode;

import java.util.Arrays;

class Solution2656 {
public int maximizeSum(int[] nums, int k) {
int res = Arrays.stream(nums).max().getAsInt();
return res * k + (k - 1) * k / 2;
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/leetcode/2736.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.leetcode;

import javafx.util.Pair;

import java.util.ArrayList;
import java.util.Stack;

class Solution2736 {
public int[] maximumSumQueries(int[] nums1, int[] nums2, int[][] queries) {
ArrayList<Pair<Integer, Integer>> nums = new ArrayList<>(nums1.length);
for (int i = 0; i < nums1.length; i++) {
nums.add(new Pair<>(nums1[i], nums2[i]));
}
nums.sort((o1, o2) -> o2.getKey() - o1.getKey());

ArrayList<Pair<int[], Integer>> queriesWithIdx = new ArrayList<>(queries.length);
for (int i = 0; i < queries.length; i++) {
queriesWithIdx.add(new Pair<>(queries[i], i));
}
queriesWithIdx.sort((o1, o2) -> o2.getKey()[0] - o1.getKey()[0]);
Stack<Pair<Integer, Integer>> stack = new Stack<>();

int[] res = new int[queries.length];
int idx = 0;
for (Pair<int[], Integer> query: queriesWithIdx) {
while (idx < nums.size() && nums.get(idx).getKey() >= query.getKey()[0]) {
Pair<Integer, Integer> element = nums.get(idx);
if (stack.empty()) {
stack.push(new Pair<>(element.getValue(), element.getValue() + element.getKey()));
} else {
Pair<Integer, Integer> top = stack.peek();
if (element.getValue() > top.getKey()) {
while (element.getKey() + element.getValue() >= top.getValue()) {
stack.pop();
if (stack.empty()) {
break;
}
top = stack.peek();
}
stack.push(new Pair<>(element.getValue(), element.getValue() + element.getKey()));
}
}
idx++;
}
res[query.getValue()] = binarySearch(stack, query.getKey()[1]);
}
return res;
}

private int binarySearch(Stack<Pair<Integer, Integer>>stack, int v) {
int bottom = 0, top = stack.size();
while (bottom < top) {
int mid = (bottom + top) / 2;
if (stack.get(mid).getKey() < v) {
bottom = mid + 1;
} else {
top = mid;
}
}
if (top == stack.size()) {
return -1;
}
return stack.get(top).getValue();
}
}

0 comments on commit 1007ec0

Please sign in to comment.