Skip to content

Commit 5c417db

Browse files
committed
Add leetcode contest 2023-06-10 - 2/4
1 parent ae233c9 commit 5c417db

File tree

7 files changed

+279
-0
lines changed

7 files changed

+279
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ zx _genReadme.mjs
5656
| [`6404-make-array-empty`](./challenges/leetcode-6404-make-array-empty) | leetcode |
5757
| [`6405-find-the-prefix-common-array-of-two-arrays`](./challenges/leetcode-6405-find-the-prefix-common-array-of-two-arrays) | leetcode |
5858
| [`6406-maximum-sum-with-exactly-k-elements`](./challenges/leetcode-6406-maximum-sum-with-exactly-k-elements) | leetcode |
59+
| [`6425-find-the-longest-semi-repetitive-substring`](./challenges/leetcode-6425-find-the-longest-semi-repetitive-substring) | leetcode |
60+
| [`6426-movement-of-robots`](./challenges/leetcode-6426-movement-of-robots) | leetcode |
61+
| [`6461-check-if-the-number-is-fascinating`](./challenges/leetcode-6461-check-if-the-number-is-fascinating) | leetcode |
5962
| [`aneo`](./challenges/codingame-aneo) | codingame |
6063
| [`ascii-art`](./challenges/codingame-ascii-art) | codingame |
6164
| [`brackets-extreme-edition`](./challenges/codingame-brackets-extreme-edition) | codingame |
@@ -158,6 +161,9 @@ ___
158161
| [`6404-make-array-empty`](./challenges/leetcode-6404-make-array-empty) | Classic |
159162
| [`6405-find-the-prefix-common-array-of-two-arrays`](./challenges/leetcode-6405-find-the-prefix-common-array-of-two-arrays) | Classic |
160163
| [`6406-maximum-sum-with-exactly-k-elements`](./challenges/leetcode-6406-maximum-sum-with-exactly-k-elements) | Classic |
164+
| [`6425-find-the-longest-semi-repetitive-substring`](./challenges/leetcode-6425-find-the-longest-semi-repetitive-substring) | Classic |
165+
| [`6426-movement-of-robots`](./challenges/leetcode-6426-movement-of-robots) | Classic |
166+
| [`6461-check-if-the-number-is-fascinating`](./challenges/leetcode-6461-check-if-the-number-is-fascinating) | Classic |
161167

162168
___
163169

