Skip to content

Commit 9ac0bd9

Browse files
committed
Added 3 solutions & modified 1 solution
1 parent 7e5921d commit 9ac0bd9

4 files changed

+107
-30
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int[] replaceElements(int[] arr) {
3+
int maxVal = -1;
4+
for (int i = arr.length - 1; i >= 0; i--) {
5+
int temp = arr[i];
6+
arr[i] = maxVal;
7+
maxVal = Math.max(maxVal, temp);
8+
}
9+
return arr;
10+
}
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
int[][] dirs = {{-1, 0}, {0, -1}, {-1, -1}};
3+
final int MOD = 1000000007;
4+
int[][] dpSum;
5+
int[][] dpCount;
6+
public int[] pathsWithMaxScore(List<String> board) {
7+
int numRows = board.size();
8+
int numCols = board.get(0).length();
9+
dpSum = new int[numRows][numCols];
10+
dpCount = new int[numRows][numCols];
11+
dpCount[numRows - 1][numCols - 1] = 1;
12+
helper(board, numRows, numCols);
13+
return new int[]{dpSum[0][0], dpCount[0][0]};
14+
}
15+
16+
private void helper(List<String> board, int numRows, int numCols) {
17+
for (int x = numRows - 1; x >= 0; x--) {
18+
for (int y = numCols - 1; y >= 0; y--) {
19+
if (dpCount[x][y] == 0) {
20+
continue;
21+
}
22+
for (int[] dir : dirs) {
23+
int newX = x + dir[0];
24+
int newY = y + dir[1];
25+
if (newX >= 0 && newY >= 0 && board.get(newX).charAt(newY) != 'X') {
26+
int tempSum = dpSum[x][y];
27+
if (board.get(newX).charAt(newY) != 'E') {
28+
tempSum += Character.getNumericValue(board.get(newX).charAt(newY));
29+
}
30+
if (tempSum > dpSum[newX][newY]) {
31+
dpCount[newX][newY] = dpCount[x][y];
32+
dpSum[newX][newY] = tempSum;
33+
}
34+
else if (tempSum == dpSum[newX][newY]) {
35+
dpCount[newX][newY] = (dpCount[newX][newY] + dpCount[x][y]) % MOD;
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}

Medium/Deepest Leaves Sum.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
Map<Integer, Integer> map;
12+
int deepestLevel;
13+
public int deepestLeavesSum(TreeNode root) {
14+
map = new HashMap<>();
15+
deepestLevel = 0;
16+
helper(root, 0);
17+
return map.get(deepestLevel);
18+
}
19+
20+
private void helper(TreeNode root, int level) {
21+
if (root == null) {
22+
return;
23+
}
24+
if (root.left == null && root.right == null) {
25+
map.put(level, map.getOrDefault(level, 0) + root.val);
26+
deepestLevel = Math.max(deepestLevel, level);
27+
return;
28+
}
29+
helper(root.left, level + 1);
30+
helper(root.right, level + 1);
31+
}
32+
}

Medium/Find and Replace Pattern.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
class Solution {
2-
public static List<String> findAndReplacePattern(String[] words, String pattern) {
3-
String givenPattern = getStringPattern(pattern);
4-
List<String> ans = new ArrayList<>();
5-
6-
for (String word : words) {
7-
if (getStringPattern(word).equals(givenPattern)) {
8-
ans.add(word);
9-
}
10-
}
11-
12-
return ans;
2+
public List<String> findAndReplacePattern(String[] words, String pattern) {
3+
String patternCode = getCode(pattern);
4+
List<String> list = new ArrayList<>();
5+
for (String word : words) {
6+
String wordCode = getCode(word);
7+
if (wordCode.equals(patternCode)) {
8+
list.add(word);
9+
}
1310
}
14-
15-
private static String getStringPattern(String pattern) {
16-
StringBuilder sb = new StringBuilder();
17-
char[] chars = pattern.toCharArray();
18-
int val = 0;
19-
Map<Character, Integer> map = new HashMap<>();
20-
21-
for (char c : chars) {
22-
if (!map.containsKey(c)) {
23-
map.put(c, val);
24-
val++;
25-
}
26-
}
27-
28-
for (int i=0; i<chars.length; i++) {
29-
sb.append(map.get(chars[i]));
30-
}
31-
32-
return sb.toString();
11+
return list;
12+
}
13+
14+
private String getCode(String word) {
15+
Map<Character, Integer> map = new HashMap<>();
16+
StringBuilder sb = new StringBuilder();
17+
int count = 0;
18+
for (char c : word.toCharArray()) {
19+
if (!map.containsKey(c)) {
20+
map.put(c, count++);
21+
}
22+
sb.append(map.get(c));
3323
}
24+
return sb.toString();
25+
}
3426
}

0 commit comments

Comments
 (0)