Skip to content

Commit b4a543b

Browse files
committed
🎉 [contest-1/107] 1/4
1 parent 93272ae commit b4a543b

File tree

5 files changed

+305
-1
lines changed

5 files changed

+305
-1
lines changed

biweekly_contest/107/1.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
3+
/**
4+
* @param {string[]} words
5+
* @return {number}
6+
*/
7+
var maximumNumberOfStringPairs = function (words) {
8+
let complement = Array({ length: words.length }).fill("");
9+
// console.log(map)
10+
// console.log(complement)
11+
12+
for (let i = 0; i < words.length; i++) {
13+
complement[i] = words[i].split('').reverse().join('');
14+
}
15+
16+
// console.log(map)
17+
// console.log(complement)
18+
19+
let count = 0;
20+
for (let i = 0; i < words.length; i++) {
21+
// console.log(`${map.get(words[i].split('').reverse().join(''))} === ${words[i]}`)
22+
for (let j = 0; j < complement.length; j++) {
23+
if (i === j) continue;
24+
if (complement[j] === words[i]) {
25+
// console.log(`${complement[j]} === ${words[i]}`)
26+
complement[j] = "";
27+
// console.log(complement)
28+
count++;
29+
break;
30+
}
31+
}
32+
33+
// if (map.get(words[i].split('').reverse().join('')) === words[i]) {
34+
// count++;
35+
// }
36+
}
37+
38+
// console.log(count)
39+
return count / 2;
40+
};
41+
42+
// ["cd", "ac", "dc", "ca", "zz"]
43+
// ["ab", "ba", "cc"]
44+
// ["aa", "ab"]
45+
let x = null;
46+
// x = maximumNumberOfStringPairs(["cd", "ac", "dc", "ca", "zz"]) // 2
47+
// x = maximumNumberOfStringPairs(["ab", "ba", "cc"]) // 1
48+
// x = maximumNumberOfStringPairs(["aa", "ab"]) // 0
49+
x = maximumNumberOfStringPairs(["aa", "ab", "aa", "aa"]) // 0
50+
console.log(x)

biweekly_contest/107/2.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @param {number} x
3+
* @param {number} y
4+
* @param {number} z
5+
* @return {number}
6+
*/
7+
const longestString = (x, y, z) => {
8+
let map = new Map(), leftCompliment = new Map(), rightCompliment = new Map(), string = "AA";
9+
10+
map.set("AA", x - 1)
11+
map.set("BB", y)
12+
map.set("AB", z)
13+
14+
leftCompliment.set("A", ["AB", "BB"])
15+
leftCompliment.set("B", ["AA"])
16+
rightCompliment.set("A", ["BB"])
17+
rightCompliment.set("B", ["AB", "AA"])
18+
19+
// fromString(x, "AA")
20+
// fromString(y, "BB")
21+
// fromString(z, "AB")
22+
//
23+
// const fromString = (n, placeholder) => {
24+
// for (let i = 0; i < n; i++) {
25+
// string.push(placeholder);
26+
// }
27+
// }
28+
29+
const expandToLeft = () => {
30+
let complimentOfFirstChar = [...leftCompliment.get(string[0])];
31+
console.log(`l -> string[0]:${string[0]}, complimentOfFirstChar:${complimentOfFirstChar}, string:${string}`);
32+
console.log(leftCompliment)
33+
console.log(complimentOfFirstChar)
34+
35+
while (0 !== complimentOfFirstChar.length) {
36+
console.log("????")
37+
console.log(complimentOfFirstChar)
38+
let curr = complimentOfFirstChar.shift();
39+
console.log(`l -> map.get(curr):${curr}`)
40+
console.log(`l --> curr1:${curr}`)
41+
if (map.get(curr) > 0) {
42+
map.set(curr, map.get(curr) - 1)
43+
string = `${curr}${string}`;
44+
console.log(`l --> ${string}`)
45+
console.log(`l --> curr2:${curr}`)
46+
47+
return expandToLeft();
48+
}
49+
}
50+
}
51+
52+
const expandToRight = () => {
53+
let lastChar = string[string.length - 1], complimentOfLastChar = [...rightCompliment.get(lastChar)];
54+
console.log(`r -> lastChar:${lastChar}, complimentOfLastChar:${complimentOfLastChar}`);
55+
56+
while (0 !== complimentOfLastChar.length) {
57+
let curr = complimentOfLastChar.shift();
58+
console.log(`r -> map.get(curr):${curr}`)
59+
if (map.get(curr) > 0) {
60+
let abCount = map.get(curr);
61+
if ("AB" === curr) {
62+
for (let i = 0; i < abCount; i++) {
63+
map.set(curr, map.get(curr) - 1)
64+
string += curr;
65+
}
66+
} else {
67+
map.set(curr, map.get(curr) - 1)
68+
string += curr;
69+
}
70+
console.log(`r --> ${string}`)
71+
return expandToRight();
72+
}
73+
}
74+
}
75+
76+
expandToLeft();
77+
expandToRight();
78+
79+
console.log(string)
80+
81+
return string.length;
82+
};
83+
84+
85+
let x = null;
86+
// x = longestString(2, 5, 1) // 12
87+
// x = longestString(3, 2, 2) // 14
88+
// x = longestString(1, 34, 1) // 8
89+
x = longestString(2, 5, 1) // 12
90+
console.log(x)

