WHAT IS THIS

This commit is contained in:
lda committed 2026-01-16 15:24:49 +07:00
1 parent 5245f4ceae
commit 614bdc11a7
6 files changed
+522

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <assert.h>
#include <string.h>
/*
nujmber. i will have to sort these.
a sensible implementation can be:
snprintf to convert this number to a list of chars.
sort these chars smallest to largest.
we will have three possibilities.
- if the number is 0, its free.
- if the number doesnt have a 0, its free. for these two cases we can atoi back to an int.
- if the number has a 0, since the number isnt 0, it has more than 1 digit. we can swap str[0] and str[1], and then its free.
*/
#define int_bits sizeof(int) * CHAR_BIT
int int_cmp(const void *a, const void *b);
int main(void)
{
int to_process;
// scanf("%d", &to_process);
to_process = (1 << 29) + (1 << 18) + (1 << 9) + 399643581;
if (to_process < 0)
to_process = -to_process;
char numarray[int_bits + 1];
snprintf(numarray, int_bits, "%d", to_process);
printf("\n numer is %s\n", numarray);
int len = strlen(numarray);
qsort(numarray, len, sizeof(char), int_cmp);
printf("\n numer is %s\n", numarray);
for (int ind; ind < len; ++ind)
if (numarray[ind] != '0')
{
if (ind)
{
char temp = numarray[ind];
numarray[ind] = numarray[0];
numarray[0] = temp;
}
break;
};
printf("\n numer is %s\n", numarray);
return 0;
}
int int_cmp(const void *a, const void *b)
{
return ((int)*(char *)a - (int)*(char *)b);
}
+76
View File
@@ -0,0 +1,76 @@
/*
fuck ts is a leetcode medium or something how can i do two of them in 45 mins
12345 => 12354
123486 => 123468
751410 => 151470
12608 => 10628
125368764567523 => 1223...53
12301614 => 10321614
89764123 => 19764823
496774580 => 446779580
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
void swapchar(char *a, char *b)
{
char tar = *a;
*a = *b;
*b = tar;
}
const int powi(const int a, const int b) {
int r = 1, bb = b; while (bb-- > 0) r *= a;
return r;
}
const int log10i(const int a) {
int c = 0, aa = a;
while (aa/=10) c++;
return c;
}
int main()
{
int leng = log10i(INT_MAX) + 1;
int to_process;
scanf("%d", &to_process);
if (to_process < 0)
to_process = -to_process;
char num_array[leng + 1];
int n = snprintf(num_array, leng, "%d", to_process);
printf("int but %d", leng);
int max_price = INT_MIN, p1max, p2max;
// price = how much it changes. 12345 => 6789 = 10000 - 0000 + 2000 - 6000 + ...;
// we calculate the lowest price.
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
{
int n1 = (num_array[i] - '0');
int n2 = (num_array[j] - '0');
if (!((n1 && n2) || (i && j)))
continue;
int p1 = powi(10, n - 1 - i);
int p2 = powi(10, n - 1 - j);
int price = n1 * p1 + n2 * p1 - n1 * p2 - n2 * p1;
if (price > max_price)
{
p1max = i;
p2max = j;
max_price = price;
}
}
if (max_price != INT_MAX)
{
printf("\nprice = %d, p1 %d, p2 %d", max_price, p1max, p2max);
swapchar(num_array + p1max, num_array + p2max);
}
printf("\nnew numbe %s", num_array);
return 0;
}
+76
View File
@@ -0,0 +1,76 @@
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int smallest_swap(const int n)
{
if (n < 10)
return n;
const int f = n % 10;
int mn = INT_MAX;
for (int m = n / 10, i = 10; m; i *= 10, m /= 10)
{
int d = m % 10;
int d1 = d, d2 = f, p1 = i, p2 = 1;
// you cant swap a 0 to the first digit
if (d2 == 0 && m < 10)
continue;
// n - og pair + swapped pair = new n
int nn = n - d1 * p1 - d2 * p2 + d1 * p2 + d2 * p1;
if (nn < mn)
mn = nn;
// else if (mn == n)
// continue;
}
return mn;
}
int orchestrator(const int n)
{
if (n < 10)
return n;
int l = smallest_swap(n);
// one digit
int r = orchestrator(n / 10) * 10 + n % 10;
return r < l ? r : l;
}
int uhh(const int n)
{
int n2 = orchestrator(n);
if (n == n2) // uhh
{
// which means no matter the swaps it will be bigger than (or equal to) the og.
int seenmask = 0;
for (int r = n; r; r /= 10)
{
int uhhhh = 1 << (r % 10);
if (seenmask & uhhhh)
{
printf("uh ");
return n;
}
else
seenmask |= uhhhh;
}
// which means no matter the swaps it will be bigger than the og. 123569 -> 123596
if (n < 100 && !(n % 10))
{
// not my fault teach
return INT_MAX;
}
else
{
return (n / 100 * 100) + (n % 10 * 10) + (n % 100 / 10);
}
}
else
return n2;
}
int main()
{
int i;
scanf("%d", &i);
printf("number in is %d, ", i);
printf("smallest is %d", uhh(i));
}
+151
View File
@@ -0,0 +1,151 @@
/*
Problem: given a, b (same length), permute only a to minimize |perm(a) - b|.
Algo proposals
Iterative (main):
1) digits(a) -> sort ascending
2) iterate all unique perms via next_permutation
3) skip leading zero
4) build number, update best with |num - b|
Complexity: Time O(n!*n), Space O(1)
Correctness: enumerates each valid perm once; best tracked globally
Recursive (alt):
1) sort digits of a
2) backtrack positions with used[]; skip duplicates (i>0 && d[i]==d[i-1] && !used[i-1])
3) block leading zero at pos 0
4) on depth n, build number and update best
Complexity: Time O(n!*n), Space O(n)
Correctness: generates each unique valid perm exactly once
Edge cases: negatives or mismatched length -> -1; leading zeros disallowed
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
static int digits(int x)
{
if (x == 0)
return 1;
int c = 0;
while (x)
{
c++;
x /= 10;
}
return c;
}
static void to_digits(int x, int n, int *d)
{
for (int i = n - 1; i >= 0; --i)
{
d[i] = x % 10;
x /= 10;
}
}
static int cmp_int(const void *a, const void *b)
{
int x = *(const int *)a, y = *(const int *)b;
return (x > y) - (x < y);
}
static int next_perm(int *a, int n)
{ // O(n) per step
int i = n - 2;
while (i >= 0 && a[i] >= a[i + 1])
i--; // O(n)
if (i < 0)
return 0;
int j = n - 1;
while (a[j] <= a[i])
j--; // O(n)
int t = a[i];
a[i] = a[j];
a[j] = t;
for (int l = i + 1, r = n - 1; l < r; l++, r--)
{ // O(n)
int u = a[l];
a[l] = a[r];
a[r] = u;
}
return 1;
}
static int build_num(const int *a, int n)
{
int v = 0;
for (int i = 0; i < n; ++i)
v = v * 10 + a[i];
return v;
}
// Iteration: enumerate perms of a, track min |perm(a)-b| (Time: O(n!*n))
int minAbsDiffIter(int a, int b)
{
if (a < 0 || b < 0) return -1;
if (digits(a) != digits(b)) return -1;
int n = digits(a);
int *d = (int *)malloc(sizeof(int) * n);
to_digits(a, n, d);
qsort(d, n, sizeof(int), cmp_int);
int best = INT_MAX;
do
{
if (d[0] == 0) continue; // no leading zero
int v = build_num(d, n);
int diff = v - b; if (diff < 0) diff = -diff;
if (diff < best) best = diff;
} while (next_perm(d, n));
free(d);
return best;
}
// Recursion: backtrack digits of a, update best on full build (Time: O(n!*n))
static void dfs_rec(int pos, int n, const int *d, int *used, int *cur, int b, int *best)
{
if (pos == n)
{
int v = build_num(cur, n);
int diff = v - b; if (diff < 0) diff = -diff;
if (diff < *best) *best = diff;
return;
}
for (int i = 0; i < n; ++i)
{
if (used[i]) continue;
if (i && d[i] == d[i - 1] && !used[i - 1]) continue; // skip dups
if (pos == 0 && d[i] == 0) continue; // no leading zero
used[i] = 1; cur[pos] = d[i];
dfs_rec(pos + 1, n, d, used, cur, b, best);
used[i] = 0;
}
}
int minAbsDiffRec(int a, int b)
{
if (a < 0 || b < 0) return -1;
if (digits(a) != digits(b)) return -1;
int n = digits(a);
int *d = (int *)malloc(sizeof(int) * n);
to_digits(a, n, d);
qsort(d, n, sizeof(int), cmp_int);
int *used = (int *)calloc(n, sizeof(int));
int *cur = (int *)malloc(sizeof(int) * n);
int best = INT_MAX;
dfs_rec(0, n, d, used, cur, b, &best);
free(d); free(used); free(cur);
return best;
}
int main(void)
{
int a = 2376, b = 1378; // expect 989 = |2367 - 1378|
printf("iter: %d\n", minAbsDiffIter(a, b));
printf("recu: %d\n", minAbsDiffRec(a, b));
return 0;
}
+119
View File
@@ -0,0 +1,119 @@
/*
f(x) = 3x + 1
THE LIST will have two lists.
*/
#include <stdlib.h>
#include <stdio.h>
typedef struct IntIntMap
{
int *x;
int *fx;
int size;
int cap;
int (*apply)(int); // f(x)
} Map;
int f(int x)
{
return 3 * x + 1;
}
Map init(int cap, int (*fn)(int))
{
int *x = (int *)malloc(sizeof(int) * cap);
if (!x)
exit(EXIT_FAILURE);
int *fx = (int *)malloc(sizeof(int) * cap);
if (!fx)
exit(EXIT_FAILURE);
return (Map){
x,
fx,
0,
cap,
fn ? fn : f,
};
}
void set_fx(Map *map, int (*fn)(int)) { map->apply = fn ? fn : f; }
void resize_m(Map *map)
{
int newcap = map->cap * 2;
int *nx = (int *)malloc(sizeof(int) * newcap);
if (!nx)
exit(EXIT_FAILURE);
int *nfx = (int *)malloc(sizeof(int) * newcap);
if (!nfx)
exit(EXIT_FAILURE);
for (int i = 0; i < map->size; ++i)
{
nx[i] = map->x[i];
nfx[i] = map->fx[i];
}
free(map->x);
free(map->fx);
map->x = nx;
map->fx = nfx;
map->cap = newcap;
}
void add_m(Map *map, int x)
{
if (map->size == map->cap)
resize_m(map);
int fx = map->apply ? map->apply(x) : f(x);
int sz = map->size++;
map->x[sz] = x;
map->fx[sz] = fx;
}
void remove_m(Map *map, int x)
{
// shift elements to the left
for (int i = 0; i < map->size; i++)
{
if (map->x[i] == x)
{
for (int j = i; j < map->size - 1; j++)
{
map->x[j] = map->x[j + 1];
map->fx[j] = map->fx[j + 1];
}
map->size--;
return;
}
}
}
void print(Map *map)
{
printf("Map:\n");
for (int i = 0; i < map->size; i++)
{
printf("%d -> %d\n", map->x[i], map->fx[i]);
}
}
void free_m(Map *map)
{
free(map->x);
free(map->fx);
map->x = map->fx = NULL;
map->size = map->cap = 0;
}
int main(){
Map map = init(6, f);
add_m(&map, 1);
add_m(&map, 4);
add_m(&map, 7);
add_m(&map, 10);
print(&map);
remove_m(&map, 10);
print(&map);
free_m(&map);
}
+37
View File
@@ -0,0 +1,37 @@
# Binary search tree
sample array:
X = [17, 3, 46, 70, 15, 13, 28, 7, 90]
## tree structure
BST built by inserting X in order:
```text
17
/ \
3 46
\ / \
15 28 70
/ \
13 90
/
7
```
## visit algorithm
### the algo
- recursively visit right child
- recursively visit left child
- visit the node, display its value
### the result
running this algorithm on the BST above yields:
```text
90 70 28 46 7 13 15 3 17
```