Skip to content

Instantly share code, notes, and snippets.

View orhanobut's full-sized avatar

Orhan Obut orhanobut

View GitHub Profile
@orhanobut
orhanobut / gist:6637845
Last active December 23, 2015 12:49
Find the top N of a given integer array. Complexity = O (n.log(k)). I needed to show top 10 data and there was too much data. Brute force wasn't a solution because getting report was slow. I made it as fast as possible. PriorityQueue was a big help because of getting data complexity is O (log(k)). I also made it a general method, thus everyone c…
private static void findTopN(int[] a, int k) {
if (a == null) return;
if (a.length <= k) {
return;
}
PriorityQueue<Integer> pq = new PriorityQueue<Integer> (k);
for (int i = 0; i < a.length; i++) {
pq.add(a[i]);
if (pq.size() > k) {
pq.poll();
@orhanobut
orhanobut / gist:6637865
Last active December 23, 2015 12:49
Finds the first sequence k of a given integer array. Complexity is O(n^2). It can be done with only o(n)
public static void findFirstSeq(int[] a, int k) {
int size = a.length;
for (int i = 0; i < size; i++) {
int total = 0;
for (int j = i; j < size; j++) {
total += a[j];
if (total > k) {
break;
} else if (total == k) {
System.out.println("FOUND IT " + a[i] + ", " + a[j]);
@orhanobut
orhanobut / gist:6637877
Last active December 23, 2015 12:49
Remove duplicates with using an extra buffer. Complexity = O (n)
public static void removeDuplicates(int[] a){
Set<Integer> s = new HashSet<Integer>();
for (int i : a) {
s.add(i);
}
s.toArray();
}
@orhanobut
orhanobut / gist:6637881
Last active December 23, 2015 12:49
Permutation of a given string Complexity = O(n!)
public static void permString(String p, String t){
int size = t.length();
if (size == 0){
System.out.println(p);
} else {
for (int i=0; i<size; i++){
perm(p + t.charAt(i),
t.substring(0,i) + t.substring(i+1));
}
}
@orhanobut
orhanobut / gist:6637890
Last active December 23, 2015 12:49
Find the top K of a given integer array. Complexity = O(n.log(k))
public static void findTopK(int[] a, int k){
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(k+1);
for (int i : a){
if (!pq.contains(i)){
pq.add(i);
}
if (pq.size() > k){
pq.poll();
}
}
@orhanobut
orhanobut / gist:6637894
Last active December 23, 2015 12:49
Find a file in the given directory Complexity = O(n!)
public static File findFile(File file, String fileName){
File[] files = file.listFiles();
if (files == null) return null;
for (File f : files){
if (f.isFile() && f.getName().equals(fileName)){
return f;
} else if (f.isDirectory()){
return findFile(f, fileName);
}
}
@orhanobut
orhanobut / gist:6648441
Created September 21, 2013 08:09
Simple ajax code structure
$.ajax({
type: "POST",
url: "file_upload.php",
data: yourData,
dataType: "json",
success: function (result) {
if (result.success) {
//your cod
}
},
@orhanobut
orhanobut / gist:8665372
Created January 28, 2014 10:34
Up down animation for dialog fragment
// Slide up animation
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="100%"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXDelta="0" />
@orhanobut
orhanobut / gist:9267654
Last active October 17, 2022 20:44
how to find square root of an integer with binary search
public static int sqr(int a){
if (a < 1) return 0;
int low = 1;
int h = a;
while (low < h){
@orhanobut
orhanobut / gist:9362671
Last active August 29, 2015 13:57
find the node in k distance in a given tree list, solution 1 : shifting -> complexity = o (n)
public class HelloWorld {
public static void main(String[] args) {
Node r = new Node(0);
r.left = new Node(1);
r.right = new Node(2);
r.left.left = new Node(3);
r.left.right = new Node(4);