@@ -187,6 +193,9 @@ ___
187193
| [`6404-make-array-empty`](./challenges/leetcode-6404-make-array-empty) | leetcode | Classic |
188194
| [`6405-find-the-prefix-common-array-of-two-arrays`](./challenges/leetcode-6405-find-the-prefix-common-array-of-two-arrays) | leetcode | Classic |
189195
| [`6406-maximum-sum-with-exactly-k-elements`](./challenges/leetcode-6406-maximum-sum-with-exactly-k-elements) | leetcode | Classic |
196+
| [`6425-find-the-longest-semi-repetitive-substring`](./challenges/leetcode-6425-find-the-longest-semi-repetitive-substring) | leetcode | Classic |
197+
| [`6426-movement-of-robots`](./challenges/leetcode-6426-movement-of-robots) | leetcode | Classic |
198+
| [`6461-check-if-the-number-is-fascinating`](./challenges/leetcode-6461-check-if-the-number-is-fascinating) | leetcode | Classic |
190199
| [`aneo`](./challenges/codingame-aneo) | codingame | Classic |
191200
| [`ascii-art`](./challenges/codingame-ascii-art) | codingame | Classic |
192201
| [`brackets-extreme-edition`](./challenges/codingame-brackets-extreme-edition) | codingame | Classic |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# leetcode-6425-find-the-longest-semi-repetitive-substring
2+
3+
https://leetcode.com/problems/find-the-longest-semi-repetitive-substring
4+
5+
**Type:** Classic
6+
7+
## Run Code Result
8+
9+
### Your input
10+
11+
<!-- prettier-ignore -->
12+
```js
13+
"52233"
14+
"5494"
15+
"1111111"
16+
```
17+
18+
### Your stdout
19+
20+
<!-- prettier-ignore -->
21+
```js
22+
```
23+
24+
### Your answer
25+
26+
<!-- prettier-ignore -->
27+
```js
28+
4
29+
4
30+
2
31+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
console.log = () => {}
2+
/**
3+
* @param {string} s
4+
* @return {number}
5+
*/
6+
var longestSemiRepetitiveSubstring = function (s) {
7+
console.log('###')
8+
const currentFreqs = {}
9+
let left = 0
10+
let right = 1
11+
let sizeMax = 1
12+
13+
for (left = 0; left < s.length; left++) {
14+
for (right = left + 1; right <= s.length; right++) {
15+
const str = s.slice(left, right).split('')
16+
17+
let seenPair = false
18+
let size = 0
19+
console.log('START', str)
20+
21+
for (let i = 0; i < str.length; i++) {
22+
console.log(str, str[i], i)
23+
if (str[i] === str[i + 1]) {
24+
if (!seenPair) {
25+
seenPair = true
26+
size++
27+
} else {
28+
size++
29+
break
30+
}
31+
} else {
32+
size++
33+
}
34+
}
35+
// if (str.length >= 2 && str[str.length - 2] !== str[str.length - 1]) {
36+
// size++
37+
// }
38+
console.log('END', str, size)
39+
sizeMax = Math.max(sizeMax, size)
40+
}
41+
}
42+
43+
return sizeMax
44+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# leetcode-6426-movement-of-robots
2+
3+
https://leetcode.com/problems/movement-of-robots
4+
5+
**Type:** Classic
6+
7+
## Run Code Result
8+
9+
### Your input
10+
11+
<!-- prettier-ignore -->
12+
```js
13+
[-2,0,2]
14+
"RLL"
15+
3
16+
[1,0]
17+
"RL"
18+
2
19+
```
20+
21+
### Your stdout
22+
23+
<!-- prettier-ignore -->
24+
```js
25+
#######
26+
#######
27+
#######
28+
[
29+
{ index: 0, pos: -2, dir: 1 },
30+
{ index: 1, pos: 0, dir: -1 },
31+
{ index: 2, pos: 2, dir: -1 }
32+
]
33+
TIME 0
34+
posWithRobots.length 1
35+
posWithRobots.length 2
36+
posWithRobots.length 1
37+
posWithRobots { '1': [ 2 ], '-1': [ 0, 1 ] }
38+
posWithMultipleRobots Set(1) { -1 }
39+
[
40+
{ index: 0, pos: -1, dir: -1 },
41+
{ index: 1, pos: -1, dir: 1 },
42+
{ index: 2, pos: 1, dir: -1 }
43+
]
44+
TIME 1
45+
posWithRobots.length 1
46+
posWithRobots.length 1
47+
posWithRobots.length 2
48+
posWithRobots { '0': [ 1, 2 ], '-2': [ 0 ] }
49+
posWithMultipleRobots Set(1) { 0 }
50+
[
51+
{ index: 0, pos: -2, dir: -1 },
52+
{ index: 1, pos: 0, dir: -1 },
53+
{ index: 2, pos: 0, dir: 1 }
54+
]
55+
TIME 2
56+
posWithRobots.length 1
57+
posWithRobots.length 1
58+
posWithRobots.length 1
59+
posWithRobots { '1': [ 2 ], '-3': [ 0 ], '-1': [ 1 ] }
60+
posWithMultipleRobots Set(0) {}
61+
[
62+
{ index: 0, pos: -3, dir: -1 },
63+
{ index: 1, pos: -1, dir: -1 },
64+
{ index: 2, pos: 1, dir: 1 }
65+
]
66+
distance -3 -1 2
67+
distance -3 1 4
68+
distance -1 1 2
69+
#######
70+
#######
71+
#######
72+
[ { index: 0, pos: 1, dir: 1 }, { index: 1, pos: 0, dir: -1 } ]
73+
TIME 0
74+
posWithRobots.length 1
75+
posWithRobots.length 1
76+
posWithRobots { '2': [ 0 ], '-1': [ 1 ] }
77+
posWithMultipleRobots Set(0) {}
78+
[ { index: 0, pos: 2, dir: 1 }, { index: 1, pos: -1, dir: -1 } ]
79+
TIME 1
80+
posWithRobots.length 1
81+
posWithRobots.length 1
82+
posWithRobots { '3': [ 0 ], '-2': [ 1 ] }
83+
posWithMultipleRobots Set(0) {}
84+
[ { index: 0, pos: 3, dir: 1 }, { index: 1, pos: -2, dir: -1 } ]
85+
distance 3 -2 5
86+
```
87+
88+
### Your answer
89+
90+
**Time Limit Exceeded when submitting.**
91+
92+
<!-- prettier-ignore -->
93+
```js
94+
8
95+
5
96+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// **Time Limit Exceeded when submitting.**
2+
3+
Number.prototype.mod = function (n) {
4+
'use strict'
5+
return ((this % n) + n) % n
6+
}
7+
8+
// console.log = () => {}
9+
10+
/**
11+
* @param {number[]} nums
12+
* @param {string} s
13+
* @param {number} d
14+
* @return {number}
15+
*/
16+
var sumDistance = function (nums, s, d) {
17+
const robots = nums.map((x, i) => ({ index: i, pos: x, dir: s[i] === 'L' ? -1 : 1 }))
18+
console.log(robots)
19+
20+
for (let t = 0; t < d; t++) {
21+
console.log('TIME', t)
22+
const posWithRobots = {}
23+
const posWithMultipleRobots = new Set()
24+
25+
for (const robot of robots) {
26+
robot.pos += robot.dir
27+
28+
if (!posWithRobots[robot.pos]) posWithRobots[robot.pos] = []
29+
posWithRobots[robot.pos].push(robot.index)
30+
console.log('posWithRobots.length', posWithRobots[robot.pos].length)
31+
if (posWithRobots[robot.pos].length >= 2) posWithMultipleRobots.add(robot.pos)
32+
}
33+
34+
console.log('posWithRobots', posWithRobots)
35+
console.log('posWithMultipleRobots', posWithMultipleRobots)
36+
posWithMultipleRobots.forEach(pos => {
37+
posWithRobots[pos].forEach(robotIndex => {
38+
robots[robotIndex].dir *= -1
39+
})
40+
})
41+
console.log(robots)
42+
}
43+
44+
let length = 0
45+
for (let i = 0; i < robots.length; i++) {
46+
for (let j = i + 1; j < robots.length; j++) {
47+
console.log('distance', robots[i].pos, robots[j].pos, Math.abs(robots[i].pos - robots[j].pos))
48+
length += Math.abs(robots[i].pos - robots[j].pos)
49+
}
50+
}
51+
console.log(length)
52+
console.log(length % (10 ** 9 + 7))
53+
54+
return length.mod(10 ** 9 + 7)
55+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# leetcode-6461-check-if-the-number-is-fascinating
2+
3+
https://leetcode.com/problems/check-if-the-number-is-fascinating
4+
5+
**Type:** Classic
6+
7+
## Run Code Result
8+
9+
### Your input
10+
11+
<!-- prettier-ignore -->
12+
```js
13+
192
14+
100
15+
```
16+
17+
### Your stdout
18+
19+
<!-- prettier-ignore -->
20+
```js
21+
```
22+
23+
### Your answer
24+
25+
<!-- prettier-ignore -->
26+
```js
27+
true
28+
false
29+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
var isFascinating = function (n) {
6+
const res = `${n}${2 * n}${3 * n}`.split('').map(x => +x)
7+
// console.log(res)
8+
const seen = new Set()
9+
for (const x of res) {
10+
if (x === 0 || seen.has(x)) return false
11+
12+
seen.add(x)
13+
}
14+
return true
15+
}

0 commit comments

Comments
 (0)