Skip to content

Commit b0982c4

Browse files
authored
Merge pull request #96 from Xtha-Sunil/hard
Hard Difficulaty
2 parents 927db7a + 35ec5d3 commit b0982c4

6 files changed

Lines changed: 361 additions & 0 deletions
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
3+
public ListNode mergeKLists(ListNode[] lists) {
4+
int len = lists.length;
5+
if (len == 0) return null;
6+
7+
int min = Integer.MAX_VALUE;
8+
int max = Integer.MIN_VALUE;
9+
10+
for (ListNode list : lists) {
11+
while (list != null) {
12+
int val = list.val;
13+
max = Math.max(max, val);
14+
min = Math.min(min, val);
15+
list = list.next;
16+
}
17+
}
18+
19+
int range = max - min + 1;
20+
ListNode bucketList[] = new ListNode[range];
21+
22+
for (ListNode list : lists) {
23+
while (list != null) {
24+
ListNode nextNode = list.next;
25+
26+
int bucketIdx = list.val - min;
27+
list.next = bucketList[bucketIdx];
28+
bucketList[bucketIdx] = list;
29+
30+
list = nextNode;
31+
}
32+
}
33+
34+
ListNode newHead = new ListNode(0);
35+
ListNode dummy = newHead;
36+
37+
for (ListNode list : bucketList) {
38+
dummy.next = list;
39+
40+
while (dummy.next != null) {
41+
dummy = dummy.next;
42+
}
43+
}
44+
45+
return newHead.next;
46+
}
47+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
class Solution {
2+
3+
private static final int SIZE = 9;
4+
private static final int BLOCK_SIZE = 3;
5+
private static final char EMPTY_CELL = '.';
6+
private static final int allBits = 0x1ff;
7+
private static final int[] bitFlags = new int[] {
8+
0x1,
9+
0x1 << 1,
10+
0x1 << 2,
11+
0x1 << 3,
12+
0x1 << 4,
13+
0x1 << 5,
14+
0x1 << 6,
15+
0x1 << 7,
16+
0x1 << 8,
17+
};
18+
19+
private char[][] board;
20+
private int[] rows;
21+
private int[] cols;
22+
private int[] blocks;
23+
private int totalCount = 0;
24+
25+
public void solveSudoku(char[][] board) {
26+
if (
27+
board == null || (board.length != SIZE && board[0].length != SIZE)
28+
) throw new IllegalArgumentException();
29+
this.board = board;
30+
initialize();
31+
initialUpdate();
32+
bruteForce();
33+
}
34+
35+
private void initialize() {
36+
rows = new int[SIZE];
37+
cols = new int[SIZE];
38+
blocks = new int[SIZE];
39+
totalCount = 0;
40+
}
41+
42+
private boolean bruteForce() {
43+
if (totalCount >= SIZE * SIZE) return true;
44+
int row = -1;
45+
int col = -1;
46+
47+
int min = SIZE + 1;
48+
for (int r = 0; r < SIZE && min > 1; ++r) {
49+
for (int c = 0; c < SIZE; ++c) {
50+
if (board[r][c] != EMPTY_CELL) continue;
51+
int candidateCounts = getCandidateCount(r, c);
52+
if (candidateCounts < min) {
53+
min = candidateCounts;
54+
row = r;
55+
col = c;
56+
}
57+
if (min == 1) break;
58+
}
59+
}
60+
if (min < 1) return false;
61+
int candidates = getCandidates(row, col);
62+
63+
for (int i = 0; i < SIZE && candidates != 0; ++i, candidates >>= 1) {
64+
if (candidates % 2 == 0) continue;
65+
mark(i, row, col);
66+
board[row][col] = toChar(i);
67+
if (bruteForce()) return true;
68+
board[row][col] = EMPTY_CELL;
69+
unmark(i, row, col);
70+
}
71+
return false;
72+
}
73+
74+
private int getCandidates(int row, int col) {
75+
int blkId = (row / BLOCK_SIZE) * BLOCK_SIZE + col / BLOCK_SIZE;
76+
return ~(rows[row] | cols[col] | blocks[blkId]) & allBits;
77+
}
78+
79+
private int getCandidateCount(int row, int col) {
80+
int candidates = getCandidates(row, col);
81+
int count = 0;
82+
for (int i = 0; candidates != 0; ++i, candidates = candidates & (candidates - 1)) ++count;
83+
return count;
84+
}
85+
86+
private void mark(int value, int row, int col) {
87+
int blkId = (row / BLOCK_SIZE) * BLOCK_SIZE + col / BLOCK_SIZE;
88+
int flag = bitFlags[value];
89+
rows[row] |= flag;
90+
cols[col] |= flag;
91+
blocks[blkId] |= flag;
92+
++totalCount;
93+
}
94+
95+
private void unmark(int value, int row, int col) {
96+
int blkId = (row / BLOCK_SIZE) * BLOCK_SIZE + col / BLOCK_SIZE;
97+
int mask = ~bitFlags[value];
98+
rows[row] &= mask;
99+
cols[col] &= mask;
100+
blocks[blkId] &= mask;
101+
--totalCount;
102+
}
103+
104+
private void initialUpdate() {
105+
for (int r = 0; r < SIZE; ++r) {
106+
for (int c = 0; c < SIZE; ++c) {
107+
if (board[r][c] == EMPTY_CELL) continue;
108+
int value = fromChar(board[r][c]);
109+
mark(value, r, c);
110+
}
111+
}
112+
}
113+
114+
private static int fromChar(char c) {
115+
return c - '1';
116+
}
117+
118+
private static char toChar(int i) {
119+
return (char) (i + '1');
120+
}
121+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
3+
public int trap(int[] height) {
4+
int left = 0,
5+
right = height.length - 1;
6+
int leftmax = 0,
7+
rightmax = 0,
8+
water = 0;
9+
10+
while (left < right) {
11+
if (height[left] < height[right]) {
12+
if (height[left] >= leftmax) leftmax = height[left];
13+
else water += leftmax - height[left];
14+
left++;
15+
} else {
16+
if (height[right] >= rightmax) {
17+
rightmax = height[right];
18+
} else water += rightmax - height[right];
19+
right--;
20+
}
21+
}
22+
return water;
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
3+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
4+
if (nums1.length > nums2.length) {
5+
return findMedianSortedArrays(nums2, nums1);
6+
}
7+
8+
int x = nums1.length;
9+
int y = nums2.length;
10+
int low = 0,
11+
high = x;
12+
13+
while (low <= high) {
14+
int partitionX = (low + high) / 2;
15+
int partitionY = (x + y + 1) / 2 - partitionX;
16+
17+
int maxLeftX = (partitionX == 0) ? Integer.MIN_VALUE : nums1[partitionX - 1];
18+
int minRightX = (partitionX == x) ? Integer.MAX_VALUE : nums1[partitionX];
19+
20+
int maxLeftY = (partitionY == 0) ? Integer.MIN_VALUE : nums2[partitionY - 1];
21+
int minRightY = (partitionY == y) ? Integer.MAX_VALUE : nums2[partitionY];
22+
23+
if (maxLeftX <= minRightY && maxLeftY <= minRightX) {
24+
if ((x + y) % 2 == 0) {
25+
return (Math.max(maxLeftX, maxLeftY) + Math.min(minRightX, minRightY)) / 2.0;
26+
} else {
27+
return Math.max(maxLeftX, maxLeftY);
28+
}
29+
} else if (maxLeftX > minRightY) {
30+
high = partitionX - 1; // move left
31+
} else {
32+
low = partitionX + 1; // move right
33+
}
34+
}
35+
36+
throw new IllegalArgumentException("Input arrays not sorted properly");
37+
}
38+
}

