Skip to content

Commit a38af6a

Browse files
committed
daily
1 parent 99a94a1 commit a38af6a

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

my-submissions/e3461 v3 combinatorics O(n).py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def hasSameDigits(self, s: str) -> bool:
33
a, b = 0, 0
44
x = len(s) - 2
55
for i, num in enumerate(s[:-1]) :
6-
a += (int(num) * comb(x, i)) % 100
6+
a += (int(num) * comb(x, i)) % 10
77
for i, num in enumerate(s[1:]) :
8-
b += (int(num) * comb(x, i)) % 100
8+
b += (int(num) * comb(x, i)) % 10
99
return a % 10 == b % 10

my-submissions/e3461 v4.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def hasSameDigits(self, s: str) -> bool:
3+
x = len(s) - 2
4+
a = sum((int(num) * comb(x, i)) % 10 for i, num in enumerate(s[:-1])) % 10
5+
b = sum((int(num) * comb(x, i)) % 10 for i, num in enumerate(s[1:])) % 10
6+
return a == b

my-submissions/e3461.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Notes
2+
3+
### V1/V2
4+
5+
Simulation method -- inefficient
6+
7+
### V3 and above
8+
9+
Combinatorics to create an $O(n)$ solution.
10+
11+
Breaking down examples
12+
13+
```
14+
s="12345"
15+
16+
1 2 3 4 5
17+
3 5 7 9
18+
(1+2)mod10 (2+3)mod10 (3+4)mod10 (4+5)mod10
19+
20+
8 12 16
21+
8 2 6
22+
23+
((1+2)mod10+(2+3)mod10)mod10 ((2+3)mod10+(3+4)mod10)mod10
24+
((3+4)mod10+(4+5)mod10)mod10
25+
26+
(1+2+2+3)mod10 (2+3+3+4)mod10
27+
(3+4+4+5)mod10
28+
29+
(1+2+2+2+3+3+3+4)mod10 (2+3+3+3+4+4+4+5)mod10
30+
31+
(1+2+2+2+3+3+3+4+2+3+3+3+4+4+4+5)mod10
32+
(1+2+2+2+2+3+3+3+3+3+3+4+4+4+4+5)mod10
33+
(1*1+4*2+6*3+3*4+1*5)mod10 = 44 => TRUE
34+
(1*a[0]+4*a[1]+6*a[2]+3*a[3]+1*a[4])mod10
35+
36+
(1*a[0]+4*a[1]+6*a[2]+3*a[3]+1\*a[4])mod10
37+
38+
This works for this case of 12345
39+
40+
3902
41+
3 9 0 2
42+
12 9 2
43+
2 9 2
44+
45+
11 11
46+
1 1 => TRUE
47+
48+
3 9 0 2
49+
(3+9)mod10 (9+0)mod10 (0+2)mod10
50+
(3+9+9+0)mod10 (9+0+0+2)mod10 = 21mod10 11mod10 = 1 1 => TRUE => CORRECT
51+
(1*a[0]+2*a[1]+1\*a[2])
52+
53+
(3+9+9+0+9+0+0+2)mod10
54+
= 32 => FALSE => WRONG
55+
56+
We find an edge case if we go all the way. However, if we stop one step before... we can compare their digit vals.
57+
58+
Seems like we should stop a step before and perform our comparison?
59+
2 operations of O(n)
60+
```

my-submissions/h3463.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def hasSameDigits(self, s: str) -> bool:
3+
x = len(s) - 2
4+
a = sum((int(num) * comb(x, i)) % 10 for i, num in enumerate(s[:-1])) % 10
5+
b = sum((int(num) * comb(x, i)) % 10 for i, num in enumerate(s[1:])) % 10
6+
return a == b
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def nextBeautifulNumber(self, n: int) -> int:
3+
is_beaut = lambda x: all(int(k) == v for k, v in Counter(str(x)).items())
4+
n += 1
5+
while not is_beaut(n) :
6+
n += 1
7+
return n

0 commit comments

Comments
 (0)