biweekly_contest/107/3.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {number}
4+
*/
5+
var minimizeConcatenatedLength = function (words) {
6+
let toLeftMap = new Map(), toRightMap = new Map(), string = words[0];
7+
8+
for (let i = 1; i < words.length; i++) {
9+
let list = words[i].split('').splice(1, words[i].length - 1), val = "";
10+
if (toRightMap.get(words[i][0])) {
11+
val += toRightMap.get(words[i][0]).val;
12+
}
13+
14+
toRightMap.set(words[i][0], { val: val + list.join(''), deleteIdx: words[i][words[i].length - 1] });
15+
}
16+
17+
for (let i = 1; i < words.length; i++) {
18+
let list = words[i].split('').splice(0, words[i].length - 1), val = "";
19+
if (toLeftMap.get(words[i][words[i].length - 1])) {
20+
val += toLeftMap.get(words[i][words[i].length - 1]).val;
21+
}
22+
toLeftMap.set(words[i][words[i].length - 1], { val: list.join('') + val, deleteIdx: words[i][0] });
23+
}
24+
25+
console.log("___0")
26+
console.log("toRightMap")
27+
console.log(toRightMap)
28+
console.log("toLeftMap")
29+
console.log(toLeftMap)
30+
31+
for (let i = 1; i < words.length; i++) {
32+
let lastChar = string[string.length - 1];
33+
console.log(`lastChar:${lastChar}`);
34+
if (toRightMap.has(lastChar)) {
35+
let { val, deleteIdx } = toRightMap.get(lastChar);
36+
console.log(`val:${val}, deleteIdx:${deleteIdx}`);
37+
string += val; // right append
38+
console.log(toRightMap)
39+
40+
toRightMap.delete(lastChar)
41+
toLeftMap.delete(deleteIdx)
42+
43+
console.log(toRightMap)
44+
}
45+
}
46+
47+
console.log(`--> post right:${string}`);
48+
49+
for (let i = 1; i < words.length; i++) {
50+
let firstChar = string[0];
51+
console.log(`l -> firstChar:${firstChar}`);
52+
if (toLeftMap.has(firstChar)) {
53+
let { val, deleteIdx } = toLeftMap.get(firstChar);
54+
console.log(`val:${val}, deleteIdx:${deleteIdx}`);
55+
string = `${val}${string}`; // left append
56+
console.log(toLeftMap)
57+
toLeftMap.delete(firstChar)
58+
toRightMap.delete(deleteIdx)
59+
console.log(toLeftMap)
60+
}
61+
}
62+
console.log(`--> post left:${string}`);
63+
64+
console.log("___1")
65+
console.log(toRightMap)
66+
console.log(toLeftMap)
67+
for (let [val, { val: val2 }] of toRightMap) {
68+
console.log(`Appending ${val}${val2}`)
69+
string += `${val}${val2}`
70+
}
71+
72+
console.log("___2")
73+
console.log(toRightMap)
74+
console.log(toLeftMap)
75+
console.log(string)
76+
77+
return string.length;
78+
};
79+
//
80+
// ["ab","b"]
81+
//
82+
83+
84+
let x = null;
85+
// x = minimizeConcatenatedLength(["aa", "ab", "bc"]) // aabc
86+
// x = minimizeConcatenatedLength(["aaa", "c", "aba"]) // aaacaba
87+
// x = minimizeConcatenatedLength(["daca", "cd", "efg", "abe"]) // cdacabefg
88+
// x = minimizeConcatenatedLength(["ab", "b"]) // aaacaba
89+
// x = minimizeConcatenatedLength(["a", "ca"]) // aaacaba
90+
// x = minimizeConcatenatedLength(["a", "cb"]) // aaacaba
91+
// x = minimizeConcatenatedLength(["a", "b"]) // aaacaba
92+
x = minimizeConcatenatedLength(["a", "aa", "ba"]) // baa
93+
console.log(x)

