This file contains hidden or 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
/* | |
<https://stackoverflow.com/questions/40296831/is-it-possible-to-force-a-copy-of-a-protected-google-doc> | |
NOTE - 2021-05-24 | |
----------------- | |
The script below isn't the fastest way to copy-and-paste from a protected | |
Google Doc. Before trying it, I'd suggest following MikoFrosty's advice from | |
the comments: |
This file contains hidden or 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
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.PriorityQueue; | |
import java.util.Queue; | |
public class FoodRatings { | |
private class Food implements Comparable<Food>{ | |
// skip boilerplate getter/setters | |
public String food; |
This file contains hidden or 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
public int minPathCost(int[][] grid, int[][] moveCost) { | |
if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0 || moveCost == null) { | |
return -1; // or throw exception | |
} | |
int[][] minCost = new int[grid.length][grid[0].length]; | |
for (int i = 0; i < minCost[0].length; i++) { | |
minCost[0][i] = grid[0][i]; // first row costs are the init values | |
} |
This file contains hidden or 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
public double angleClock(int hour, int minutes) { | |
if (hour == 12) { | |
hour = 0; | |
} | |
double angle = Math.abs(minutes * 6 - (hour + (double)minutes / 60) * 30); | |
if (angle > 180) { | |
angle = 360 - angle; | |
} | |
return angle; |
This file contains hidden or 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
public int numIdenticalPairsWithSort(int[] nums) { | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
Arrays.sort(nums); | |
int goodPairsCount = 0; | |
int start = 0; | |
int end = 0; |
This file contains hidden or 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
public int numIdenticalPairs(int[] nums) { | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
// key: int value; value: number of occurrence | |
Map<Integer, Integer> lookup = new HashMap<>(); | |
int goodPairsCount = 0; | |
for (int i : nums) { | |
if (lookup.containsKey(i)) { |
This file contains hidden or 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
public int rob(int[] nums) { | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
if (nums.length == 1) { | |
return nums[0]; | |
} | |
// rob first house, so need to remove the last house | |
int[] rob_first_nums = new int[nums.length - 1]; |
This file contains hidden or 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
public int rob(int[] num) { | |
if (num == null || num.length == 0) { | |
return 0; | |
} | |
int n = num.length; | |
int[] lookup = new int[n + 1]; // DP array size normally larger than 1 | |
lookup[0] = 0; | |
lookup[1] = num[0]; |
This file contains hidden or 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
private class DisjointSet { | |
private Map<String, String> lookup = new HashMap<>(); | |
public void init(int row, int col) { | |
for (int i = 0; i < row; i++) { | |
for (int j = 0; j < col; j++) { | |
String key = i + "," + j; | |
// initially key value is the same, meaning the parent of the disjoint set is itself, i.e., isolated or not initialized | |
this.lookup.put(key, key); | |
} |
This file contains hidden or 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
private final int[] xDirection = {1, 0, -1, 0}; | |
private final int[] yDirection = {0, -1, 0, 1 }; | |
public int numIslands(char[][] grid) { | |
if (grid == null || grid.length == 0 || grid[0].length == 0) { | |
return 0; | |
} | |
int m = grid.length; | |
int n = grid[0].length; |
NewerOlder