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
# Columns: | |
# log(N) N Time/Iter(usec)[std::lower_bound] N Time/Iter(usec)[l2search] | |
23 8388608 0.460176 8388608 0.269376 | |
24 16777216 0.621821 16777216 0.435599 | |
25 33554432 0.735565 33554432 0.615605 | |
26 67108864 0.856156 67108864 0.598811 | |
27 134217728 0.959821 134217728 0.636344 | |
28 268435456 1.552199 268435456 0.771024 | |
29 536870912 1.158228 536870912 0.818436 | |
30 1073741824 1.936374 1073741824 0.905859 |
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
/* Compile and run: | |
* | |
* g++ -std=c++0x InPlaceMergeNsqrtN.cpp && ./a.out 23 | |
*/ | |
/* Merges 2 sorted arrays of size N and sqrt(N) in Theta(N) time | |
* | |
*/ | |
#include <iostream> | |
#include <vector> |
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
def unionRanges(intervals): | |
intervals = sorted(intervals) # Sort by elem[0] | |
ranges = [ ] | |
if len(intervals) == 0: | |
return [ ] | |
start = intervals[0][0] | |
end = intervals[0][1] | |
for i in range(1, len(intervals)): | |
if intervals[i][0] < end: | |
end = max(end, intervals[i][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
#include <iostream> | |
#include <stack> | |
using namespace std; | |
// Counts numbers from 0 to n-1. | |
struct Counter { | |
int i = 0; | |
int n; | |
bool first = true; // Tracks if this is the first invocation of the next() function | |
Counter(int _n): n(_n) {} |
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
#include <iostream> | |
using namespace std; | |
// Counts numbers from 0 to n-1. | |
struct Counter { | |
int i = 0; | |
int n; | |
bool first = true; // Tracks if this is the first invocation of the next() function | |
Counter(int _n): n(_n) {} | |
int* next() { |
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
#include <iostream> | |
#include <stack> | |
using namespace std; | |
struct TreeNode { | |
int val; | |
TreeNode *left, *right; | |
TreeNode(int v): val(v), left(nullptr), right(nullptr) {} | |
}; |
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 torch | |
class AddTensorsModel(torch.nn.Module): | |
def __init__(self): | |
super().__init__(); | |
self.t1 = torch.Tensor([0, 5, 10]) | |
def helper(self, x, y): | |
z = self.t1 | |
z = z + x + y |
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
m = AddtensorsModel() | |
res = m(torch.Tensor([1, 2, 3]), torch.Tensor([3, 4, 5])) | |
print(res) |
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
scripted = torch.jit.script(m) |
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
from torch.utils.mobile_optimizer import optimize_for_mobile | |
optimized_model = optimize_for_mobile(scripted) |