Skip to content

Commit

Permalink
Toggled some more shit
Browse files Browse the repository at this point in the history
  • Loading branch information
ttommytang committed Apr 2, 2018
1 parent 6963e67 commit 124c63f
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 98 deletions.
94 changes: 0 additions & 94 deletions src/Basic.java

This file was deleted.

3 changes: 0 additions & 3 deletions src/BricksFallingWhenHit.java

This file was deleted.

106 changes: 105 additions & 1 deletion src/HitAndFall.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,115 @@
import java.util.*;

public class HitAndFall {
public static void main(String[] args) {

int[][] wall1 = {{1,0,0,0}, {1,1,1,0}};
int[][] wall2 = {{1,0,0,0}, {1,1,0,0}};

int[][] hits1 = {{1,3}, {1,1}, {0,0}, {1,0}};
int[][] hits2 = {{1,1}, {1,0}};

HitAndFall test = new HitAndFall();

int[] falls1 = test.hitBricks(wall1, hits1);
int[] falls2 = test.hitBricks(wall2, hits2);
}

private int[] dirs = new int[]{0, -1, 0, 1, 0};
public int[] hitBricks(int[][] grid, int[][] hits) {
int[] res = new int[hits.length];
int rows = grid.length, cols = grid[0].length;

int[][] A = new int[rows][cols];
for (int r = 0; r < rows; r++)
A[r] = grid[r].clone();

for (int[] hit : hits)
A[hit[0]][hit[1]] = 0;

// DSU need to be size of R*C + 1, last union for roof
DSU dsu = new DSU(rows * cols + 1);

// Build DSU based on post-hit grid
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (A[r][c] == 1) {
int idx = r * cols + c;
if (r == 0)
dsu.union(idx, rows * cols);
if (r > 0 && A[r - 1][c] == 1)
dsu.union(idx, (r - 1)*cols + c);
if (c > 0 && A[r][c - 1] == 1)
dsu.union(idx, r * cols + c - 1);
}
}
}

int t = hits.length - 1;
while(t >= 0) {
int hitR = hits[t][0];
int hitC = hits[t][1];

int postRoofUnionSize = dsu.roofUnionSize();
if (grid[hitR][hitC] == 1) {
int hitIndex = hitR * cols + hitC;
for (int i = 0; i < 4; i++) {
int exploreR = hitR + dirs[i];
int exploreC = hitC + dirs[i+1];

if (exploreR >= 0 && exploreR < rows && exploreC >= 0 && exploreC < cols && A[exploreR][exploreC] == 1)
dsu.union(hitIndex, exploreR * cols + exploreC);
}
if (hitR == 0)
dsu.union(hitIndex, rows * cols);
A[hitR][hitC] = 1;
res[t] = Math.max(dsu.roofUnionSize() - postRoofUnionSize - 1, 0);
}
t--;
}
return res;
}

static class DSU {
int[] parents;
int[] rank;
int[] sz;

DSU(int N) {
parents = new int[N];
for (int i = 0; i < N; i++)
parents[i] = i;
rank = new int[N];
sz = new int[N];
Arrays.fill(sz, 1);
}

int find(int x) {
if (x == parents[x]) return x;
return parents[x] = find(parents[x]);
}

void union(int x, int y) {
int xParent = find(x);
int yParent = find(y);

if (xParent == yParent) return;
if (rank[xParent] < rank[yParent]) {
int temp = yParent;
yParent = xParent;
xParent = temp;
}
if (rank[xParent] == rank[yParent]) rank[xParent]++;

parents[yParent] = xParent;
sz[xParent] += sz[yParent];
}

int unionSize(int x) {
return sz[find(x)];
}

int roofUnionSize() {
return unionSize(sz.length - 1) - 1;
}
}
}
87 changes: 87 additions & 0 deletions src/WordBreak.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,94 @@
import java.util.*;

public class WordBreak {
public static void main(String[] args) {
WordBreak test = new WordBreak();

String s = "catsanddog";

String[] words = {"cat", "cats", "and", "sand", "dog"};
List<String> dict = Arrays.asList(words);

// boolean isSub = test.wordBreak(s, dict);
// List<String> sentences = test.wordBreakReturnSentences(s, dict);

List<String> sentences = test.wordBreakWithMEM(s, dict, 0);
}

public boolean wordBreak(String s, List<String> wordDict) {
if (s == null || wordDict == null) return false;

boolean[] dp = new boolean[s.length()];

for(int i = 1; i <= s.length(); i++) {
if (wordDict.contains(s.substring(0, i))) {
dp[i-1] = true;
continue;
}

for(int j = i - 1; j > 0; j--) {
if(dp[j - 1] && wordDict.contains(s.substring(j, i))) {
dp[i-1] = true;
break;
}
}
}

return dp[s.length() - 1];
}

public List<String> wordBreakReturnSentences(String s, List<String> wordDict) {
List<String> res = new ArrayList<>();
if (s == null || wordDict == null)
return res;

int used = 0;
StringBuilder current = new StringBuilder("");

wordBreakHelper(s, wordDict, used, current, res);

return res;
}

private void wordBreakHelper(String s, List<String> wordDict, int used, StringBuilder current, List<String> res) {
if(used >= s.length()) {
current.setLength(current.length() - 1);
res.add(current.toString());
return;
}

for (int i = used + 1; i <= s.length(); i++) {
if (wordDict.contains(s.substring(used, i))) {
int oldLength = current.length();
current.append(s.substring(used, i));
current.append(' ');
wordBreakHelper(s, wordDict, i, current, res);
current.setLength(oldLength);
}
}
}

HashMap<Integer, List<String>> wordBreakMem = new HashMap<>();

private List<String> wordBreakWithMEM(String s, List<String> wordDict, int used) {
// MEM for post fix string partitions
if (wordBreakMem.containsKey(used))
return wordBreakMem.get(used);


List<String> res = new ArrayList<>();
if (used == s.length())
res.add("");

for (int i = used + 1; i <= s.length(); i++) {
if (wordDict.contains(s.substring(used, i))) {
List<String> posts = wordBreakWithMEM(s, wordDict, i);
for (String post : posts) {
res.add(s.substring(used, i) + (post.equals("") ? "" : " ") + post);
}
}
}
wordBreakMem.put(used, res);
return res;
}
}

0 comments on commit 124c63f

Please sign in to comment.