Skip to content

Instantly share code, notes, and snippets.

View monkeylyf's full-sized avatar

Yifeng Liu monkeylyf

  • Google
  • Bay area
View GitHub Profile
@monkeylyf
monkeylyf / add.py
Created November 28, 2018 09:56
Add list of matrix
def add(matrix1, matrix2):
m1 = len(matrix1)
n1 = len(matrix1[0])
m2 = len(matrix2)
n2 = len(matrix2[0])
if m1 != m2 or n1 != n2:
raise ValueError("Given matrices are not the same size.")
result = [[None for _ in range(n1)] for _ in range(m1)]
@monkeylyf
monkeylyf / add.py
Created November 28, 2018 09:56
Add list of matrix
def add(matrix1, matrix2):
m1 = len(matrix1)
n1 = len(matrix1[0])
m2 = len(matrix2)
n2 = len(matrix2[0])
if m1 != m2 or n1 != n2:
raise ValueError("Given matrices are not the same size.")
result = [[None for _ in range(n1)] for _ in range(m1)]
@monkeylyf
monkeylyf / merge_sort.cpp
Created November 30, 2015 23:07
Merge sort
/*
* Merge sort in cpp.
*
*/
#include <stdio.h>
#include <vector>
using std::vector;
@monkeylyf
monkeylyf / Shuffle_Pattern.py
Created January 6, 2015 21:22
Shuffle_Pattern.py
"""google_Shuffle_Pattern.py
Given a hand of cards, and you need to shuffle it. Assume that you always shuffle
the cards exactly the same way.
Will the cards reach the original state if you keep shuffling it? And if so, given
the shuffle pattern, how many times it will take to shuffle it back to its original
state?
Pattern is given as a integer array A, with no duplicates.
@monkeylyf
monkeylyf / Inna_and_Dima.java
Created November 24, 2014 04:33
codeforces_Inna_and_Dima.java
/**
* codeforces_Inna_and_Dima.
*
* http://codeforces.com/contest/374/problem/C
*/
import java.util.HashMap;
import java.util.Map;
"""codeforces_Inna_And_Pink_Pony.py
http://codeforces.com/contest/374/problem/A
"""
def reachable(args):
(x, y, i, j, a, b) = args
div_x, mod_x = divmod(abs(x - i), a)
if mod_x != 0:
return None
@monkeylyf
monkeylyf / favorate_seq.py
Created November 19, 2014 22:56
fuck me. WA???
"""hackerrank_Favorite_Sequence.py
https://www.hackerrank.com/contests/w12/challenges/favourite-sequence
"""
from pprint import pprint as p
def toposort(graph):
""""""
# Start of the inner function.
@monkeylyf
monkeylyf / continental_divide.java
Created November 7, 2014 21:42
continental_divide.java
/**
* Continental_Divide.
*
* Given a geo map, represented as a two-dimension matrix with each
* element as the height value,
*
* Define both left and upper borders adjacent to pacific ocean, and both
* right and down boaders adjacent to atlantic ocean. Also define the
* continental divide as a location, that have flows from itself to both
* pacific ocean and atlantic ocean. At last, define a flow, think it as
@monkeylyf
monkeylyf / Minimum_Distance_Sum_To_K_Points_In_Maze.java
Created November 7, 2014 02:01
Minimum_Distance_Sum_To_K_Points_In_Maze.java
/**
* Minimum_Distance_Sum_To_K_Points_In_Maze.
*
* Given a maze, represented as a matrix (1: wall, 0: path) and k points, find
* the point that the sum of distances from this point to k points is minimum.
*
* If there are more than one point, find all of them.
*/
import java.util.ArrayList;
@monkeylyf
monkeylyf / pinball_maze.py
Created November 5, 2014 04:17
pinball maze
"""pinabll_maze
Find the exit of the maze for Mr.Pinball.
Assuming: Maze is represented as a matrix, 0: path, 1: wall.
Entrance is at ?? and exit is at ??.
There will be at least one path leads to the exit of the maze.
"""
class Solution(object):