Skip to content

Commit 353e58d

Browse files
committed
Added 1 solution & modified 3 solutions
1 parent 70fa662 commit 353e58d

File tree

4 files changed

+70
-60
lines changed

4 files changed

+70
-60
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxAbsValExpr(int[] arr1, int[] arr2) {
3+
int res = 0;
4+
int n = arr1.length;
5+
int[] dirs = {-1,1};
6+
for (int dir1 : dirs) {
7+
for (int dir2 : dirs) {
8+
int closest = dir1 * arr1[0] + dir2 * arr2[0] + 0;
9+
for (int i = 1; i < n; ++i) {
10+
int cur = dir1 * arr1[i] + dir2 * arr2[i] + i;
11+
res = Math.max(res, cur - closest);
12+
closest = Math.min(closest, cur);
13+
}
14+
}
15+
}
16+
return res;
17+
}
18+
}
Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
class Solution {
2-
public int missingElement(int[] nums, int k) {
3-
int idx = 0;
4-
while (idx < nums.length - 1) {
5-
if (nums[idx] != nums[idx + 1] - 1) {
6-
int diff = nums[idx + 1] - nums[idx] - 1;
7-
if (diff >= k) {
8-
int step = nums[idx] + 1;
9-
while (k > 1) {
10-
step++;
11-
k--;
12-
}
13-
14-
return step;
15-
}
16-
else {
17-
k -= diff;
18-
}
19-
}
20-
21-
idx++;
22-
}
23-
24-
return nums[nums.length - 1] + k;
2+
public int missingElement(int[] nums, int k) {
3+
int n = nums.length;
4+
if (k > missing(n - 1, nums)) {
5+
return nums[n - 1] + k - missing(n - 1, nums);
256
}
7+
int start = 0;
8+
int end = n - 1;
9+
while (start < end) {
10+
int mid = start + (end - start) / 2;
11+
if (missing(mid, nums) < k) {
12+
start = mid + 1;
13+
}
14+
else {
15+
end = mid;
16+
}
17+
}
18+
return nums[start - 1] + k - missing(start - 1, nums);
19+
}
20+
21+
private int missing(int idx, int[] nums) {
22+
return nums[idx] - nums[0] - idx;
23+
}
2624
}

Medium/Palindromic Substrings.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
class Solution {
2-
public int countSubstrings(String s) {
3-
int count = 0;
4-
for (int i=0;i<s.length();i++) {
5-
for (int j=i;j<s.length();j++) {
6-
if(isPalindrome(s.substring(i,j+1))) {
7-
count++;
8-
}
9-
}
2+
public int countSubstrings(String s) {
3+
int N = s.length();
4+
int ans = 0;
5+
for (int center = 0; center <= 2 * N - 1; ++center) {
6+
int left = center / 2;
7+
int right = left + center % 2;
8+
while (left >= 0 && right < N && s.charAt(left) == s.charAt(right)) {
9+
ans++;
10+
left--;
11+
right++;
1012
}
11-
12-
return count;
13-
}
14-
15-
public boolean isPalindrome(String s) {
16-
StringBuilder sb = new StringBuilder("");
17-
sb.append(s);
18-
return sb.reverse().toString().equals(s);
1913
}
14+
return ans;
15+
}
2016
}

Medium/Uncrossed Lines.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
class Solution {
2-
public int maxUncrossedLines(int[] A, int[] B) {
3-
Integer[][] dp = new Integer[A.length][B.length];
4-
return helper(A, 0, B, 0, dp);
2+
Integer[][] dp;
3+
public int maxUncrossedLines(int[] A, int[] B) {
4+
dp = new Integer[A.length][B.length];
5+
return helper(A, 0, B, 0);
6+
}
7+
8+
private int helper(int[] A, int idxA, int[] B, int idxB) {
9+
if (idxA == A.length || idxB == B.length) {
10+
return 0;
11+
}
12+
if (dp[idxA][idxB] != null) {
13+
return dp[idxA][idxB];
514
}
6-
7-
private int helper(int[] A, int i, int[] B, int j, Integer[][] dp) {
8-
if (i >= A.length || j >= B.length) {
9-
return 0;
10-
}
11-
12-
if (dp[i][j] != null) {
13-
return dp[i][j];
14-
}
15-
16-
if (A[i] == B[j]) {
17-
dp[i][j] = 1 + helper(A, i + 1, B, j + 1, dp);
18-
}
19-
else {
20-
dp[i][j] = Math.max(helper(A, i + 1, B, j, dp), helper(A, i, B, j + 1, dp));
21-
}
22-
23-
return dp[i][j];
15+
if (A[idxA] == B[idxB]) {
16+
dp[idxA][idxB] = 1 + helper(A, idxA + 1, B, idxB + 1);
2417
}
18+
else {
19+
dp[idxA][idxB] = Math.max(helper(A, idxA + 1, B, idxB), helper(A, idxA, B, idxB + 1));
20+
}
21+
return dp[idxA][idxB];
22+
}
2523
}

0 commit comments

Comments
 (0)