-
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.
- Loading branch information
Showing
7 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
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,19 @@ | ||
package com.leetcode; | ||
|
||
class Solution1094 { | ||
public boolean carPooling(int[][] trips, int capacity) { | ||
int[] passengers = new int[1001]; | ||
for (int[] trip: trips) { | ||
passengers[trip[1]] += trip[0]; | ||
passengers[trip[2]] -= trip[0]; | ||
} | ||
int cnt = 0; | ||
for (int i: passengers) { | ||
cnt += i; | ||
if (cnt > capacity) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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,51 @@ | ||
package com.leetcode; | ||
|
||
import javafx.beans.property.ReadOnlyIntegerWrapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
class Solution1462 { | ||
public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) { | ||
HashSet<Integer>[] preDirectSet = new HashSet[numCourses]; | ||
HashSet<Integer>[] preSet = new HashSet[numCourses]; | ||
for (int[] prerequisite: prerequisites) { | ||
if (preDirectSet[prerequisite[1]] == null) { | ||
preDirectSet[prerequisite[1]] = new HashSet<>(); | ||
} | ||
preDirectSet[prerequisite[1]].add(prerequisite[0]); | ||
} | ||
|
||
for (int i = 0; i < numCourses; i++) { | ||
HashSet<Integer> tmp = new HashSet<>(); | ||
dfs(i, preDirectSet, tmp, new boolean[numCourses]); | ||
preSet[i] = tmp; | ||
} | ||
|
||
List<Boolean> res = new ArrayList<>(); | ||
for (int[] query: queries) { | ||
if (preSet[query[1]].contains(query[0])) { | ||
res.add(true); | ||
} else { | ||
res.add(false); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
private void dfs(int idx, HashSet<Integer>[] preDirectSet, HashSet<Integer> set, boolean[] visited) { | ||
if (visited[idx]) { | ||
return; | ||
} | ||
visited[idx] = true; | ||
set.add(idx); | ||
if (preDirectSet[idx] == null) { | ||
return; | ||
} | ||
for (int next: preDirectSet[idx]) { | ||
dfs(next, preDirectSet, set, visited); | ||
} | ||
} | ||
} |
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,23 @@ | ||
package com.leetcode; | ||
|
||
import java.util.Arrays; | ||
|
||
class Solution1465 { | ||
public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) { | ||
Arrays.sort(horizontalCuts); | ||
int maxH = horizontalCuts[0]; | ||
for (int i = 1; i < horizontalCuts.length; i++) { | ||
maxH = Math.max(maxH, horizontalCuts[i] - horizontalCuts[i-1]); | ||
} | ||
maxH = Math.max(maxH, h - horizontalCuts[horizontalCuts.length-1]); | ||
|
||
Arrays.sort(verticalCuts); | ||
int maxV = verticalCuts[0]; | ||
for (int i = 1; i < verticalCuts.length; i++) { | ||
maxV = Math.max(maxV, verticalCuts[i] - verticalCuts[i-1]); | ||
} | ||
maxV = Math.max(maxV, w - verticalCuts[verticalCuts.length-1]); | ||
|
||
return (int) (((long)maxV * (long)maxH) % (long)(1e9+7)); | ||
} | ||
} |
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,85 @@ | ||
package com.leetcode; | ||
|
||
import jdk.nashorn.internal.ir.SplitReturn; | ||
|
||
import java.util.LinkedList; | ||
|
||
class FrontMiddleBackQueue { | ||
// first-----head------last first-------tail--------last | ||
LinkedList<Integer> head; | ||
LinkedList<Integer> tail; | ||
|
||
public FrontMiddleBackQueue() { | ||
this.head = new LinkedList<>(); | ||
this.tail = new LinkedList<>(); | ||
} | ||
|
||
public void pushFront(int val) { | ||
if (this.head.size() > this.tail.size()) { | ||
this.tail.addFirst(this.head.removeLast()); | ||
} | ||
this.head.addFirst(val); | ||
} | ||
|
||
public void pushMiddle(int val) { | ||
if (this.head.size() > this.tail.size()) { | ||
this.tail.addFirst(this.head.removeLast()); | ||
} | ||
this.head.addLast(val); | ||
} | ||
|
||
public void pushBack(int val) { | ||
this.tail.addLast(val); | ||
if (this.head.size() < this.tail.size()) { | ||
this.head.addLast(this.tail.removeFirst()); | ||
} | ||
} | ||
|
||
public int popFront() { | ||
if (this.head.size() == 0) { | ||
return -1; | ||
} | ||
int res = this.head.removeFirst(); | ||
if (this.head.size() < this.tail.size()) { | ||
this.head.addLast(this.tail.removeFirst()); | ||
} | ||
return res; | ||
} | ||
|
||
public int popMiddle() { | ||
if (this.head.size() == 0) { | ||
return -1; | ||
} | ||
int res = this.head.removeLast(); | ||
if (this.head.size() < this.tail.size()) { | ||
this.head.addLast(this.tail.removeFirst()); | ||
} | ||
return res; | ||
} | ||
|
||
public int popBack() { | ||
if (this.tail.size() == 0) { | ||
if (this.head.size() == 0) { | ||
return -1; | ||
} else { | ||
return this.head.removeLast(); | ||
} | ||
} | ||
int res = this.tail.removeLast(); | ||
while(this.head.size() - this.tail.size() > 1) { | ||
this.tail.addFirst(this.head.removeLast()); | ||
} | ||
return res; | ||
} | ||
} | ||
|
||
/** | ||
* Your FrontMiddleBackQueue object will be instantiated and called as such: | ||
* FrontMiddleBackQueue obj = new FrontMiddleBackQueue(); | ||
* obj.pushFront(val); | ||
* obj.pushMiddle(val); | ||
* obj.pushBack(val); | ||
* int param_4 = obj.popFront(); | ||
* int param_5 = obj.popMiddle(); | ||
* int param_6 = obj.popBack(); | ||
*/ |
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,22 @@ | ||
package com.leetcode; | ||
|
||
import java.util.Comparator; | ||
import java.util.PriorityQueue; | ||
|
||
class Solution1962 { | ||
public int minStoneSum(int[] piles, int k) { | ||
int res = 0; | ||
PriorityQueue<Integer> pq = new PriorityQueue<>((Comparator.comparingInt(o -> -o))); | ||
for (int pile: piles) { | ||
pq.add(pile); | ||
res += pile; | ||
} | ||
for (int i = 0; i < k; i++) { | ||
int v = pq.poll(); | ||
int minus = v/2; | ||
res -= minus; | ||
pq.add(v-minus); | ||
} | ||
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,33 @@ | ||
package com.leetcode; | ||
|
||
import javafx.util.Pair; | ||
|
||
class Solution2661 { | ||
public int firstCompleteIndex(int[] arr, int[][] mat) { | ||
int m = mat.length; | ||
int n = mat[0].length; | ||
int[] mCnt = new int[m]; | ||
int[] nCnt = new int[n]; | ||
|
||
int[][] idx = new int[m * n][2]; | ||
for (int i = 0; i < m; i++) { | ||
for (int j = 0; j < n; j++) { | ||
idx[mat[i][j]-1][0] = i; | ||
idx[mat[i][j]-1][1] = j; | ||
} | ||
} | ||
|
||
for (int i = 0; i < arr.length; i++) { | ||
int[] p = idx[arr[i]]; | ||
mCnt[p[0]]++; | ||
if (mCnt[p[0]] == n) { | ||
return i; | ||
} | ||
nCnt[p[1]]++; | ||
if (nCnt[p[1]] == m) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
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,15 @@ | ||
package com.leetcode; | ||
|
||
class Solution746 { | ||
public int minCostClimbingStairs(int[] cost) { | ||
int[] dp = new int[cost.length + 1]; | ||
for (int i = 2; i < dp.length; i++) { | ||
int v = dp[i-1] + cost[i-1]; | ||
if (i - 2 >= 0) { | ||
v = Math.min(v, dp[i-2] + cost[i-2]); | ||
} | ||
dp[i] = v; | ||
} | ||
return dp[dp.length-1]; | ||
} | ||
} |