Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd8cc38

Browse files
committed
2 parents 33f27d2 + 8daca7d commit cd8cc38

36 files changed

+782
-33
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Large diffs are not rendered by default.

cpp/0394-decode-string.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
string decodeString(string s) {
4+
stack<string> stack;
5+
string result;
6+
7+
for (int i = 0; i < s.length(); i++) {
8+
if (s[i] != ']') {
9+
stack.push(string(1, s[i]));
10+
} else {
11+
string substr;
12+
while (!stack.empty() && stack.top() != "[") {
13+
substr = stack.top() + substr;
14+
stack.pop();
15+
}
16+
stack.pop();
17+
18+
string k;
19+
while (!stack.empty() && isdigit(stack.top()[0])) {
20+
k = stack.top() + k;
21+
stack.pop();
22+
}
23+
int kInt = stoi(k);
24+
25+
string temp;
26+
for (int j = 0; j < kInt; j++) {
27+
temp += substr;
28+
}
29+
stack.push(temp);
30+
}
31+
}
32+
33+
while (!stack.empty()) {
34+
result = stack.top() + result;
35+
stack.pop();
36+
}
37+
38+
return result;
39+
}
40+
};

java/0226-invert-binary-tree.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public TreeNode invertTree(TreeNode root) {
44
if (root == null) return null;
55
TreeNode node = new TreeNode(root.val);
66
node.right = invertTree(root.left);
7-
node.val = root.val;
87
node.left = invertTree(root.right);
98
return node;
109
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class NumMatrix {
2+
int[][] preSum;
3+
4+
public NumMatrix(int[][] matrix) {
5+
int m = matrix.length;
6+
int n = matrix[0].length;
7+
preSum = new int[m + 1][n + 1];
8+
9+
for (int i = 0; i < m; i++) {
10+
for (int j = 0; j < n; j++) {
11+
preSum[i + 1][j + 1] = preSum[i + 1][j] + preSum[i][j + 1] - preSum[i][j] + matrix[i][j];
12+
}
13+
}
14+
}
15+
16+
public int sumRegion(int row1, int col1, int row2, int col2) {
17+
return preSum[row2 + 1][col2 + 1] - preSum[row2 + 1][col1] - preSum[row1][col2 + 1] + preSum[row1][col1];
18+
}
19+
}
20+
21+
/**
22+
* Your NumMatrix object will be instantiated and called as such:
23+
* NumMatrix obj = new NumMatrix(matrix);
24+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
25+
*/

java/0661-image-smoother.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution {
2+
public int[][] imageSmoother(int[][] img) {
3+
int ROWS = img.length, COLS = img[0].length;
4+
int[][] res = new int[ROWS][COLS];
5+
6+
for(int r = 0; r < ROWS; r++){
7+
for(int c = 0; c < COLS; c++){
8+
int total = 0, cnt = 0;
9+
for(int i = r - 1; i < r + 2; i++){
10+
for(int j = c - 1; j < c + 2; j++){
11+
if(i < 0 || i == ROWS || j < 0 || j == COLS)
12+
continue;
13+
total += img[i][j];
14+
cnt += 1;
15+
}
16+
}
17+
res[r][c] = total / cnt;
18+
}
19+
}
20+
return res;
21+
}
22+
}
23+
24+
// Optimal Solution
25+
26+
class Solution {
27+
public int[][] imageSmoother(int[][] img) {
28+
int ROWS = img.length, COLS = img[0].length;
29+
30+
for(int r = 0; r < ROWS; r++){
31+
for(int c = 0; c < COLS; c++){
32+
int total = 0, cnt = 0;
33+
34+
for(int i = r - 1; i < r + 2; i++){
35+
for(int j = c - 1; j < c + 2; j++){
36+
if(i < 0 || i == ROWS || j < 0 || j == COLS)
37+
continue;
38+
total += img[i][j] % 256;
39+
cnt += 1;
40+
}
41+
}
42+
img[r][c] = img[r][c] ^ (total / cnt) << 8;
43+
}
44+
}
45+
for(int r = 0; r < ROWS; r++){
46+
for(int c = 0; c < COLS; c++){
47+
img[r][c] = img[r][c] >> 8;
48+
}
49+
}
50+
return img;
51+
}
52+
}

java/0704-binary-search.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ public int search(int[] nums, int target) {
55
int j = nums.length - 1;
66

77
while (i <= j) {
8-
int mid = (i + j) / 2;
8+
// mid is calculated this way to prevent integer overflow.
9+
// See: https://blog.research.google/2006/06/extra-extra-read-all-about-it-nearly.html
10+
int mid = i + (j - i) / 2;
911

1012
if (nums[mid] == target) return mid; else if (
1113
nums[mid] < target

java/0904-fruit-into-baskets.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int totalFruit(int[] fruits) {
3+
Map<Integer, Integer> fruitTypeToItsCount = new HashMap<>();
4+
int maxTotalFruits = 0;
5+
int l = 0;
6+
int r = 0;
7+
8+
while (r < fruits.length) {
9+
fruitTypeToItsCount.put(fruits[r], fruitTypeToItsCount.getOrDefault(fruits[r], 0) + 1);
10+
11+
while (fruitTypeToItsCount.size() > 2) {
12+
fruitTypeToItsCount.put(fruits[l], fruitTypeToItsCount.get(fruits[l]) - 1);
13+
if (fruitTypeToItsCount.get(fruits[l]) == 0) {
14+
fruitTypeToItsCount.remove(fruits[l]);
15+
}
16+
l++;
17+
}
18+
19+
maxTotalFruits = Math.max(maxTotalFruits, r - l + 1);
20+
r++;
21+
}
22+
23+
return maxTotalFruits;
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public boolean validateStackSequences(int[] pushed, int[] popped) {
3+
Stack<Integer> stack = new Stack<>();
4+
5+
int i = 0;
6+
for (int value : pushed) {
7+
stack.push(value);
8+
9+
while (i < popped.length && !stack.isEmpty() && stack.peek() == popped[i]) {
10+
stack.pop();
11+
i++;
12+
}
13+
}
14+
15+
return stack.isEmpty();
16+
}
17+
}
Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,47 @@
1+
/* Top Down Method
2+
-----------------------------------*/
13
class Solution {
2-
3-
int mod = 1000000007;
4+
int MOD = (int)1e9 + 7;
5+
Map<String, Integer> memo;
46

57
public int numRollsToTarget(int n, int k, int target) {
6-
if (target > n * k || target < n) return 0;
7-
if (n == 1) return target < n ? 0 : 1;
8-
int[][] dp = new int[n + 1][target + 1];
9-
return helper(n, k, target, dp);
8+
memo = new HashMap<>();
9+
return count(n, k, target);
10+
}
11+
12+
private int count(int n, int k, int target){
13+
String currState = n + "," + target;
14+
if(n == 0)
15+
return (target == 0)? 1: 0;
16+
if(memo.containsKey(currState))
17+
return memo.get(currState);
18+
19+
int res = 0;
20+
for(int val = 1; val < k + 1; val++)
21+
res = (res + count(n - 1, k, target - val)) % MOD;
22+
memo.put(currState, res);
23+
return res;
1024
}
25+
}
26+
27+
/* Bottom Up Method
28+
-----------------------------------------*/
29+
class Solution {
30+
public int numRollsToTarget(int n, int k, int target) {
31+
int[] dp = new int[target + 1];
32+
dp[0] = 1;
33+
int MOD = (int) 1e9 + 7;
34+
35+
for(int dice = 0; dice < n; dice++){
36+
int[] next_dp = new int[target + 1];
1137

12-
public int helper(int n, int k, int target, int[][] dp) {
13-
if (target > n * k || target < n) return 0;
14-
if (target == 0 && n == 0) return 1;
15-
if (dp[n][target] != 0) return dp[n][target];
16-
int sum = 0;
17-
for (int i = 1; i <= k; i++) {
18-
sum = (sum + helper(n - 1, k, target - i, dp)) % mod;
38+
for(int val = 1; val < k + 1; val++){
39+
for(int total = val; total < target + 1; total++){
40+
next_dp[total] = (next_dp[total] + dp[total - val]) % MOD;
41+
}
42+
}
43+
dp = next_dp;
1944
}
20-
return dp[n][target] = sum;
45+
return dp[target];
2146
}
2247
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int maxScore(String s) {
3+
int zero = 0, one = 0;
4+
int res = 0;
5+
6+
for(char c: s.toCharArray()){
7+
if(c == '1')
8+
one += 1;
9+
}
10+
for(int i = 0; i < s.length()-1; i++){
11+
char c = s.charAt(i);
12+
if(c == '0')
13+
zero += 1;
14+
else
15+
one -= 1;
16+
res = Math.max(res, zero + one);
17+
}
18+
return res;
19+
}
20+
}

java/1436-destination-city.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public String destCity(List<List<String>> paths) {
3+
Set<String> set = new HashSet<>();
4+
for(List<String> path: paths){
5+
String city = path.get(0);
6+
set.add(city);
7+
}
8+
9+
for(List<String> path: paths){
10+
String city = path.get(1);
11+
if(!set.contains(city))
12+
return city;
13+
}
14+
return "";
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int maxProduct(int[] nums) {
3+
Arrays.sort(nums);
4+
5+
return (nums[nums.length-1]-1)*(nums[nums.length-2] -1);
6+
}
7+
}

java/1496-path-crossing.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isPathCrossing(String path) {
3+
Set<String> set = new HashSet<>();
4+
set.add("[0, 0]");
5+
int[] pos = {0, 0};
6+
7+
for(char c: path.toCharArray()){
8+
if(c == 'N'){
9+
pos[1] += 1;
10+
}
11+
else if(c == 'S')
12+
pos[1] -= 1;
13+
else if(c == 'E')
14+
pos[0] += 1;
15+
else
16+
pos[0] -= 1;
17+
if(set.contains(Arrays.toString(pos)))
18+
return true;
19+
set.add(Arrays.toString(pos));
20+
}
21+
return false;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*-------------------------------
2+
Time Complexity: O(n)
3+
Space Complexity: O(1)
4+
--------------------------------*/
5+
class Solution {
6+
public int minCost(String colors, int[] neededTime) {
7+
int res = 0, l = 0;
8+
9+
for(int r = 1; r < colors.length(); r++){
10+
if(colors.charAt(l) == colors.charAt(r)){
11+
if(neededTime[l] < neededTime[r]){
12+
res += neededTime[l];
13+
l = r;
14+
}
15+
else
16+
res += neededTime[r];
17+
}
18+
else
19+
l = r;
20+
}
21+
return res;
22+
}
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int numSpecial(int[][] mat) {
3+
int r = mat.length;
4+
int c = mat[0].length;
5+
int[] rowOnes = new int[r];
6+
int[] colOnes = new int[c];
7+
8+
for(int i = 0; i < r; i++){
9+
for(int j = 0; j < c; j++){
10+
if(mat[i][j] == 1){
11+
rowOnes[i]++;
12+
colOnes[j]++;
13+
}
14+
}
15+
}
16+
17+
int res = 0;
18+
for(int i = 0; i < r; i++){
19+
for(int j = 0; j < c; j++){
20+
if(mat[i][j] == 1 && rowOnes[i] == 1 && colOnes[j] == 1){
21+
res++;
22+
}
23+
}
24+
}
25+
return res;
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*--------------------------------
2+
Time Complexity: O(nlog(n))
3+
Space Complexity: O(1)
4+
---------------------------------*/
5+
class Solution {
6+
public int maxWidthOfVerticalArea(int[][] points) {
7+
Arrays.sort(points, (p1, p2) -> p1[0] - p2[0]);
8+
9+
int res = 0;
10+
for(int i = 1; i < points.length; i++){
11+
res = Math.max(res, points[i][0] - points[i-1][0]);
12+
}
13+
return res;
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int minOperations(String s) {
3+
int count = 0;
4+
5+
for(int i = 0; i < s.length(); i++){
6+
if(i % 2 == 1)
7+
count += (s.charAt(i) == '0')? 1: 0;
8+
else
9+
count += (s.charAt(i) == '1')? 1: 0;
10+
}
11+
return Math.min(count, s.length() - count);
12+
}
13+
}

0 commit comments

Comments
 (0)