Skip to content
Permalink
Browse files

Toggled some more shit

  • Loading branch information...
ttommytang committed Apr 2, 2018
1 parent 6963e67 commit 124c63f9e7fc08b27ed4e424bfbd00cb40ebca0d
Showing with 192 additions and 98 deletions.
  1. +0 −94 src/Basic.java
  2. +0 −3 src/BricksFallingWhenHit.java
  3. +105 −1 src/HitAndFall.java
  4. +87 −0 src/WordBreak.java

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -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;
}
}
}
@@ -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.
You can’t perform that action at this time.