-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/java' into java
- Loading branch information
Showing
10 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |