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 java.util.*; | |
class Hamming | |
{ | |
static List<Integer> getHamming(int n) { | |
int count = 1; | |
List<Integer> hams = new ArrayList<>(); | |
Set<Integer> seen = new HashSet<>(); | |
hams.add(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
import java.io.*; | |
import java.util.*; | |
public class Intersect { | |
static Set<Integer> intersection(List<Set<Integer>> sets) { | |
Set<Integer> s1 = sets.get(0); | |
List<Integer> removeList = new ArrayList<>(); | |
for (int s = 1; s < sets.size(); ++s) { | |
for (int i : s1) { | |
if (!(sets.get(s).contains(i))) removeList.add(i); |
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 java.io.*; | |
import java.util.*; | |
public class Kperm { | |
static void kperm(List<Integer> items, int k) { | |
kperm(items, new ArrayList<Integer>(), k); | |
} | |
static void kperm(List<Integer> items, List<Integer> accum, int k) { | |
if (accum.size() == k) { |
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 std.stdio; | |
import std.algorithm; | |
auto maxDiff(T)(T[] array) { | |
T m = 0, minVal = array[0]; | |
foreach (i; 1 .. array.length) { | |
auto diff = array[i] - minVal; | |
m = max(m, diff); | |
minVal = min(minVal, array[i]); | |
} |
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 java.util.*; | |
public class Coins | |
{ | |
static List<Integer> makeChange(int[] coins, int sum) { | |
return makeChange(coins, new ArrayList<Integer>(), sum); | |
} | |
static List<Integer> makeChange(int[] coins, List<Integer> accum, int sum) { |
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 java.util.*; | |
public class Test | |
{ | |
static String balance(String input) { | |
int count = 0; | |
StringBuilder sb = new StringBuilder(); | |
for (char c : input.toCharArray()) { |
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 std.stdio; | |
import std.typecons; | |
alias Tuple!(int, int) Pair; | |
// O(n^2) | |
auto triple(int[] array) { | |
Pair[int] map; | |
for (int j = 1; j < array.length-1; ++j) { | |
for (int i = 0; i < j; ++i) { |
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
int find(Node* root, int k) { | |
int result = -1; | |
find(root, k, result); | |
return result; | |
} | |
void find(Node* root, ref int k, ref int result) { | |
if (root.left) { | |
find(root.left, k, result); | |
} |
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 std.stdio; | |
auto swap(T)(ref T a, ref T b) { | |
T tmp = a; | |
a = b; | |
b = tmp; | |
} | |
auto part(T)(T[] array, int low, int high) { | |
auto size = low-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
import java.util.*; | |
public class Test | |
{ | |
static String rle(String input) { | |
if (input == null || input.equals("")) return ""; | |
StringBuilder sb = new StringBuilder(); | |
int i = 0; | |
while (i < input.length()) { |