This file contains 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 partition(int n) { | |
part(n, [], n); | |
} | |
private void part(int n, int[] array, int start) { | |
if (n == 0) { | |
writeln(array); |
This file contains 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; | |
void combinations(T)(T[] array) { | |
comb(array, []); | |
} | |
private void comb(T)(T[] array, T[] prefix) { | |
if (array.length) { | |
writeln(prefix ~ [array[0]]); |
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) | |
void skyscrapers(int *h, int len) { | |
int* pks = (int*)malloc(sizeof(int)*len); | |
int i, p=h[0], v=0, sum=0; | |
v = pks[0] = h[0]; | |
for (i = 1; i < len; ++i) { |
This file contains 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
Node reverse(Node head, int k) { | |
return reverse(head, k, length(head)); | |
} | |
Node reverse(Node head, int k, int remaining) { | |
if (remaining < k) return head; | |
auto i = 0; | |
Node next, prev = null, first = head; | |
while (head && i < k) { |
This file contains 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.Arrays; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.Set; | |
public class Powerset { | |
static Set<Set<Integer>> powerSet(Set<Integer> s) { | |
Set<Set<Integer>> ps = new HashSet<Set<Integer>>(); | |
powerSet(s, new HashSet<Integer>(), ps); | |
return ps; |
This file contains 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; | |
auto maxSubArray(T)(T[] array) { | |
auto start = 0, stop = 0, tmp = 0; | |
auto maxSum = 0, sum = 0; | |
foreach (i; 0 .. array.length) { | |
sum += array[i]; | |
if (sum < 0) { | |
sum = 0; |
This file contains 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; | |
class Node | |
{ | |
int val; | |
Node left, right; | |
this(int val, Node left=null, Node right=null) { | |
this.val = val; |
This file contains 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()) { |
This file contains 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 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); | |
} |