leetcode/hard/51_n-queens.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// class Solution {
2+
// public List<List<String>> solveNQueens(int n) {
3+
// List<List<String>> results = new ArrayList<>();
4+
// boolean[][] board = new boolean[n][n];
5+
// backtrack(board, 0 ,results);
6+
// return results;
7+
// }
8+
// private void backtrack(boolean[][] board, int row, List<List<String>> results){
9+
// if(row == board.length){
10+
// results.add(createBoard(board));
11+
// return;
12+
// }
13+
// for(int col=0; col<board.length; col++){
14+
// if(isSafe(board,row,col)){
15+
// board[row][col] = true;
16+
// backtrack(board, row+1, results);
17+
// board[row][col] = false;
18+
// }
19+
// }
20+
// }
21+
22+
// private boolean isSafe(boolean[][] board,int row,int col){
23+
// for(int i=0; i < row; i++){
24+
// if(board[i][col]) return false;
25+
// }
26+
27+
// for(int i=row-1, j=col-1; i>=0 && j>=0; i--, j--){
28+
// if(board[i][j]) return false;
29+
// }
30+
31+
// for(int i=row-1, j=col+1; i>=0 && j < board.length; i--, j++ ){
32+
// if(board[i][j]) return false;
33+
// }
34+
35+
// return true;
36+
// }
37+
38+
// private List<String> createBoard(boolean[][] board){
39+
// List<String> b = new ArrayList<>();
40+
// for(int i=0; i<board.length; i++){
41+
// StringBuilder row = new StringBuilder();
42+
// for(int j=0; j<board.length; j++){
43+
// row.append(board[i][j] ? 'Q' : '.');
44+
// }
45+
// b.add(row.toString());
46+
// }
47+
// return b;
48+
// }
49+
// }
50+
51+
class Solution {
52+
53+
private int n;
54+
private int fullMask;
55+
private int[] queens; // queens[row] = col position
56+
private String[] templates; // templates[c] = board row with 'Q' at c
57+
private List<List<String>> results;
58+
59+
public List<List<String>> solveNQueens(int n) {
60+
this.n = n;
61+
this.fullMask = (1 << n) - 1;
62+
this.queens = new int[n];
63+
this.results = new ArrayList<>();
64+
buildTemplates();
65+
backtrack(0, 0, 0, 0);
66+
return results;
67+
}
68+
69+
// Pre-build n Strings like "...Q.." for each column position
70+
private void buildTemplates() {
71+
templates = new String[n];
72+
char[] row = new char[n];
73+
for (int c = 0; c < n; c++) {
74+
Arrays.fill(row, '.');
75+
row[c] = 'Q';
76+
templates[c] = new String(row);
77+
}
78+
}
79+
80+
/**
81+
* @param row which row we're filling
82+
* @param colMask occupied columns bitmap
83+
* @param d1Mask occupied (row+col) diagonals bitmap
84+
* @param d2Mask occupied (row-col+n-1) diagonals bitmap
85+
*/
86+
private void backtrack(int row, int colMask, int d1Mask, int d2Mask) {
87+
if (row == n) {
88+
// build solution by referencing pre-made row‑strings
89+
List<String> sol = new ArrayList<>(n);
90+
for (int r = 0; r < n; r++) {
91+
sol.add(templates[queens[r]]);
92+
}
93+
results.add(sol);
94+
return;
95+
}
96+
// bits free in this row
97+
int avail = fullMask & ~(colMask | d1Mask | d2Mask);
98+
while (avail != 0) {
99+
int bit = avail & -avail; // rightmost free column
100+
avail ^= bit; // remove it
101+
int col = Integer.numberOfTrailingZeros(bit);
102+
queens[row] = col;
103+
104+
backtrack(row + 1, colMask | bit, (d1Mask | bit) << 1, (d2Mask | bit) >>> 1);
105+
// no need to unset queens[row], it'll be overwritten
106+
}
107+
}
108+
}

leetcode/hard/65_valid-number.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
3+
public boolean isNumber(String s) {
4+
boolean isdot = false,
5+
ise = false,
6+
nums = false;
7+
for (int i = 0; i < s.length(); i++) {
8+
char c = s.charAt(i);
9+
if (Character.isDigit(c)) nums = true;
10+
else if (c == '+' || c == '-') {
11+
if (i > 0 && s.charAt(i - 1) != 'e' && s.charAt(i - 1) != 'E') return false;
12+
} else if (c == 'e' || c == 'E') {
13+
if (ise || !nums) return false;
14+
ise = true;
15+
nums = false;
16+
} else if (c == '.') {
17+
if (isdot || ise) return false;
18+
isdot = true;
19+
} else return false;
20+
}
21+
return nums;
22+
}
23+
}

0 commit comments

Comments
 (0)