Skip to content

Commit

Permalink
2023/12/23
Browse files Browse the repository at this point in the history
  • Loading branch information
baowj-678 committed Dec 23, 2023
1 parent 1007ec0 commit 40e4a20
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/leetcode/1094.java
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;
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/leetcode/1462.java
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);
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/leetcode/1465.java
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));
}
}
85 changes: 85 additions & 0 deletions src/main/java/com/leetcode/1670.java
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();
*/
22 changes: 22 additions & 0 deletions src/main/java/com/leetcode/1962.java
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;
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/leetcode/2661.java
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;
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/leetcode/746.java
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];
}
}

0 comments on commit 40e4a20

Please sign in to comment.