Skip to content

Commit d1f5a7b

Browse files
committed
Added 2 solutions & modified 2 solutions
1 parent 9761fa0 commit d1f5a7b

5 files changed

+178
-61
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int currMin = Integer.MAX_VALUE;
4+
int profit = 0;
5+
6+
for (int price : prices) {
7+
if (price > currMin) {
8+
profit += price - currMin;
9+
}
10+
11+
currMin = price;
12+
}
13+
14+
return profit;
15+
}
16+
}

Easy/best_time_to_buy_and_sell_stockII.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

Hard/Optimal Account Balancing.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Solution {
2+
int min = Integer.MAX_VALUE;
3+
public int minTransfers(int[][] transactions) {
4+
Map<Integer, Integer> account = new HashMap<>();
5+
for (int[] transaction : transactions) {
6+
int amount = transaction[2];
7+
account.put(transaction[0], account.getOrDefault(transaction[0], 0) + amount);
8+
account.put(transaction[1], account.getOrDefault(transaction[1], 0) - amount);
9+
}
10+
11+
LinkedList<Integer> posBalance = new LinkedList<>();
12+
LinkedList<Integer> negBalance = new LinkedList<>();
13+
14+
for (Integer key : account.keySet()) {
15+
if (account.get(key) < 0) {
16+
negBalance.add(account.get(key));
17+
}
18+
else if (account.get(key) > 0) {
19+
posBalance.add(account.get(key));
20+
}
21+
}
22+
23+
dfs(posBalance, negBalance, 0);
24+
return min;
25+
}
26+
27+
private void dfs(List<Integer> posBalance, List<Integer> negBalance, int count) {
28+
if (posBalance.size() == 0 || negBalance.size() == 0) {
29+
min = Math.min(count, min);
30+
return;
31+
}
32+
33+
if (count >= min) {
34+
return;
35+
}
36+
37+
int posAmount = posBalance.get(0);
38+
39+
for(int j = 0; j < negBalance.size(); j++) {
40+
int negAmount = negBalance.get(j);
41+
int newPositiveVal = Math.max(posAmount + negAmount, 0);
42+
int newNegativeVal = Math.min(0, posAmount + negAmount);
43+
if (newPositiveVal == 0){
44+
posBalance.remove(0);
45+
}
46+
else {
47+
posBalance.set(0, newPositiveVal);
48+
}
49+
if (newNegativeVal == 0){
50+
negBalance.remove(j);
51+
}
52+
else{
53+
negBalance.set(j, newNegativeVal);
54+
}
55+
56+
dfs(posBalance, negBalance, count + 1);
57+
58+
if(newPositiveVal == 0){
59+
posBalance.add(0, posAmount);
60+
}
61+
else {
62+
posBalance.set(0, posAmount);
63+
}
64+
65+
if(newNegativeVal == 0){
66+
negBalance.add(j, negAmount);
67+
}
68+
else{
69+
negBalance.set(j, negAmount);
70+
}
71+
}
72+
}
73+
}

Hard/Stream of Characters.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class StreamChecker {
2+
String[] words;
3+
TrieNode root;
4+
StringBuilder sb = new StringBuilder();
5+
public StreamChecker(String[] words) {
6+
this.words = words;
7+
root = new TrieNode('#');
8+
treeify(words);
9+
}
10+
11+
private void treeify (String[] words) {
12+
for (String word : words) {
13+
TrieNode curr = root;
14+
for (int i = word.length() - 1; i >= 0; i--) {
15+
char c = word.charAt(i);
16+
if (curr.childrens[c - 'a'] == null) {
17+
curr.childrens[c - 'a'] = new TrieNode(c);
18+
}
19+
20+
curr = curr.childrens[c - 'a'];
21+
22+
if (i == 0) {
23+
curr.isWord = true;
24+
}
25+
}
26+
}
27+
}
28+
29+
public boolean query(char letter) {
30+
sb.append(letter);
31+
TrieNode curr = root;
32+
for (int i = sb.length() - 1; i >= 0; i--) {
33+
if (curr.childrens[sb.charAt(i) - 'a'] != null) {
34+
curr = curr.childrens[sb.charAt(i) - 'a'];
35+
}
36+
else {
37+
break;
38+
}
39+
40+
if (curr.isWord) {
41+
return true;
42+
}
43+
}
44+
45+
return false;
46+
}
47+
48+
class TrieNode {
49+
char key;
50+
TrieNode[] childrens;
51+
boolean isWord;
52+
53+
public TrieNode (char key) {
54+
this.key = key;
55+
this.childrens = new TrieNode[26];
56+
isWord = false;
57+
}
58+
}
59+
}
60+
61+
/**
62+
* Your StreamChecker object will be instantiated and called as such:
63+
* StreamChecker obj = new StreamChecker(words);
64+
* boolean param_1 = obj.query(letter);
65+
*/

