Skip to content

Commit df11e95

Browse files
committed
Code is Added
1 parent 58fac0e commit df11e95

File tree

26 files changed

+385
-0
lines changed

26 files changed

+385
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [981. Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashMap;
2+
3+
class TimeMap {
4+
private final HashMap<String, HashMap<Integer, String>> keyTimeMap;
5+
6+
public TimeMap() {
7+
keyTimeMap = new HashMap<>();
8+
}
9+
10+
public void set(String key, String value, int timestamp) {
11+
if (!keyTimeMap.containsKey(key)) {
12+
keyTimeMap.put(key, new HashMap<>());
13+
}
14+
keyTimeMap.get(key).put(timestamp, value);
15+
}
16+
17+
public String get(String key, int timestamp) {
18+
if (!keyTimeMap.containsKey(key)) {
19+
return "";
20+
}
21+
22+
for (int currTime = timestamp; currTime >= 1; currTime--) {
23+
if (keyTimeMap.get(key).containsKey(currTime)) {
24+
return keyTimeMap.get(key).get(currTime);
25+
}
26+
}
27+
28+
return "";
29+
}
30+
}
31+
32+
/**
33+
* Your TimeMap object will be instantiated and called as such:
34+
* TimeMap obj = new TimeMap();
35+
* obj.set(key,value,timestamp);
36+
* String param_2 = obj.get(key,timestamp);
37+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class MinimumCostForTickets {
5+
int[] costs;
6+
Integer[] memo;
7+
Set<Integer> set;
8+
9+
public int mincostTickets(int[] days, int[] costs) {
10+
this.costs = costs;
11+
memo = new Integer[366];
12+
set = new HashSet<>();
13+
for (int day : days) {
14+
set.add(day);
15+
}
16+
return dp(1);
17+
}
18+
19+
private int dp(int i) {
20+
if (i > 365) {
21+
return 0;
22+
}
23+
if (memo[i] != null) {
24+
return memo[i];
25+
}
26+
if (set.contains(i)) {
27+
memo[i] = Math.min(Math.min(dp(i + 1) + costs[0], dp(i + 7) + costs[1]), dp(i + 30) + costs[2]);
28+
} else {
29+
memo[i] = dp(i + 1);
30+
}
31+
return memo[i];
32+
}
33+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [983. Minimum Cost For Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(1)$. The total day is 365, so the time complexity is constant.
7+
- Space Complexity: $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [985. Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class SumOfEvenNumbersAfterQueries {
2+
public int[] sumEvenAfterQueries(int[] nums, int[][] queries) {
3+
int sum = 0;
4+
for (int num : nums) {
5+
if (num % 2 == 0) sum += num;
6+
}
7+
8+
int[] res = new int[nums.length];
9+
10+
for (int i = 0; i < queries.length; i++) {
11+
int val = queries[i][0];
12+
int index = queries[i][1];
13+
if (nums[index] % 2 == 0) sum -= nums[index];
14+
nums[index] += val;
15+
if (nums[index] % 2 == 0) sum += nums[index];
16+
res[i] = sum;
17+
}
18+
return res;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class IntervalListIntersections {
5+
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
6+
if (firstList.length == 0 || secondList.length == 0) return new int[][]{};
7+
List<int[]> res = new ArrayList<>();
8+
int i = 0, j = 0;
9+
while (i < firstList.length && j < secondList.length) {
10+
int low = Math.max(firstList[i][0], secondList[j][0]);
11+
int high = Math.min(firstList[i][1], secondList[j][1]);
12+
if (low <= high) res.add(new int[]{low, high});
13+
if (firstList[i][1] < secondList[j][1]) i++;
14+
else j++;
15+
}
16+
return res.toArray(new int[res.size()][]);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [986. Interval List Intersections](https://leetcode.com/problems/interval-list-intersections/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [987. Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.*;
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* public class TreeNode {
6+
* int val;
7+
* TreeNode left;
8+
* TreeNode right;
9+
* TreeNode() {}
10+
* TreeNode(int val) { this.val = val; }
11+
* TreeNode(int val, TreeNode left, TreeNode right) {
12+
* this.val = val;
13+
* this.left = left;
14+
* this.right = right;
15+
* }
16+
* }
17+
*/
18+
class VerticalOrderTraversalOfABinaryTree {
19+
Map<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map;
20+
21+
private void dfs(TreeNode root, int index, int level) {
22+
if (root == null) return;
23+
map.putIfAbsent(index, new TreeMap<>());
24+
map.get(index).putIfAbsent(level, new PriorityQueue<>());
25+
map.get(index).get(level).add(root.val);
26+
dfs(root.left, index - 1, level + 1);
27+
dfs(root.right, index + 1, level + 1);
28+
}
29+
30+
public List<List<Integer>> verticalTraversal(TreeNode root) {
31+
if (root == null) return null;
32+
map = new TreeMap<>();
33+
dfs(root, 0, 0);
34+
List<List<Integer>> res = new LinkedList<>();
35+
for (int key : map.keySet()) {
36+
List<Integer> list = new LinkedList<>();
37+
TreeMap<Integer, PriorityQueue<Integer>> tm = map.get(key);
38+
for (int k : tm.keySet()) {
39+
PriorityQueue<Integer> pq = tm.get(k);
40+
while (!pq.isEmpty()) list.add(pq.poll());
41+
}
42+
res.add(list);
43+
}
44+
return res;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
5+
class AddToArrayFormOfInteger {
6+
public List<Integer> addToArrayForm(int[] num, int k) {
7+
List<Integer> ans = new ArrayList<>();
8+
for (int i = num.length - 1; i >= 0 || k > 0; i--, k /= 10) {
9+
if (i >= 0)
10+
k += num[i];
11+
ans.add(k % 10);
12+
}
13+
Collections.reverse(ans);
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [989. Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(\max(n,\log k))$. $n$ is the length of `num`.
7+
- Space Complexity: $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [990. Satisfiability of Equality Equations](https://leetcode.com/problems/satisfiability-of-equality-equations/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class SatisfiabilityOfEqualityEquations {
2+
int[] parent = new int[26];
3+
4+
private int find(int x) {
5+
if (parent[x] == x) return x;
6+
return parent[x] = find(parent[x]);
7+
}
8+
9+
public boolean equationsPossible(String[] equations) {
10+
for (int i = 0; i < 26; i++) parent[i] = i;
11+
for (String e : equations) {
12+
if (e.charAt(1) == '=') {
13+
parent[find(e.charAt(0)- 'a')] = find(e.charAt(3) - 'a');
14+
}
15+
}
16+
for (String e : equations) {
17+
if (e.charAt(1) == '!' && find(e.charAt(0) - 'a') == find(e.charAt(3) - 'a')) {
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
}

0994.rotting-oranges/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [994. Rotting Oranges](https://leetcode.com/problems/rotting-oranges/)
2+
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class RottingOranges {
2+
private void rotAdjacent(int[][] grid, int i, int j, int minutes) {
3+
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0 || (1 < grid[i][j] && grid[i][j] < minutes)) return;
4+
else {
5+
grid[i][j] = minutes;
6+
rotAdjacent(grid, i - 1, j, minutes + 1);
7+
rotAdjacent(grid, i + 1, j, minutes + 1);
8+
rotAdjacent(grid, i, j - 1, minutes + 1);
9+
rotAdjacent(grid, i, j + 1, minutes + 1);
10+
}
11+
}
12+
13+
public int orangesRotting(int[][] grid) {
14+
if (grid == null || grid.length == 0) return -1;
15+
16+
for (int i = 0; i < grid.length; i++) {
17+
for (int j = 0; j < grid[0].length; j++) {
18+
if (grid[i][j] == 2) rotAdjacent(grid, i, j, 2);
19+
}
20+
}
21+
22+
int minutes = 2;
23+
for (int[] row : grid) {
24+
for (int cell : row) {
25+
if (cell == 1) return -1;
26+
minutes = Math.max(minutes, cell);
27+
}
28+
}
29+
30+
return minutes - 2;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class FindTheTownJudge {
2+
public int findJudge(int n, int[][] trust) {
3+
int[] isTrusted = new int[n + 1];
4+
int[] trustOthers = new int[n + 1];
5+
for (int[] edge : trust) {
6+
trustOthers[edge[0]]++;
7+
isTrusted[edge[1]]++;
8+
}
9+
for (int i = 1; i <= n; i++) {
10+
if (isTrusted[i] == n - 1 && trustOthers[i] == 0)
11+
return i;
12+
}
13+
return -1;
14+
}
15+
}

0997.find-the-town-judge/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [997. Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n+m)$. $m$ is the length of `trust` array. We first iterate the `trust` array and iterate $n$ times to find the judge. The total time complexity is $O(n+m)$.
7+
- Space Complexity: $O(n)$. We use two arrays to record trust other people and is trusted array. The total space complexity is $O(2n)=O(n)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Arrays;
2+
3+
class CapacityToShipPackagesWithinDDays {
4+
public int shipWithinDays(int[] weights, int days) {
5+
int left = Arrays.stream(weights).max().getAsInt();
6+
int right = Arrays.stream(weights).sum();
7+
while (left < right) {
8+
int mid = (left + right) / 2;
9+
int need = 1, curr = 0;
10+
for (int weight : weights) {
11+
if (curr + weight > mid) {
12+
need++;
13+
curr = 0;
14+
}
15+
curr += weight;
16+
}
17+
if (need <= days) {
18+
right = mid;
19+
} else {
20+
left = mid + 1;
21+
}
22+
}
23+
return left;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1011. Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$. $w$ is the sum of `weights` and $n$ is the length of `weights`. Binary Search costs $O(\log w)$. Also, we iterate `weights` in the binary search which costs $O(n)$. The total time complexity is $O(n\log w)$.
7+
- Space Complexity: $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class MaximumDifferenceBetweenNodeAndAncestor {
17+
private int result = 0;
18+
19+
public int maxAncestorDiff(TreeNode root) {
20+
if (root == null) return 0;
21+
dfs(root, root.val, root.val);
22+
return result;
23+
}
24+
25+
private void dfs(TreeNode root, int currMax, int currMin) {
26+
if (root == null) return;
27+
int possibleResult = Math.max(Math.abs(currMax - root.val), Math.abs(root.val - currMin));
28+
result = Math.max(result, possibleResult);
29+
currMax = Math.max(currMax, root.val);
30+
currMin = Math.min(currMin, root.val);
31+
dfs(root.left, currMax, currMin);
32+
dfs(root.right, currMax, currMin);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1026. Maximum Difference Between Node and Ancestor](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/)
2+

1035.uncrossed-lines/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1035. Uncrossed Lines](https://leetcode.com/problems/uncrossed-lines/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n\times m)$.
7+
- Space Complexity: $O(n\times m)$.

0 commit comments

Comments
 (0)