-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
56 changed files
with
3,535 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 0 ms submission | ||
class Solution { | ||
public int numJewelsInStones(String J, String S) { | ||
int[] arr = new int[128]; | ||
for (char ch: S.toCharArray()) { | ||
arr[ch] += 1; | ||
} | ||
int res = 0; | ||
for (char ch: J.toCharArray()) { | ||
res += arr[ch]; | ||
} | ||
return res; | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
|
||
sample 34396 kb submission | ||
class Solution { | ||
public int numJewelsInStones(String J, String S) { | ||
Map<Character, Integer> stonesByType = new HashMap<>(); | ||
for (Character c : S.toCharArray()) { | ||
if (J.indexOf(c) != -1) { | ||
stonesByType.merge(c, 1, (oldValue, newValue) -> ++oldValue); | ||
} | ||
} | ||
int result = 0; | ||
for (char c : J.toCharArray()) { | ||
if (stonesByType.containsKey(c)) { | ||
result += stonesByType.get(c); | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,145 @@ | ||
__________________________________________________________________________________________________ | ||
sample 1 ms submission | ||
class Solution { | ||
public int slidingPuzzle(int[][] board) { | ||
if (noSolution(board)) return -1; | ||
int target = 42792; // 123450 = 001 010 011 100 101 000 -> 42792 | ||
boolean[] hash = new boolean[181897]; // 543210 = 101 100 011 010 001 000 -> 181896 | ||
int start = 0; | ||
for (int[] line : board) { | ||
for (int c : line) { | ||
start = (start << 3) | c; | ||
} | ||
} | ||
if (start == target) return 0; | ||
|
||
__________________________________________________________________________________________________ | ||
int[][] move = new int[][] { { 1, 3 }, { 0, 2, 4 }, { 1, 5 }, { 0, 4 }, { 3, 1, 5 }, { 4, 2 } }; | ||
|
||
List<Integer> now = new ArrayList<>(); | ||
now.add(start); | ||
hash[start] = true; | ||
int step = 1; | ||
while (!now.isEmpty()) { | ||
List<Integer> next = new ArrayList<>(); | ||
for (int b : now) { | ||
int i = find0(b); | ||
for (int j : move[i]) { | ||
int nb = move(b, j, i); | ||
if (nb == target) return step; | ||
if (hash[nb]) continue; | ||
hash[nb] = true; | ||
next.add(nb); | ||
} | ||
} | ||
now = next; | ||
++step; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int find0(int b) { | ||
int i; | ||
for (i = 0; (b & 0x7) != 0; ++i) { | ||
b >>= 3; | ||
} | ||
return 5 - i; | ||
} | ||
|
||
int move(int b, int i, int j) { | ||
i = (5 - i) * 3; | ||
j = (5 - j) * 3; | ||
int ibit = (b >> i) & 0x7; | ||
return (b & ~(0x7 << i)) | (ibit << j); | ||
} | ||
|
||
boolean noSolution(int[][] board) { | ||
int count = 0; | ||
for (int x = 1; x < 6; ++x) { | ||
int val = board[x/3][x%3]; | ||
if (val == 0) continue; | ||
for (int y = 0; y < x; ++y) { | ||
if (board[y/3][y%3] > val) ++count; | ||
} | ||
} | ||
return (count & 1) == 1; | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
sample 36548 kb submission | ||
class Solution { | ||
public int slidingPuzzle(int[][] board) { | ||
if (board == null || board.length == 0 || board[0].length == 0) | ||
return 0; | ||
|
||
String target = "123450"; | ||
String start = ""; | ||
|
||
int row = board.length; | ||
int col = board[0].length; | ||
|
||
for (int i = 0; i < row; i++) | ||
{ | ||
for (int j = 0; j < col; j++) | ||
{ | ||
start += board[i][j] + ""; | ||
} | ||
} | ||
|
||
Queue<String> que = new LinkedList<String>(); | ||
Set<String> set = new HashSet<String>(); | ||
|
||
que.offer(start); | ||
set.add(start); | ||
int steps = 0; | ||
|
||
int[] d = new int[] {1, -1, 3, -3}; | ||
|
||
while (que.size() > 0) | ||
{ | ||
steps++; | ||
|
||
int size = que.size(); | ||
for (int s = 0; s < size; s++) | ||
{ | ||
String cur = que.poll(); | ||
if (cur.equals(target)) | ||
return steps-1; | ||
|
||
int curPos = cur.indexOf("0"); | ||
|
||
for (int k = 0; k < 4; k++) | ||
{ | ||
int nextPos = curPos + d[k]; | ||
if (isValid(board, curPos, nextPos)) | ||
{ | ||
char[] ca = cur.toCharArray(); | ||
ca[curPos] = ca[nextPos]; | ||
ca[nextPos] = '0'; | ||
String next = new String(ca); | ||
|
||
if (!set.contains(next)) | ||
{ | ||
que.offer(next); | ||
set.add(next); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private boolean isValid(int[][] board, int cur, int next) | ||
{ | ||
int row = board.length; | ||
int col = board[0].length; | ||
|
||
if (next >= 0 && next < row * col && !(cur == 2 && next == 3) && !(cur == 3 && next == 2)) | ||
return true; | ||
else | ||
return false; | ||
} | ||
} | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 1 ms submission | ||
class Solution { | ||
public boolean isIdealPermutation(int[] A) { | ||
for(int i = 0; i < A.length; i++){ | ||
if(A[i] - i < -1 || A[i] - i > 1){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
|
||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
|
||
sample 39756 kb submission | ||
class Solution { | ||
public boolean isIdealPermutation(int[] A) { | ||
for (int i = 0; i < A.length; ++i) { | ||
if (Math.abs(A[i] - i) > 1) return false; | ||
} | ||
return true; | ||
} | ||
} | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,49 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 1 ms submission | ||
class Solution { | ||
public boolean canTransform(String start, String end) { | ||
// https://www.cnblogs.com/grandyang/p/9001474.html | ||
char[] arr1 = start.toCharArray(); | ||
char[] arr2 = end.toCharArray(); | ||
int len = arr1.length, i = 0, j = 0; | ||
while(i < len && j < len) { | ||
while(i < len && arr1[i] == 'X') i++; | ||
while(j < len && arr2[j] == 'X') j++; | ||
if((i < len) ^ (j < len)) return false; | ||
if(i < len && j < len) { | ||
if(arr1[i] != arr2[j]) return false; | ||
if(arr1[i] == 'L' && i < j) return false; | ||
if(arr1[i] == 'R' && i > j) return false; | ||
i++; | ||
j++; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
|
||
sample 37240 kb submission | ||
class Solution { | ||
public boolean canTransform(String start, String end) { | ||
int i = 0, j = 0; | ||
int length = start.length(); | ||
|
||
while(i < length && j < length) | ||
{ | ||
while(i < length && start.charAt(i) == 'X') i++; | ||
while(j < length && end.charAt(j) == 'X') j++; | ||
if((i == length) && (j == length)) | ||
return true; | ||
if((i < length) ^ (j < length)) | ||
return false; | ||
char c1 = start.charAt(i); | ||
char c2 = end.charAt(j); | ||
if((c1 != c2) || (c1 == 'R') && (i>j) || (c2 == 'L') && (i<j)) | ||
return false; | ||
i++; | ||
j++; | ||
} | ||
return true; | ||
} | ||
} | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,66 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 1 ms submission | ||
class Solution { | ||
public int swimInWater(int[][] grid) { | ||
int n=grid.length,low=0,high=n*n-1,mid; | ||
while(low<high){ | ||
mid = (low+high)/2; | ||
if( possible(grid,mid) ) | ||
high = mid; | ||
else | ||
low = mid+1; | ||
} | ||
return low; | ||
} | ||
public static boolean possible(int grid[][],int time){ | ||
boolean visited[][] = new boolean[ grid.length ][ grid.length ]; | ||
return dfs( visited,grid,time,0,0 ); | ||
} | ||
public static boolean dfs( boolean v[][],int g[][],int t,int i,int j){ | ||
if( i<0 || i>=g.length || j<0 || j>=g.length || v[i][j] ) | ||
return false; | ||
v[i][j] = true; | ||
if( g[i][j] > t ) | ||
return false; | ||
if(i==g.length-1 && j==g.length-1) | ||
return true; | ||
return dfs(v,g,t,i+1,j) || dfs(v,g,t,i,j+1) || dfs(v,g,t,i-1,j) || | ||
dfs(v,g,t,i,j-1); | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
sample 36772 kb submission | ||
class Solution { | ||
|
||
public int swimInWater(int[][] grid) { | ||
int N = grid.length; | ||
Set<Integer> seen = new HashSet<Integer>(); | ||
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((k1, k2) -> | ||
grid[k1 / N][k1 % N] - grid[k2 / N][k2 % N]); | ||
pq.offer(0); | ||
int ans = -1; | ||
|
||
int[] dr = new int[]{1, -1, 0, 0}; | ||
int[] dc = new int[]{0, 0, 1, -1}; | ||
|
||
while (!pq.isEmpty()) { | ||
int k = pq.poll(); | ||
int r = k / N, c = k % N; | ||
ans = Math.max(ans, grid[r][c]); | ||
if (r == N-1 && c == N-1) return ans; | ||
|
||
for (int i = 0; i < 4; ++i) { | ||
int cr = r + dr[i], cc = c + dc[i]; | ||
int ck = cr * N + cc; | ||
if (0 <= cr && cr < N && 0 <= cc && cc < N && !seen.contains(ck)) { | ||
pq.offer(ck); | ||
seen.add(ck); | ||
} | ||
} | ||
} | ||
|
||
return ans; | ||
|
||
} | ||
} | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
0ms | ||
class Solution { | ||
public int kthGrammar(int N, int K) { | ||
return Integer.bitCount(K-1) & 1; | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
|
||
sample 31584 kb submission | ||
class Solution { | ||
public int kthGrammar(int N, int K) { | ||
if (N == 1) return 0; | ||
return (1 - (K%2)) ^ kthGrammar(N-1, (K+1)/2); | ||
} | ||
} | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,37 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
Runtime: 0 ms | ||
Memory Usage: 33.4 MB | ||
class Solution { | ||
public boolean reachingPoints(int sx, int sy, int tx, int ty) { | ||
if (sx == tx && sy == ty) { | ||
return true; | ||
} else if (tx == ty || sx > tx || sy > ty) { | ||
return false; | ||
} else if (tx > ty) { | ||
int subtract = Math.max(1, (tx - sx)/ty); | ||
return reachingPoints(sx, sy, tx - subtract * ty, ty); | ||
} else { | ||
int subtract = Math.max(1, (ty - sy)/tx); | ||
return reachingPoints(sx, sy, tx, ty - subtract * tx); | ||
} | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
|
||
sample 31820 kb submission | ||
class Solution { | ||
public boolean reachingPoints(int sx, int sy, int tx, int ty) { | ||
while (tx >= sx && ty >= sy) { | ||
if (tx == sx && ty == sy) return true; | ||
if (tx >= ty) { | ||
if (ty == sy) return (tx - sx) % ty == 0; | ||
tx = tx % ty; | ||
} else { | ||
if (tx == sx) return (ty - sy) % tx == 0; | ||
ty = ty % tx; | ||
} | ||
} | ||
if (tx == sx && ty == sy) return true; | ||
return false; | ||
} | ||
} | ||
__________________________________________________________________________________________________ |
Oops, something went wrong.