Medium/Minesweeper.java

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,43 @@
11
class Solution {
2-
public char[][] updateBoard(char[][] board, int[] click) {
3-
int row = click[0];
4-
int col = click[1];
5-
6-
updateBoard(board, row, col);
7-
2+
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, -1}, {-1, 1}, {1, 1}, {-1, -1}};
3+
public char[][] updateBoard(char[][] board, int[] click) {
4+
dfs(board, click[0], click[1]);
85
return board;
96
}
107

11-
private void updateBoard(char[][] board, int row, int col) {
12-
if (row < 0 || col < 0 || row >= board.length || col >= board[0].length) {
8+
private void dfs(char[][] board, int x, int y) {
9+
if (!isCoordinateValid(x, y, board)) {
1310
return;
1411
}
1512

16-
if (board[row][col] == 'M') {
17-
board[row][col] = 'X';
13+
if (board[x][y] == 'M') {
14+
board[x][y] = 'X';
1815
}
1916

20-
if (board[row][col] == 'E') {
21-
int mineCount = getMineCount(board, row, col);
17+
if (board[x][y] == 'E') {
18+
int mineCount = getAdjacentMineCount(board, x, y);
2219
if (mineCount == 0) {
23-
board[row][col] = 'B';
24-
updateBoard(board, row + 1, col);
25-
updateBoard(board, row, col + 1);
26-
updateBoard(board, row, col - 1);
27-
updateBoard(board, row - 1, col);
28-
updateBoard(board, row - 1, col - 1);
29-
updateBoard(board, row + 1, col + 1);
30-
updateBoard(board, row - 1, col + 1);
31-
updateBoard(board, row + 1, col - 1);
20+
board[x][y] = 'B';
21+
for (int[] dir : dirs) {
22+
dfs(board, x + dir[0], y + dir[1]);
23+
}
3224
}
3325
else {
34-
board[row][col] = (char) (mineCount + '0');
26+
board[x][y] = (char) (mineCount + '0');
3527
}
36-
}
28+
}
3729
}
3830

39-
private int getMineCount(char[][] board, int row, int col) {
31+
private boolean isCoordinateValid(int x, int y, char[][] board) {
32+
return !(x < 0 || x >= board.length || y < 0 || y >= board[0].length);
33+
}
34+
35+
private int getAdjacentMineCount(char[][] board, int x, int y) {
4036
int count = 0;
41-
42-
if (row + 1 < board.length && board[row + 1][col] == 'M') {
43-
count++;
44-
}
45-
if (row - 1 >= 0 && board[row - 1][col] == 'M') {
46-
count++;
47-
}
48-
if (col + 1 < board[0].length && board[row][col + 1] == 'M') {
49-
count++;
50-
}
51-
if (col - 1 >= 0 && board[row][col - 1] == 'M') {
52-
count++;
53-
}
54-
if (row + 1 < board.length && col + 1 < board[0].length && board[row + 1][col + 1] == 'M') {
55-
count++;
56-
}
57-
if (row + 1 < board.length && col - 1 >= 0 && board[row + 1][col - 1] == 'M') {
58-
count++;
59-
}
60-
if (row - 1 >= 0 && col + 1 < board[0].length && board[row - 1][col + 1] == 'M') {
61-
count++;
62-
}
63-
if (row - 1 >= 0 && col - 1 >= 0 && board[row - 1][col - 1] == 'M') {
64-
count++;
37+
for (int[] dir : dirs) {
38+
if (isCoordinateValid(x + dir[0], y + dir[1], board) && board[x + dir[0]][y + dir[1]] == 'M') {
39+
count++;
40+
}
6541
}
6642

6743
return count;

0 commit comments

Comments
 (0)