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 1447f65

Browse files
Added solutions for 173-179, 210,211,212.
1 parent 5150b33 commit 1447f65

File tree

17 files changed

+453
-4
lines changed

17 files changed

+453
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0101_0200.s0173_binary_search_tree_iterator;
2+
3+
import com_github_leetcode.TreeNode;
4+
5+
public class BSTIterator {
6+
7+
TreeNode node;
8+
9+
public BSTIterator(TreeNode root) {
10+
node = root;
11+
}
12+
13+
public int next() {
14+
int res = -1;
15+
while (node != null) {
16+
if (node.left != null) {
17+
TreeNode rightMost = node.left;
18+
while (rightMost.right != null) {
19+
rightMost = rightMost.right;
20+
}
21+
rightMost.right = node;
22+
TreeNode temp = node.left;
23+
node.left = null;
24+
node = temp;
25+
} else {
26+
res = node.val;
27+
node = node.right;
28+
return res;
29+
}
30+
}
31+
32+
return res;
33+
}
34+
35+
public boolean hasNext() {
36+
return node != null;
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0101_0200.s0174_dungeon_game;
2+
3+
public class Solution {
4+
public int calculateMinimumHP(int[][] dungeon) {
5+
int m = dungeon.length;
6+
int n = dungeon[0].length;
7+
int[][] memo = new int[m][n];
8+
return Math.max(1, 1 - traverse(dungeon, 0, 0, memo));
9+
}
10+
11+
private int traverse(int[][] dungeon, int r, int c, int[][] memo) {
12+
if (r == dungeon.length - 1 && c == dungeon[0].length - 1) {
13+
return dungeon[r][c];
14+
}
15+
if (memo[r][c] != 0) {
16+
return memo[r][c];
17+
}
18+
int res = Integer.MIN_VALUE;
19+
if (r + 1 < dungeon.length) {
20+
res = Math.max(res, traverse(dungeon, r + 1, c, memo));
21+
}
22+
if (c + 1 < dungeon[0].length) {
23+
res = Math.max(res, traverse(dungeon, r, c + 1, memo));
24+
}
25+
res = Math.min(dungeon[r][c], res + dungeon[r][c]);
26+
memo[r][c] = res;
27+
return res;
28+
}
29+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Write your MySQL query statement below
2+
select distinct num as ConsecutiveNums from
3+
(select num, lag(num,1) over() as l1, lag(num,2) over() as l2
4+
from Logs) con_thr
5+
where num = l1 and num = l2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT ifnull(
3+
(SELECT distinct(Salary)
4+
FROM Employee
5+
ORDER BY Salary DESC
6+
LIMIT 1
7+
OFFSET 1), NULL) SecondHighestSalary;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
2+
BEGIN
3+
DECLARE M INT;
4+
SET M=N-1;
5+
RETURN (
6+
# Write your MySQL query statement below.
7+
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1
8+
);
9+
END
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Write your MySQL query statement below
2+
select Score, DENSE_RANK() OVER(order by Score Desc) as 'Rank' from Scores;
3+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0101_0200.s0179_largest_number;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public String largestNumber(int[] nums) {
7+
int n = nums.length;
8+
String[] s = new String[n];
9+
for (int i = 0; i < n; i++) {
10+
s[i] = String.valueOf(nums[i]);
11+
}
12+
Arrays.sort(s, (a, b) -> (b + a).compareTo(a + b));
13+
14+
StringBuilder sb = new StringBuilder();
15+
for (String str : s) {
16+
sb.append(str);
17+
}
18+
String result = sb.toString();
19+
return result.startsWith("0") ? "0" : result;
20+
}
21+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write your MySQL query statement below
2-
select distinct num as ConsecutiveNums from
3-
(select num, lag(num,1) over() as l1, lag(num,2) over() as l2
4-
from Logs) con_thr
5-
where num = l1 and num = l2
2+
select a.firstname, a.lastname, b.city, b.state
3+
from person a
4+
left join address b on
5+
a.personid=b.personid
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g0201_0300.s0210_course_schedule_ii;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int[] findOrder(int numCourses, int[][] prerequisites) {
10+
Map<Integer, List<Integer>> graph = new HashMap<>();
11+
for (int i = 0; i < numCourses; i++) {
12+
graph.put(i, new ArrayList<>());
13+
}
14+
15+
for (int[] classes : prerequisites) {
16+
graph.get(classes[0]).add(classes[1]);
17+
}
18+
19+
List<Integer> output = new ArrayList<>();
20+
Map<Integer, Boolean> visited = new HashMap<>();
21+
for (int c : graph.keySet()) {
22+
if (dfs(c, graph, visited, output)) {
23+
return new int[0];
24+
}
25+
}
26+
27+
int[] res = new int[output.size()];
28+
for (int i = 0; i < output.size(); i++) {
29+
res[i] = output.get(i);
30+
}
31+
return res;
32+
}
33+
34+
private boolean dfs(
35+
int course,
36+
Map<Integer, List<Integer>> graph,
37+
Map<Integer, Boolean> visited,
38+
List<Integer> output) {
39+
if (visited.containsKey(course)) {
40+
return visited.get(course);
41+
}
42+
43+
visited.put(course, true);
44+
45+
for (int c : graph.get(course)) {
46+
if (dfs(c, graph, visited, output)) {
47+
return true;
48+
}
49+
}
50+
51+
visited.put(course, false);
52+
output.add(course);
53+
return false;
54+
}
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0201_0300.s0211_design_add_and_search_words_data_structure;
2+
3+
import java.util.ArrayList;
4+
5+
@SuppressWarnings({"unchecked", "java:S3626"})
6+
public class WordDictionary {
7+
8+
private ArrayList<String>[] dict = new ArrayList[501];
9+
10+
public WordDictionary() {
11+
/** Initialize your data structure here. */
12+
}
13+
14+
/** Adds a word into the data structure. */
15+
public void addWord(String word) {
16+
ArrayList<String> a = dict[word.length()];
17+
if (a == null) {
18+
a = new ArrayList<>();
19+
}
20+
a.add(word);
21+
dict[word.length()] = a;
22+
}
23+
24+
/**
25+
* Returns if the word is in the data structure. A word could contain the dot character '.' to
26+
* represent any one letter.
27+
*/
28+
public boolean search(String word) {
29+
if (dict[word.length()] == null) {
30+
return false;
31+
}
32+
ArrayList<String> a = dict[word.length()];
33+
for (String s : a) {
34+
boolean match = true;
35+
for (int i = 0; i < word.length() && match; i++) {
36+
if (word.charAt(i) == '.') {
37+
continue;
38+
} else if (s.charAt(i) != word.charAt(i)) {
39+
match = false;
40+
}
41+
}
42+
if (match) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
}
49+
50+
/**
51+
* Your WordDictionary object will be instantiated and called as such: WordDictionary obj = new
52+
* WordDictionary(); obj.addWord(word); boolean param_2 = obj.search(word);
53+
*/

0 commit comments

Comments
(0)

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