Skip to content

Instantly share code, notes, and snippets.

View zjplab's full-sized avatar
🏠
Working from home

JP Zhang zjplab

🏠
Working from home
View GitHub Profile
import torch
import torch.nn.functional as F
from tqdm import tqdm
class Gaussian(object):
def __init__(self, mu, rho):
super().__init__()
self.mu = mu
self.rho = rho
// ==UserScript==
// @name leetcode题解助手
// @namespace leetcode_helper
// @version 1.2.0
// @description 查找leetcode题解(九章算法的题解搜索,C++答案搜索, 提供Github上开源的python题解的一键查询按钮,可以直接在谷歌搜索题目名称,还能切换语言)
// @author sherpahu & zjplab
// @icon https://assets.leetcode.com/static_assets/public/webpack_bundles/images/logo-dark.e99485d9b.svg
// @include *://leetcode-cn.com/problems/*
// @include *://leetcode.com/problems/*
/************************************/
@zjplab
zjplab / BinarySearchTree_another_version.py
Last active December 15, 2022 22:19
Binary Tree Python Implementation
# Python program to demonstrate delete operation
# in binary search tree
# A Binary Tree Node
class Node:
# Constructor to create a new node
def __init__(self, key):
self.key = key
self.left = None
@zjplab
zjplab / permutation.py
Created March 4, 2017 12:13
all permutations of a given string
# Python program to print all permutations with
# duplicates allowed
def toString(List):
return ''.join(List)
# Function to print permutations of string
# This function takes three parameters:
# 1. String
# 2. Starting index of the string
@zjplab
zjplab / deletelist.cpp
Created February 20, 2017 13:32
Deleting a node in a linkedlist
// A complete working C program to demonstrate deletion in singly
// linked list
#include <stdio.h>
#include <stdlib.h>
// A linked list node
struct node
{
int data;
struct node *next;
@zjplab
zjplab / 01knapsack.cpp
Created February 15, 2017 13:26
0-1 Knapsack problem dynamic programming
#include<stdio.h>
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b)? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
int knapsack(int val[], int weight[],int w,int n)
{
if(n==0 || w==0 ) return 0;
@zjplab
zjplab / LIS.cpp
Last active February 6, 2017 02:59
Longest Increasing Subsequence
int LIS(int arr[],int n){
//base case
if(n==1) return 1;
//otherwise
int curr_max=1;
for(int i=0;i<n-1;++i){ // i< n-1 because n>=2
while(arr[i]<arr[n-1] ){
int subproblem=LIS(arr,i);
@zjplab
zjplab / new_gist_file.txt
Created February 5, 2017 14:32
Optimal substructure:
Consider finding a shortest path for travelling between two cities by car, as illustrated in Figure 1. Such an example is likely to exhibit optimal substructure. That is, if the shortest route from Seattle to Los Angeles passes through Portland and then Sacramento, then the shortest route from Portland to Los Angeles must pass through Sacramento too. That is, the problem of how to get from Portland to Los Angeles is nested inside the problem of how to get from Seattle to Los Angeles. (The wavy lines in the graph represent solutions to the subproblems.)
As an example of a problem that is unlikely to exhibit optimal substructure, consider the problem of finding the cheapest airline ticket from Buenos Aires to Kyiv. Even if that ticket involves stops in Miami and then London, we can't conclude that the cheapest ticket from Miami to Kyiv stops in London, because the price at which an airline sells a multi-flight trip is usually not the sum of the prices at which it would sell the individual flights in the
@zjplab
zjplab / 0_reuse_code.js
Created January 28, 2017 14:35
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
Kadane’s Algorithm:
Initialize:
max_so_far = 0
max_ending_here = 0
Loop for each element of the array
(a) max_ending_here = max_ending_here + a[i]
(b) if(max_ending_here < 0)
max_ending_here = 0