Skip to content

Instantly share code, notes, and snippets.

@Helenyin123
Helenyin123 / 340.HashMap.java
Last active October 8, 2018 02:02
340. Longest Substring with At Most K Distinct Characters
public int lengthOfLongestSubstringKDistinct(String s, int k) {
Map<Character, Integer> map = new HashMap<>();
if (s == null || s.length() == 0 || k == 0) return 0;
int start = 0;
int longest = 0;
for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i)) || map.size() < k) {
map.put(s.charAt(i), i);
} else {
int minPos = getMinPos(map);
public boolean hasGroupsSizeX(int[] deck) {
if (deck == null || deck.length <= 1) return false;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < deck.length; i++) {
int freq = map.getOrDefault(deck[i], 0);
map.put(deck[i], freq + 1);
}
int n = 0; List<Integer> lcf = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (n == 0) {
@Helenyin123
Helenyin123 / 906.Super Palindrome(1st Math).java
Last active September 23, 2018 01:02
906.Super Palindrome
class Solution {
public int superpalindromesInRange(String L, String R) {
Long l = (long)Math.sqrt(Long.parseLong(L));
Long r = (long)Math.sqrt(Long.parseLong(R));
int re = 0;
for(int odd = 0;odd <= 1;odd++){
long core = 1;
long num = getP(core,odd == 0);
while(num <= r){
if(num >= l){
You will be supplied with two data files in CSV format .
The first file contains statistics about various dinosaurs. The second file contains additional data.
Given the following formula, `speed = ((STRIDE_LENGTH / LEG_LENGTH) - 1) * SQRT(LEG_LENGTH * g)`
Where g = 9.8 m/s^2 (gravitational constant)
Write a program to read in the data files from disk, it must then print the names of only the bipedal dinosaurs from fastest to slowest.
Do not print any other information.
$ cat dataset1.csv
NAME,LEG_LENGTH,DIET
// store all the flower position to a TreeSet, each time a flower bloom, we check the distance of this flower to
// two closest flower is k or not, return the first data that satisfy the condition.
// TreeSet: Time = O(NlogN) Space = O(N)
public int kEmptySlots(int[] flowers, int k) {
if (flowers == null || k > flowers.length - 2) return -1;
int N = flowers.length;
TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < N; i++) {
set.add(flowers[i]); // include right and exclude left
Integer lower = set.lower(flowers[i]);
public int numDecodings(String s) {
if (s == null || s.length() == 0) return 0;
int[] result = new int[]{0};
dfs(s, 0, 1, result);
return result[0];
}
private void dfs(String s, int i, int count, int[] result) {
if (i == s.length()) {
count = count % 1000000007;
result[0] = (result[0] + count) % 1000000007;
@Helenyin123
Helenyin123 / 765. Couples Holding Hands(1st).java
Created January 14, 2018 11:18
765. Couples Holding Hands
bhh
@Helenyin123
Helenyin123 / 764. Largest Plus Sign(concise).java
Created January 14, 2018 05:21
764. Largest Plus Sign.java
public int orderOfLargestPlusSign(int N, int[][] mines) {
int[][] matrix = new int[N][N];
for (int i = 0; i < N; i++) {
Arrays.fill(matrix[i], 1);
}
for (int[] pair : mines) {
matrix[pair[0]][pair[1]] = 0;
}
// calculate number of consecutive 1's in four directions: up, down, left, right
int[][] left = new int[N][N];
@Helenyin123
Helenyin123 / minFactory.java
Created January 13, 2018 18:33
Houzz Interview
public int minFactory(int[] array, int range) {
int i = 0;
int count = 0;
int start = 0;
while (i < array.length && i <= range) {
if (array[i] == 1) {
start = i;
}
i++;
}
@Helenyin123
Helenyin123 / SwapWithA.java
Last active December 10, 2017 03:17
OfferUp
/**
有一个数组,里面是26个字母,顺序是乱的,有个API叫做 swich_with_a(int pos), 这个方法的作用是调换pos位置上的字母和字母”a“的位置
(无论”a”在哪里,这个方法会把“a“换到pos的位置)。让你实现把这26个字母排序,但是只能用到这个swith_with_a来调换位置。
遇到一个char[i]有两个思路:1. 把char[i]换到出去,到它应该在的位置上去,但这样不能让char[i]本身是对的,走完一遍后结果还是不对
2. 不管char[i]是什么,我只要把字母i + 'a'换进来,换到位置i上就行,这样就保证了那个位置的字母一定是对的
*/
public char[] sortChar(char[] array) {
// record each character's index
int[] index = new int[26];