Skip to content

Commit 681b66f

Browse files
authored
Added solutions and tests for problems 52 - 71.
1 parent 68726e8 commit 681b66f

File tree

40 files changed

+949
-0
lines changed

40 files changed

+949
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package s0052_n_queens_ii;
2+
3+
public class Solution {
4+
public int totalNQueens(int n) {
5+
boolean[] row = new boolean[n];
6+
boolean[] col = new boolean[n];
7+
boolean[] diagonal = new boolean[n + n - 1];
8+
boolean[] antiDiagonal = new boolean[n + n - 1];
9+
10+
return totalNQueens(n, 0, row, col, diagonal, antiDiagonal);
11+
}
12+
13+
public static int totalNQueens(
14+
int n,
15+
int r,
16+
boolean[] row,
17+
boolean[] col,
18+
boolean[] diagonal,
19+
boolean[] antiDiagonal) {
20+
if (r == n) return 1;
21+
22+
int count = 0;
23+
for (int c = 0; c < n; c++) {
24+
if (!row[r] && !col[c] && !diagonal[r + c] && !antiDiagonal[r - c + n - 1]) {
25+
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = true;
26+
count += totalNQueens(n, r + 1, row, col, diagonal, antiDiagonal);
27+
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = false;
28+
}
29+
}
30+
return count;
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package s0053_maximum_subarray;
2+
3+
public class Solution {
4+
public int maxSubArray(int[] nums) {
5+
int maxi = Integer.MIN_VALUE;
6+
int sum = 0;
7+
for (int i = 0; i < nums.length; i++) {
8+
// calculating sub-array sum
9+
sum += nums[i];
10+
maxi = Math.max(sum, maxi);
11+
if (sum < 0) {
12+
// there is no point to carry a -ve subarray sum. hence setting to 0
13+
sum = 0;
14+
}
15+
}
16+
return maxi;
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package s0054_spiral_matrix;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Solution {
7+
public List<Integer> spiralOrder(int[][] matrix) {
8+
List<Integer> list = new ArrayList<>();
9+
int r = 0;
10+
int c = 0;
11+
int bigR = matrix.length - 1;
12+
int bigC = matrix[0].length - 1;
13+
while (r <= bigR && c <= bigC) {
14+
for (int i = c; i <= bigC; i++) {
15+
list.add(matrix[r][i]);
16+
}
17+
r++;
18+
for (int i = r; i <= bigR; i++) {
19+
list.add(matrix[i][bigC]);
20+
}
21+
bigC--;
22+
for (int i = bigC; i >= c && r <= bigR; i--) {
23+
list.add(matrix[bigR][i]);
24+
}
25+
bigR--;
26+
for (int i = bigR; i >= r && c <= bigC; i--) {
27+
list.add(matrix[i][c]);
28+
}
29+
c++;
30+
}
31+
return list;
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package s0055_jump_game;
2+
3+
public class Solution {
4+
public boolean canJump(int[] nums) {
5+
int sz = nums.length;
6+
// we set 1 so it won't break on the first iteration
7+
int tmp = 1;
8+
for (int i = 0; i < sz; i++) {
9+
// we always deduct tmp for every iteration
10+
tmp--;
11+
if (tmp < 0) {
12+
// if from previous iteration tmp is already 0, it will be <0 here
13+
// leading to false value
14+
return false;
15+
}
16+
// we get the maximum value because this value is supposed
17+
// to be our iterator, if both values are 0, then the next
18+
// iteration we will return false
19+
// if either both or one of them are not 0 then we will keep doing this and check.
20+
21+
// We can stop the whole iteration with this condition. without this condition the code
22+
// runs in 2ms 79.6%, adding this condition improves the performance into 1ms 100%
23+
// because if the test case jump value is quite large, instead of just iterate, we can
24+
// just check using this condition
25+
// example: [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] -> we can just jump to the end without
26+
// iterating whole array
27+
tmp = Math.max(tmp, nums[i]);
28+
if (i + tmp >= sz - 1) {
29+
return true;
30+
}
31+
}
32+
// we can just return true at the end, because if tmp is 0 on previous
33+
// iteration,
34+
// even though the next iteration index is the last one, it will return false under the
35+
// tmp<0 condition
36+
return true;
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package s0056_merge_intervals;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Solution {
8+
public int[][] merge(int[][] intervals) {
9+
// sorting
10+
// so that we can perform the task linearly
11+
Arrays.sort(
12+
intervals,
13+
(int[] a, int[] b) -> {
14+
if (a[0] == b[0]) return Integer.compare(a[1], b[1]);
15+
return Integer.compare(a[0], b[0]);
16+
});
17+
18+
List<List<Integer>> list = new ArrayList<>();
19+
int i = 0;
20+
while (i < intervals.length) {
21+
// storing start
22+
int start = intervals[i][0];
23+
int end = intervals[i][1];
24+
i++;
25+
while (i < intervals.length && intervals[i][0] <= end) {
26+
// making sure range is not shrinking
27+
if (intervals[i][1] > end) {
28+
end = intervals[i][1];
29+
}
30+
i++;
31+
}
32+
List<Integer> temp = new ArrayList<>();
33+
temp.add(start);
34+
temp.add(end);
35+
list.add(temp);
36+
}
37+
int[][] arr = new int[list.size()][2];
38+
i = 0;
39+
for (List<Integer> l : list) {
40+
arr[i][0] = l.get(0);
41+
arr[i][1] = l.get(1);
42+
i++;
43+
}
44+
return arr;
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package s0057_insert_interval;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public int[][] insert(int[][] intervals, int[] newInterval) {
7+
int n = intervals.length;
8+
int l = 0;
9+
int r = n - 1;
10+
while (l < n && newInterval[0] > intervals[l][1]) {
11+
l++;
12+
}
13+
while (r >= 0 && newInterval[1] < intervals[r][0]) {
14+
r--;
15+
}
16+
17+
int[][] res = new int[l + n - r][2];
18+
for (int i = 0; i < l; i++) {
19+
res[i] = Arrays.copyOf(intervals[i], intervals[i].length);
20+
}
21+
res[l][0] = Math.min(newInterval[0], l == n ? newInterval[0] : intervals[l][0]);
22+
res[l][1] = Math.max(newInterval[1], r == -1 ? newInterval[1] : intervals[r][1]);
23+
for (int i = l + 1, j = r + 1; j < n; i++, j++) {
24+
res[i] = intervals[j];
25+
}
26+
27+
return res;
28+
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package s0058_length_of_last_word;
2+
3+
public class Solution {
4+
public int lengthOfLastWord(String str) {
5+
int len = 0;
6+
for (int i = str.length() - 1; i >= 0; i--) {
7+
char ch = str.charAt(i);
8+
if (ch == ' ' && len > 0) {
9+
break;
10+
} else if (ch != ' ') {
11+
len++;
12+
}
13+
}
14+
return len;
15+
}
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package s0059_spiral_matrix_ii;
2+
3+
public class Solution {
4+
public int[][] generateMatrix(int n) {
5+
int num = 1;
6+
int rStart = 0;
7+
int rEnd = n - 1;
8+
int cStart = 0;
9+
int cEnd = n - 1;
10+
int[][] spiral = new int[n][n];
11+
while (rStart <= rEnd && cStart <= cEnd) {
12+
for (int k = cStart; k <= cEnd; k++) {
13+
spiral[rStart][k] = num++;
14+
}
15+
rStart++;
16+
for (int k = rStart; k <= rEnd; k++) {
17+
spiral[k][cEnd] = num++;
18+
}
19+
cEnd--;
20+
if (rStart <= rEnd) {
21+
for (int k = cEnd; k >= cStart; k--) {
22+
spiral[rEnd][k] = num++;
23+
}
24+
}
25+
rEnd--;
26+
if (cStart <= cEnd) {
27+
for (int k = rEnd; k >= rStart; k--) {
28+
spiral[k][cStart] = num++;
29+
}
30+
}
31+
cStart++;
32+
}
33+
return spiral;
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package s0060_permutation_sequence;
2+
3+
public class Solution {
4+
public String getPermutation(int n, int k) {
5+
char[] res = new char[n];
6+
k = k - 1; // We want the permutation sequence to be zero-indexed
7+
8+
int a = (1 << n) - 1; // The set bits indicate the available digits
9+
int m = 1;
10+
for (int i = 2; i < n; i++) m *= i; // m = (n - 1)!
11+
12+
for (int i = 0; i < n; i++) {
13+
int b = a;
14+
for (int j = 0; j < k / m; j++) b &= b - 1;
15+
b &= ~b + 1; // b is the bit corresponding to the digit we want
16+
17+
res[i] = (char) ('1' + Integer.bitCount(b - 1));
18+
19+
a &= ~b; // Remove b from the set of available digits
20+
k %= m;
21+
m /= Math.max(1, n - i - 1);
22+
}
23+
24+
return String.valueOf(res);
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package s0061_rotate_list;
2+
3+
import com_github_leetcode.ListNode;
4+
5+
public class Solution {
6+
public ListNode rotateRight(ListNode head, int k) {
7+
if (head == null || k == 0) return head;
8+
ListNode tail = head;
9+
// find the count and let tail points to last node
10+
int count = 1;
11+
while (tail != null && tail.next != null) {
12+
count++;
13+
tail = tail.next;
14+
}
15+
16+
// calculate number of times to rotate by count modulas
17+
int times = k % count;
18+
// if K=0 then return original
19+
if (times == 0) return head;
20+
ListNode temp = head;
21+
// iterate and go to the K+1 th node from the end or count - K - 1 node from
22+
// start
23+
for (int i = 1; i <= count - times - 1 && temp != null; i++) {
24+
temp = temp.next;
25+
}
26+
ListNode newHead = null;
27+
if (temp != null && tail != null) {
28+
newHead = temp.next;
29+
temp.next = null;
30+
tail.next = head;
31+
}
32+
33+
return newHead;
34+
}
35+
}

0 commit comments

Comments
 (0)