diff --git a/src/main/java/com/leetcode/1094.java b/src/main/java/com/leetcode/1094.java new file mode 100644 index 0000000..63750d8 --- /dev/null +++ b/src/main/java/com/leetcode/1094.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/1462.java b/src/main/java/com/leetcode/1462.java new file mode 100644 index 0000000..5aef192 --- /dev/null +++ b/src/main/java/com/leetcode/1462.java @@ -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 checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) { + HashSet[] preDirectSet = new HashSet[numCourses]; + HashSet[] 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 tmp = new HashSet<>(); + dfs(i, preDirectSet, tmp, new boolean[numCourses]); + preSet[i] = tmp; + } + + List 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[] preDirectSet, HashSet 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); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/1465.java b/src/main/java/com/leetcode/1465.java new file mode 100644 index 0000000..6f29551 --- /dev/null +++ b/src/main/java/com/leetcode/1465.java @@ -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)); + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/1670.java b/src/main/java/com/leetcode/1670.java new file mode 100644 index 0000000..2d9a992 --- /dev/null +++ b/src/main/java/com/leetcode/1670.java @@ -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 head; + LinkedList 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(); + */ \ No newline at end of file diff --git a/src/main/java/com/leetcode/1962.java b/src/main/java/com/leetcode/1962.java new file mode 100644 index 0000000..8e5f9c7 --- /dev/null +++ b/src/main/java/com/leetcode/1962.java @@ -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 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; + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/2661.java b/src/main/java/com/leetcode/2661.java new file mode 100644 index 0000000..c369298 --- /dev/null +++ b/src/main/java/com/leetcode/2661.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/746.java b/src/main/java/com/leetcode/746.java new file mode 100644 index 0000000..cbe8602 --- /dev/null +++ b/src/main/java/com/leetcode/746.java @@ -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]; + } +} \ No newline at end of file