biweekly_contest/107/4.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} logs
4+
* @param {number} x
5+
* @param {number[]} queries
6+
* @return {number[]}
7+
*/
8+
var countServers = function (nbOfServers, logs, interval, queries) {
9+
let serverMap = new Map(), intervalList = [], result = [];
10+
11+
// default
12+
for (let i = 0; i < nbOfServers; i++) {
13+
serverMap.set(i + 1, []);
14+
}
15+
16+
// based on logs
17+
for (let i = 0; i < logs.length; i++) {
18+
serverMap.set(logs[i][0], serverMap.get(logs[i][0]).concat(logs[i][1]));
19+
}
20+
21+
// construct intervals
22+
for (let i = 0; i < queries.length; i++) {
23+
intervalList.push([queries[i] - interval, queries[i]])
24+
}
25+
26+
27+
for (let i = 0; i < intervalList.length; i++) {
28+
let start = intervalList[i][0], end = intervalList[i][1], receiveds = 0;
29+
for (let [server, downtimes] of serverMap) {
30+
for (let j = 0; j < downtimes.length; j++) {
31+
let dt = downtimes[j];
32+
console.log(`${dt} < ${start} && ${dt} > ${end}`)
33+
if (start <= dt && dt <= end) {
34+
receiveds++;
35+
break;
36+
}
37+
}
38+
}
39+
40+
result.push(nbOfServers - receiveds);
41+
}
42+
43+
44+
console.log(serverMap)
45+
console.log(intervalList)
46+
console.log(result)
47+
48+
return result;
49+
};
50+
51+
let x = null;
52+
x = countServers(3,
53+
[[1, 3], [2, 6], [1, 5]],
54+
5,
55+
[10, 11]) // [1, 2]
56+
57+
// x = countServers(
58+
// 3,
59+
// [[2, 4], [2, 1], [1, 2], [3, 1]],
60+
// 2,
61+
// [3, 4]) // [0, 1]
62+
63+
x = countServers(
64+
3,
65+
[[1, 35], [1, 32], [1, 11], [1, 39], [2, 29]],
66+
8,
67+
[38, 30, 23, 33, 15, 31, 34, 22, 11, 14]
68+
) // [2,2,3,1,2,2,1,3,2,2]
69+
70+
console.log(x)

test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,5 @@
102102
// console.log(1 & 1);
103103

104104
// Array
105-
console.log(Array(26).fill(0));
105+
// console.log(Array(26).fill(0));
106+

0 commit comments

Comments
 (0)