Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5e7105

Browse files
add java solutions of 889 ~ 900 & 1226
1 parent 80ba2e9 commit f5e7105

File tree

14 files changed

+753
-0
lines changed

14 files changed

+753
-0
lines changed

‎README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
291291
|1116|[Print Zero Even Odd](https://leetcode.com/problems/print-zero-even-odd/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/concurrency/_1116.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Concurrency
292292
|1117|[Building H2O](https://leetcode.com/problems/building-h2o/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/concurrency/_1117.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Concurrency
293293
|1195|[Fizz Buzz Multithreaded](https://leetcode.com/problems/fizz-buzz-multithreaded/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/concurrency/_1195.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Concurrency
294+
|1226|[The Dining Philosophers](https://leetcode.com/problems/the-dining-philosophers/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/concurrency/_1226.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Concurrency
294295

295296

296297

@@ -779,6 +780,18 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
779780
|886|[Possible Bipartition](https://leetcode.com/problems/possible-bipartition/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_886.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
780781
|887|[Super Egg Drop](https://leetcode.com/problems/super-egg-drop/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_887.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
781782
|888|[Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_888.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
783+
|889|[Construct Binary Tree from Preorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_889.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
784+
|890|[Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_890.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
785+
|891|[Sum of Subsequence Widths](https://leetcode.com/problems/sum-of-subsequence-widths/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_891.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
786+
|892|[Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_892.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
787+
|893|[Groups of Special-Equivalent Strings](https://leetcode.com/problems/groups-of-special-equivalent-strings/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_893.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
788+
|894|[All Possible Full Binary Trees](https://leetcode.com/problems/all-possible-full-binary-trees/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_894.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
789+
|895|[Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_895.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
790+
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_896.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
791+
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_897.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Others
792+
|898|[Bitwise ORs of Subarrays](https://leetcode.com/problems/bitwise-ors-of-subarrays/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_898.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
793+
|899|[Orderly Queue](https://leetcode.com/problems/orderly-queue/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_899.java) & Python |![hard](https://github.com/guobinhit/myleetcode/blob/master/images/hard.png)| Others
794+
|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_900.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Others
782795

783796

784797

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.hit.basmath.learn.concurrency;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.concurrent.Semaphore;
6+
7+
/**
8+
* 1226. The Dining Philosophers
9+
* <p>
10+
* Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.
11+
* <p>
12+
* Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when they have both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it is not being used by another philosopher. After an individual philosopher finishes eating, they need to put down both forks so that the forks become available to others. A philosopher can take the fork on their right or the one on their left as they become available, but cannot start eating before getting both forks.
13+
* <p>
14+
* Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply and an infinite demand are assumed.
15+
* <p>
16+
* Design a discipline of behavior (a concurrent algorithm) such that no philosopher will starve; i.e., each can forever continue to alternate between eating and thinking, assuming that no philosopher can know when others may want to eat or think.
17+
* <p>
18+
* The problem statement and the image above are taken from wikipedia.org
19+
* <p>
20+
* The philosophers' ids are numbered from 0 to 4 in a clockwise order. Implement the function void wantsToEat(philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork) where:
21+
* <p>
22+
* philosopher is the id of the philosopher who wants to eat.
23+
* pickLeftFork and pickRightFork are functions you can call to pick the corresponding forks of that philosopher.
24+
* eat is a function you can call to let the philosopher eat once he has picked both forks.
25+
* putLeftFork and pickRightFork are functions you can call to put down the corresponding forks of that philosopher.
26+
* The philosophers are assumed to be thinking as long as they are not asking to eat (the function is not being called with their number).
27+
* Five threads, each representing a philosopher, will simultaneously use one object of your class to simulate the process. It is possible that the function will be called for the same philosopher more than once, even before the last call ends.
28+
* <p>
29+
* Example 1:
30+
* <p>
31+
* Input: n = 1
32+
* Output: [[4,2,1],[4,1,1],[0,1,1],[2,2,1],[2,1,1],[2,0,3],[2,1,2],[2,2,2],[4,0,3],[4,1,2],[0,2,1],[4,2,2],[3,2,1],[3,1,1],[0,0,3],[0,1,2],[0,2,2],[1,2,1],[1,1,1],[3,0,3],[3,1,2],[3,2,2],[1,0,3],[1,1,2],[1,2,2]]
33+
* Explanation:
34+
* n is the number of times each philosopher will call the function.
35+
* The output array describes the calls you made to the functions controlling the forks and the eat function, its format is:
36+
* output[i] = [a, b, c] (three integers)
37+
* - a is the id of a philosopher.
38+
* - b specifies the fork: {1 : left, 2 : right}.
39+
* - c specifies the operation: {1 : pick, 2 : put, 3 : eat}.
40+
* <p>
41+
* Constraints:
42+
* <p>
43+
* 1 <= n <= 60
44+
*/
45+
public class _1226 {
46+
class DiningPhilosophers {
47+
48+
private Semaphore[] forks = new Semaphore[5];
49+
private Semaphore eatingPeople = new Semaphore(2);
50+
51+
public DiningPhilosophers() {
52+
for (int i = 0; i < 5; i++) {
53+
forks[i] = new Semaphore(1);
54+
}
55+
}
56+
57+
// call the run() method of any runnable to execute its code
58+
public void wantsToEat(int philosopher,
59+
Runnable pickLeftFork,
60+
Runnable pickRightFork,
61+
Runnable eat,
62+
Runnable putLeftFork,
63+
Runnable putRightFork) throws InterruptedException {
64+
65+
Semaphore leftFork = forks[philosopher];
66+
Semaphore rightFork = forks[(philosopher + 5 - 1) % 5];
67+
68+
eatingPeople.acquire();
69+
70+
leftFork.acquire();
71+
rightFork.acquire();
72+
pickLeftFork.run();
73+
pickRightFork.run();
74+
eat.run();
75+
putLeftFork.run();
76+
putRightFork.run();
77+
leftFork.release();
78+
rightFork.release();
79+
80+
eatingPeople.release();
81+
}
82+
}
83+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import com.hit.common.TreeNode;
4+
5+
/**
6+
* 889. Construct Binary Tree from Preorder and Postorder Traversal
7+
* <p>
8+
* Return any binary tree that matches the given preorder and postorder traversals.
9+
* <p>
10+
* Values in the traversals pre and post are distinct positive integers.
11+
* <p>
12+
* Example 1:
13+
* <p>
14+
* Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
15+
* Output: [1,2,3,4,5,6,7]
16+
* <p>
17+
* Note:
18+
* <p>
19+
* 1 <= pre.length == post.length <= 30
20+
* pre[] and post[] are both permutations of 1, 2, ..., pre.length.
21+
* It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.
22+
*/
23+
public class _889 {
24+
public TreeNode constructFromPrePost(int[] pre, int[] post) {
25+
return constructFromPrePost(pre, 0, pre.length - 1, post, 0, post.length - 1);
26+
}
27+
28+
private TreeNode constructFromPrePost(int[] pre, int preL, int preR, int[] post, int postL, int postR) {
29+
if (preL > preR || postL > postR) return null;
30+
TreeNode root = new TreeNode(pre[preL]);
31+
if (preL == preR) return root;
32+
33+
int index = -1;
34+
for (int i = postL; i < postR; i++) {
35+
if (pre[preL + 1] == post[i]) {
36+
index = i;
37+
break;
38+
}
39+
}
40+
if (index == -1) return root;
41+
42+
root.left = constructFromPrePost(pre, preL + 1, preL + 1 + (index - postL), post, postL, index);
43+
root.right = constructFromPrePost(pre, preL + 1 + (index - postL) + 1, preR, post, index + 1, postR);
44+
return root;
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* 890. Find and Replace Pattern
8+
* <p>
9+
* You have a list of words and a pattern, and you want to know which words in words matches the pattern.
10+
* <p>
11+
* A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
12+
* <p>
13+
* (Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
14+
* <p>
15+
* Return a list of the words in words that match the given pattern.
16+
* <p>
17+
* You may return the answer in any order.
18+
* <p>
19+
* Example 1:
20+
* <p>
21+
* Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
22+
* Output: ["mee","aqq"]
23+
* Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
24+
* "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
25+
* since a and b map to the same letter.
26+
* <p>
27+
* Note:
28+
* <p>
29+
* 1 <= words.length <= 50
30+
* 1 <= pattern.length = words[i].length <= 20
31+
*/
32+
public class _890 {
33+
public List<String> findAndReplacePattern(String[] words, String pattern) {
34+
List<String> res = new LinkedList<>();
35+
for (String w : words) {
36+
int[] p = new int[26], s = new int[26];
37+
boolean same = true;
38+
for (int i = 0; i < w.length(); i++) {
39+
if (s[w.charAt(i) - 'a'] != p[pattern.charAt(i) - 'a']) {
40+
same = false;
41+
break;
42+
} else {
43+
s[w.charAt(i) - 'a'] = p[pattern.charAt(i) - 'a'] = i + 1;
44+
}
45+
}
46+
if (same) res.add(w);
47+
}
48+
return res;
49+
}
50+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 891. Sum of Subsequence Widths
7+
* <p>
8+
* Given an array of integers A, consider all non-empty subsequences of A.
9+
* <p>
10+
* For any sequence S, let the width of S be the difference between the maximum and minimum element of S.
11+
* <p>
12+
* Return the sum of the widths of all subsequences of A.
13+
* <p>
14+
* As the answer may be very large, return the answer modulo 10^9 + 7.
15+
* <p>
16+
* Example 1:
17+
* <p>
18+
* Input: [2,1,3]
19+
* Output: 6
20+
* Explanation:
21+
* Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3].
22+
* The corresponding widths are 0, 0, 0, 1, 1, 2, 2.
23+
* The sum of these widths is 6.
24+
* <p>
25+
* Note:
26+
* <p>
27+
* 1 <= A.length <= 20000
28+
* 1 <= A[i] <= 20000
29+
*/
30+
public class _891 {
31+
public int sumSubseqWidths(int[] A) {
32+
Arrays.sort(A);
33+
long c = 1, res = 0, mod = (long) 1e9 + 7;
34+
for (int i = 0; i < A.length; ++i, c = (c << 1) % mod)
35+
res = (res + A[i] * c - A[A.length - i - 1] * c) % mod;
36+
return (int) ((res + mod) % mod);
37+
}
38+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.hit.basmath.learn.others;
2+
3+
/**
4+
* 892. Surface Area of 3D Shapes
5+
* <p>
6+
* On a N * N grid, we place some 1 * 1 * 1 cubes.
7+
* <p>
8+
* Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
9+
* <p>
10+
* Return the total surface area of the resulting shapes.
11+
* <p>
12+
* Example 1:
13+
* <p>
14+
* Input: [[2]]
15+
* Output: 10
16+
* <p>
17+
* Example 2:
18+
* <p>
19+
* Input: [[1,2],[3,4]]
20+
* Output: 34
21+
* <p>
22+
* Example 3:
23+
* <p>
24+
* Input: [[1,0],[0,2]]
25+
* Output: 16
26+
* <p>
27+
* Example 4:
28+
* <p>
29+
* Input: [[1,1,1],[1,0,1],[1,1,1]]
30+
* Output: 32
31+
* <p>
32+
* Example 5:
33+
* <p>
34+
* Input: [[2,2,2],[2,1,2],[2,2,2]]
35+
* Output: 46
36+
* <p>
37+
* Note:
38+
* <p>
39+
* 1 <= N <= 50
40+
* 0 <= grid[i][j] <= 50
41+
*/
42+
public class _892 {
43+
public int surfaceArea(int[][] grid) {
44+
int res = 0, n = grid.length;
45+
for (int i = 0; i < n; ++i) {
46+
for (int j = 0; j < n; ++j) {
47+
if (grid[i][j] > 0) res += grid[i][j] * 4 + 2;
48+
if (i > 0) res -= Math.min(grid[i][j], grid[i - 1][j]) * 2;
49+
if (j > 0) res -= Math.min(grid[i][j], grid[i][j - 1]) * 2;
50+
}
51+
}
52+
return res;
53+
}
54+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.hit.basmath.learn.others;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* 893. Groups of Special-Equivalent Strings
9+
* <p>
10+
* You are given an array A of strings.
11+
* <p>
12+
* A move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S.
13+
* <p>
14+
* Two strings S and T are special-equivalent if after any number of moves onto S, S == T.
15+
* <p>
16+
* For example, S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz" that swap S[0] and S[2], then S[1] and S[3].
17+
* <p>
18+
* Now, a group of special-equivalent strings from A is a non-empty subset of A such that:
19+
* <p>
20+
* Every pair of strings in the group are special equivalent, and;
21+
* The group is the largest size possible (ie., there isn't a string S not in the group such that S is special equivalent to every string in the group)
22+
* Return the number of groups of special-equivalent strings from A.
23+
* <p>
24+
* Example 1:
25+
* <p>
26+
* Input: ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
27+
* Output: 3
28+
* Explanation:
29+
* One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings are all pairwise special equivalent to these.
30+
* <p>
31+
* The other two groups are ["xyzz", "zzxy"] and ["zzyx"]. Note that in particular, "zzxy" is not special equivalent to "zzyx".
32+
* <p>
33+
* Example 2:
34+
* <p>
35+
* Input: ["abc","acb","bac","bca","cab","cba"]
36+
* Output: 3
37+
* <p>
38+
* Note:
39+
* <p>
40+
* 1 <= A.length <= 1000
41+
* 1 <= A[i].length <= 20
42+
* All A[i] have the same length.
43+
* All A[i] consist of only lowercase letters.
44+
*/
45+
public class _893 {
46+
public int numSpecialEquivGroups(String[] A) {
47+
Set<String> set = new HashSet<>();
48+
for (String s : A) {
49+
int[] odd = new int[26];
50+
int[] even = new int[26];
51+
for (int i = 0; i < s.length(); i++) {
52+
if (i % 2 == 1) odd[s.charAt(i) - 'a']++;
53+
else even[s.charAt(i) - 'a']++;
54+
}
55+
String sig = Arrays.toString(odd) + Arrays.toString(even);
56+
set.add(sig);
57+
}
58+
return set.size();
59+
}
60+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /