Skip to content

Instantly share code, notes, and snippets.

View orhanobut's full-sized avatar

Orhan Obut orhanobut

View GitHub Profile
@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: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: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: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: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:6637818
Last active December 23, 2015 12:49
Get the minimum value of a given integer array. Complexity = O (n)
public static int getMin(int[] a) {
if (a == null) {
return 0;
}
if (a.length == 1) {
return a[0];
}
int min = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] < min) {
@orhanobut
orhanobut / gist:6637825
Last active December 23, 2015 12:49
Get the minimum value of a given sorted integer array. Complexity log(n)
public static int getMinValue(int[] a) {
if (a == null)
return -1;
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > a[hi]) {
lo = mid + 1;
} else if (a[mid] < a[hi]) {
@orhanobut
orhanobut / gist:6637803
Last active December 23, 2015 12:49
Algorithm to check if the given 2 strings are anagram. Complexity = O (n)
public static boolean checkIfAnagram(String s1, String s2) {
int len1 = s1.length();
for (int i = 0; i < len1; i++) {
if (s1.charAt(i) != s2.charAt(len1 - i - 1)) {
return false;
}
}
return true;
}