Skip to content

Commit 93ad784

Browse files
# Conflicts: # README.md
2 parents 575f2df + 4ca18a8 commit 93ad784

28 files changed

+1353
-704
lines changed

README.md

+537-530
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1029. Two City Scheduling
7+
*
8+
* There are 2N people a company is planning to interview.
9+
* The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1].
10+
* Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.
11+
*
12+
* Example 1:
13+
*
14+
* Input: [[10,20],[30,200],[400,50],[30,20]]
15+
* Output: 110
16+
* Explanation:
17+
* The first person goes to city A for a cost of 10.
18+
* The second person goes to city A for a cost of 30.
19+
* The third person goes to city B for a cost of 50.
20+
* The fourth person goes to city B for a cost of 20.
21+
*
22+
* The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
23+
*
24+
* Note:
25+
*
26+
* 1 <= costs.length <= 100
27+
* It is guaranteed that costs.length is even.
28+
* 1 <= costs[i][0], costs[i][1] <= 1000
29+
* */
30+
public class _1029 {
31+
public static class Solution1 {
32+
/**credit: https://leetcode.com/problems/two-city-scheduling/discuss/280173/Java-4-lines-intuitive-solution
33+
* and
34+
* https://leetcode.com/problems/two-city-scheduling/discuss/278771/Java-sort-solution*/
35+
public int twoCitySchedCost(int[][] costs) {
36+
Arrays.sort(costs, (a, b) -> (a[0] - a[1] - (b[0] - b[1])));
37+
int cost = 0;
38+
for (int i = 0; i < costs.length; i++) {
39+
if (i < costs.length / 2) {
40+
cost += costs[i][0];
41+
} else {
42+
cost += costs[i][1];
43+
}
44+
}
45+
return cost;
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 1038. Binary Search Tree to Greater Sum Tree
7+
*
8+
* Given the root of a binary search tree with distinct values,
9+
* modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val.
10+
* As a reminder, a binary search tree is a tree that satisfies these constraints:
11+
*
12+
* The left subtree of a node contains only nodes with keys less than the node's key.
13+
* The right subtree of a node contains only nodes with keys greater than the node's key.
14+
* Both the left and right subtrees must also be binary search trees.
15+
*
16+
*
17+
* Example 1:
18+
* 4(30)
19+
* / \
20+
* 1(36) 6(21)
21+
* / \ / \
22+
* 0(36) 2(35) 5(26) 7(15)
23+
* \ \
24+
* 3(33) 8(8)
25+
*
26+
* Input: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
27+
* Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
28+
*
29+
*
30+
* Note:
31+
*
32+
* The number of nodes in the tree is between 1 and 100.
33+
* Each node will have value between 0 and 100.
34+
* The given tree is a binary search tree.*/
35+
public class _1038 {
36+
public static class Solution1 {
37+
/**credit: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/discuss/286725/JavaC%2B%2BPython-Revered-Inorder-Traversal*/
38+
int greaterSum = 0;
39+
40+
public TreeNode bstToGst(TreeNode root) {
41+
if (root.right != null) {
42+
bstToGst(root.right);
43+
}
44+
greaterSum = root.val = greaterSum + root.val;
45+
if (root.left != null) {
46+
bstToGst(root.left);
47+
}
48+
return root;
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1051. Height Checker
7+
*
8+
* Students are asked to stand in non-decreasing order of heights for an annual photo.
9+
* Return the minimum number of students not standing in the right positions.
10+
* (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)
11+
*
12+
* Example 1:
13+
* Input: [1,1,4,2,1,3]
14+
* Output: 3
15+
* Explanation:
16+
* Students with heights 4, 3 and the last 1 are not standing in the right positions.
17+
*
18+
* Note:
19+
*
20+
* 1 <= heights.length <= 100
21+
* 1 <= heights[i] <= 100
22+
* */
23+
public class _1051 {
24+
public static class Solution1 {
25+
public int heightChecker(int[] heights) {
26+
int[] originals = Arrays.copyOf(heights, heights.length);
27+
Arrays.sort(heights);
28+
int count = 0;
29+
for (int i = 0; i < originals.length; i++) {
30+
if (heights[i] != originals[i]) {
31+
count++;
32+
}
33+
}
34+
return count;
35+
}
36+
}
37+
}

src/main/java/com/fishercoder/solutions/_500.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package com.fishercoder.solutions;
22

3-
43
import java.util.ArrayList;
54
import java.util.Arrays;
65
import java.util.HashSet;
76
import java.util.List;
87
import java.util.Set;
98

109
/**
10+
* 500. Keyboard Row
11+
*
1112
* Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
12-
* <p>
13-
* <p>
13+
*
1414
* American keyboard
15-
* <p>
16-
* <p>
15+
*
1716
* Example 1:
1817
* Input: ["Hello", "Alaska", "Dad", "Peace"]
1918
* Output: ["Alaska", "Dad"]
19+
*
2020
* Note:
2121
* You may use one character in the keyboard more than once.
2222
* You may assume the input string will only contain letters of alphabet.

src/main/java/com/fishercoder/solutions/_501.java

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.Map;
1010
import java.util.Queue;
1111

12-
1312
/**
1413
* 501. Find Mode in Binary Search Tree
1514
* Given a binary search tree with duplicates. You have to find all the mode(s) in given binary tree.

src/main/java/com/fishercoder/solutions/_502.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,27 @@
2929
*/
3030
public class _502 {
3131

32-
/**credit: https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues*/
33-
public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
34-
PriorityQueue<int[]> capitalHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]);
35-
PriorityQueue<int[]> profitHeap = new PriorityQueue<>((a,b) -> b[1] - a[1]);
36-
for (int i = 0; i < Profits.length; i++) {
37-
capitalHeap.add(new int[]{Capital[i], Profits[i]});
38-
}
39-
while (k-- > 0) {
40-
while (!capitalHeap.isEmpty() && capitalHeap.peek()[0] <= W) {
41-
profitHeap.add(capitalHeap.poll());
32+
public static class Solution1 {
33+
/**
34+
* credit: https://discuss.leetcode.com/topic/77768/very-simple-greedy-java-solution-using-two-priorityqueues
35+
*/
36+
public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
37+
PriorityQueue<int[]> capitalHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]);
38+
PriorityQueue<int[]> profitHeap = new PriorityQueue<>((a, b) -> b[1] - a[1]);
39+
for (int i = 0; i < Profits.length; i++) {
40+
capitalHeap.add(new int[]{Capital[i], Profits[i]});
4241
}
43-
if (profitHeap.isEmpty()) {
44-
break;
42+
while (k-- > 0) {
43+
while (!capitalHeap.isEmpty() && capitalHeap.peek()[0] <= W) {
44+
profitHeap.add(capitalHeap.poll());
45+
}
46+
if (profitHeap.isEmpty()) {
47+
break;
48+
}
49+
W += profitHeap.poll()[1];
4550
}
46-
W += profitHeap.poll()[1];
51+
return W;
4752
}
48-
return W;
4953
}
5054

5155
}

src/main/java/com/fishercoder/solutions/_503.java

+42-32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.Stack;
44

55
/**
6+
* 503. Next Greater Element II
7+
*
68
* Given a circular array (the next element of the last element is the first element of the array),
79
* print the Next Greater Number for every element.
810
* The Next Greater Number of a number x is the first greater number to its traversing-order next in the array,
@@ -18,45 +20,53 @@
1820
*/
1921
public class _503 {
2022

21-
//Credit: https://discuss.leetcode.com/topic/77881/typical-ways-to-solve-circular-array-problems-java-solution
22-
//Note: we store INDEX into the stack, reversely, the larger index put at the bottom of the stack, the smaller index at the top
23-
public int[] nextGreaterElements(int[] nums) {
24-
if (nums == null || nums.length == 0) {
25-
return nums;
26-
}
27-
int len = nums.length;
28-
Stack<Integer> stack = new Stack<>();
29-
for (int i = len - 1; i >= 0; i--) {
30-
stack.push(i);
31-
//push all indexes into the stack reversely
32-
}
33-
int[] result = new int[len];
34-
for (int i = len - 1; i >= 0; i--) {
35-
result[i] = -1;
36-
//initialize it to be -1 in case we cannot find its next greater element in the array
37-
while (!stack.isEmpty() && (nums[stack.peek()] <= nums[i])) {
38-
stack.pop();
23+
public static class Solution1 {
24+
/**
25+
* Credit: https://discuss.leetcode.com/topic/77881/typical-ways-to-solve-circular-array-problems-java-solution
26+
* Note: we store INDEX into the stack, reversely, the larger index put at the bottom of the stack, the smaller index at the top
27+
*/
28+
public int[] nextGreaterElements(int[] nums) {
29+
if (nums == null || nums.length == 0) {
30+
return nums;
31+
}
32+
int len = nums.length;
33+
Stack<Integer> stack = new Stack<>();
34+
for (int i = len - 1; i >= 0; i--) {
35+
stack.push(i);
36+
//push all indexes into the stack reversely
3937
}
40-
if (!stack.isEmpty()) {
41-
result[i] = nums[stack.peek()];
38+
int[] result = new int[len];
39+
for (int i = len - 1; i >= 0; i--) {
40+
result[i] = -1;
41+
//initialize it to be -1 in case we cannot find its next greater element in the array
42+
while (!stack.isEmpty() && (nums[stack.peek()] <= nums[i])) {
43+
stack.pop();
44+
}
45+
if (!stack.isEmpty()) {
46+
result[i] = nums[stack.peek()];
47+
}
48+
stack.push(i);
4249
}
43-
stack.push(i);
50+
return result;
4451
}
45-
return result;
4652
}
4753

48-
//credit: https://leetcode.com/articles/next-greater-element-ii/
49-
public int[] nextGreaterElements_editorial_solution(int[] nums) {
50-
int[] result = new int[nums.length];
51-
Stack<Integer> stack = new Stack<>();
52-
for (int i = nums.length * 2 - 1; i >= 0; i--) {
53-
while (!stack.isEmpty() && nums[stack.peek()] <= nums[i % nums.length]) {
54-
stack.pop();
54+
public static class Solution2 {
55+
/**
56+
* credit: https://leetcode.com/articles/next-greater-element-ii/
57+
*/
58+
public int[] nextGreaterElements(int[] nums) {
59+
int[] result = new int[nums.length];
60+
Stack<Integer> stack = new Stack<>();
61+
for (int i = nums.length * 2 - 1; i >= 0; i--) {
62+
while (!stack.isEmpty() && nums[stack.peek()] <= nums[i % nums.length]) {
63+
stack.pop();
64+
}
65+
result[i % nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()];
66+
stack.push(i % nums.length);
5567
}
56-
result[i % nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()];
57-
stack.push(i % nums.length);
68+
return result;
5869
}
59-
return result;
6070
}
6171

6272
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 504. Base 7
5+
*
46
* Given an integer, return its base 7 string representation.
5-
67
Example 1:
78
Input: 100
89
Output: "202"
10+
911
Example 2:
1012
Input: -7
1113
Output: "-10"
@@ -14,7 +16,9 @@
1416
*/
1517
public class _504 {
1618

17-
public String convertToBase7(int num) {
18-
return String.valueOf(Integer.toString(num, 7));
19+
public static class Solution1 {
20+
public String convertToBase7(int num) {
21+
return String.valueOf(Integer.toString(num, 7));
22+
}
1923
}
2024
}

0 commit comments

Comments
 (0)