Skip to content

Commit a45481c

Browse files
committedOct 31, 2021
misc
1 parent 3e6acb1 commit a45481c

6 files changed

+201
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Check if an Original String Exists Given Two Encoded Strings
2+
const int P = 200003;
3+
struct HNode {int key; bool val; HNode *next; } pool[P], *head[P], *pit;
4+
5+
class Solution {
6+
static const int BASE = 999*10;
7+
string s1, s2;
8+
unordered_map<unsigned, char> memo;
9+
bool f(int i, int j, int p) {
10+
int key = ((p+BASE)*41+i)*41+j, hash = key%P;
11+
for (HNode *x = head[hash]; x; x = x->next)
12+
if (x->key == key)
13+
return x->val;
14+
if (s1.size() == i && s2.size() == j) return !p;
15+
bool ret = false;
16+
17+
if (unsigned(s1[i]-'0') < 10) {
18+
int x = 0;
19+
do {
20+
x = x*10+s1[i]-'0';
21+
ret = f(++i, j, p+x);
22+
} while (!ret && unsigned(s1[i]-'0') < 10);
23+
} else if (unsigned(s2[j]-'0') < 10) {
24+
int x = 0;
25+
do {
26+
x = x*10+s2[j]-'0';
27+
ret = f(i, ++j, p-x);
28+
} while (!ret && unsigned(s2[j]-'0') < 10);
29+
} else {
30+
if (p > 0) {
31+
if (j < s2.size())
32+
ret = f(i, j+1, p-1);
33+
} else if (p < 0) {
34+
if (i < s1.size())
35+
ret = f(i+1, j, p+1);
36+
} else if (s1[i] == s2[j])
37+
ret = f(i+1, j+1, p);
38+
}
39+
HNode *x = pit == end(pool) ? new HNode : pit++;
40+
*x = {key, ret, head[hash]};
41+
head[hash] = x;
42+
return ret;
43+
}
44+
public:
45+
bool possiblyEquals(string s1, string s2) {
46+
this->s1 = s1;
47+
this->s2 = s2;
48+
pit = pool;
49+
fill_n(head, sizeof(head)/sizeof(*head), nullptr);
50+
return f(0, 0, 0);
51+
}
52+
};
53+
54+
/// unordered_map (slow)
55+
56+
class Solution {
57+
static const int BASE = 999*10;
58+
string s1, s2;
59+
unordered_map<unsigned, char> memo;
60+
bool f(int i, int j, int p) {
61+
char &ret = memo[((p+BASE)*41+i)*41+j];
62+
if (ret) return ret-1;
63+
if (s1.size() == i && s2.size() == j) return !p;
64+
65+
if (unsigned(s1[i]-'0') < 10) {
66+
int x = 0;
67+
do {
68+
x = x*10+s1[i]-'0';
69+
ret = f(++i, j, p+x);
70+
} while (!ret && unsigned(s1[i]-'0') < 10);
71+
} else if (unsigned(s2[j]-'0') < 10) {
72+
int x = 0;
73+
do {
74+
x = x*10+s2[j]-'0';
75+
ret = f(i, ++j, p-x);
76+
} while (!ret && unsigned(s2[j]-'0') < 10);
77+
} else {
78+
if (p > 0) {
79+
if (j < s2.size())
80+
ret = f(i, j+1, p-1);
81+
} else if (p < 0) {
82+
if (i < s1.size())
83+
ret = f(i+1, j, p+1);
84+
} else if (s1[i] == s2[j])
85+
ret = f(i+1, j+1, p);
86+
}
87+
return ret++;
88+
}
89+
public:
90+
bool possiblyEquals(string s1, string s2) {
91+
this->s1 = s1;
92+
this->s2 = s2;
93+
return f(0, 0, 0);
94+
}
95+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Count Number of Maximum Bitwise-OR Subsets
2+
import std/bitops
3+
4+
proc countMaxOrSubsets(nums: ptr UncheckedArray[cint], n: int): int {.exportc.} =
5+
var
6+
sum = newSeq[cint](1 shl n)
7+
mx = 0
8+
for i in 1..<(1 shl n):
9+
let j = i.countTrailingZeroBits
10+
sum[i] = sum[i.bitxor 1 shl j].bitor nums[j]
11+
for i in 1..<(1 shl n):
12+
if sum[i] > mx:
13+
mx = sum[i]
14+
result = 0
15+
if sum[i] == mx:
16+
inc result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
proc findKthNumber(m: int, n: int, k: int): int {.exportc.} =
2+
var
3+
l = 1
4+
h = m*n
5+
while l < h:
6+
let mid = (l+h) shr 1
7+
var
8+
c = 0
9+
j = n
10+
for i in 1..m:
11+
while j > 0 and i*j > mid:
12+
dec j
13+
c += j
14+
if c < k: l = mid+1
15+
else: h = mid
16+
l
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Kth Smallest Product of Two Sorted Arrays
2+
import std/algorithm
3+
4+
proc kthSmallestProduct(a: ptr UncheckedArray[cint], n: int, b: ptr UncheckedArray[cint], m: int, k: int): int64 {.exportc.} =
5+
var
6+
a0, a1, b0, b1: seq[cint]
7+
sign = 1
8+
l = 0'i64
9+
h = 10000000000'i64
10+
k = k
11+
a0 = newSeq[cint]()
12+
for x in toOpenArray(a, 0, n-1):
13+
if x < 0: a0.add -x
14+
else: a1.add x
15+
a0.reverse
16+
for x in toOpenArray(b, 0, m-1):
17+
if x < 0: b0.add -x
18+
else: b1.add x
19+
b0.reverse
20+
21+
proc count(a: seq[cint], b: seq[cint], mid: int64): int64 =
22+
var j = b.len
23+
for x in a:
24+
while j > 0 and cast[int64](x)*b[j-1] > mid:
25+
dec j
26+
result += j
27+
28+
let neg = a0.len*b1.len+a1.len*b0.len
29+
if neg < k:
30+
k -= neg
31+
else:
32+
k = neg-k+1
33+
sign = -1
34+
b0.swap b1
35+
while l < h:
36+
let mid = (l+h) shr 1
37+
if count(a0, b0, mid)+count(a1, b1, mid) < k: l = mid+1
38+
else: h = mid
39+
l*sign
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Minimum Operations to Convert Number
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
int minimumOperations(vector<int>& a, int start, int goal) {
7+
const int N = 1001;
8+
vector<int> d(N, -1), q{start};
9+
d[start] = 0;
10+
REP(i, q.size()) {
11+
int u = q[i];
12+
REP(j, a.size())
13+
for (int x : {u+a[j], u-a[j], u^a[j]}) {
14+
if (x == goal) return d[u]+1;
15+
if (unsigned(x) < N && d[x] < 0) {
16+
d[x] = d[u]+1;
17+
q.push_back(x);
18+
}
19+
}
20+
}
21+
return -1;
22+
}
23+
};

‎smallest-index-with-equal-value.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Smallest Index With Equal Value
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
int smallestEqual(vector<int>& a) {
7+
REP(i, a.size())
8+
if (i%10 == a[i])
9+
return i;
10+
return -1;
11+
}
12+
};

0 commit comments

Comments
 (0)