File tree 4 files changed +158
-0
lines changed
4 files changed +158
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ * https://leetcode-cn.com/problems/find-lucky-integer-in-an-array/
4
+ *
5
+ * Easy
6
+ *
7
+ * 5368. 找出数组中的幸运数
8
+ *
9
+ * - 哈希表
10
+ *
11
+ * 60ms 100.00%
12
+ *
13
+ * 35.7mb 100.00%
14
+ *
15
+ * 时间复杂度 O(n)
16
+ * 空间复杂度 O(n)
17
+ */
18
+ const findLucky = arr => {
19
+ const record = new Map ( ) ;
20
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
21
+ if ( ! record . get ( arr [ i ] ) ) {
22
+ record . set ( arr [ i ] , 0 ) ;
23
+ }
24
+ record . set ( arr [ i ] , record . get ( arr [ i ] ) + 1 ) ;
25
+ }
26
+ let ans = - 1 ;
27
+ for ( const [ key , value ] of record . entries ( ) ) {
28
+ if ( + key === + value ) {
29
+ ans = Math . max ( ans , + key ) ;
30
+ }
31
+ }
32
+
33
+ return ans ;
34
+ }
Original file line number Diff line number Diff line change
1
+ const numTeams = rating => {
2
+ let ans = 0 ;
3
+ const max = rating . length ;
4
+ for ( let i = 0 ; i < max ; i ++ ) {
5
+ for ( let j = i + 1 ; j < max ; j ++ ) {
6
+ for ( let k = j + 1 ; k < max ; k ++ ) {
7
+ if ( rating [ i ] < rating [ j ] && rating [ j ] < rating [ k ] ) {
8
+ ans ++ ;
9
+ }
10
+
11
+ if ( rating [ i ] > rating [ j ] && rating [ j ] > rating [ k ] ) {
12
+ ans ++ ;
13
+ }
14
+ }
15
+ }
16
+ }
17
+
18
+ return ans ;
19
+ }
20
+
21
+ function getCount ( count ) {
22
+ if ( count < 3 ) {
23
+ return 0 ;
24
+ }
25
+ return count * ( count - 1 ) * ( count - 2 ) / 6
26
+ }
Original file line number Diff line number Diff line change
1
+
2
+ var UndergroundSystem = function ( ) {
3
+ this . inMap = new Map ( ) ;
4
+ this . resultMap = new Map ( ) ;
5
+ } ;
6
+
7
+ /**
8
+ * @param {number } id
9
+ * @param {string } stationName
10
+ * @param {number } t
11
+ * @return {void }
12
+ */
13
+ UndergroundSystem . prototype . checkIn = function ( id , stationName , t ) {
14
+ this . inMap . set ( id , [ stationName , t ] ) ;
15
+ } ;
16
+
17
+ /**
18
+ * @param {number } id
19
+ * @param {string } stationName
20
+ * @param {number } t
21
+ * @return {void }
22
+ */
23
+ UndergroundSystem . prototype . checkOut = function ( id , stationName , t ) {
24
+ const [ startStation , startT ] = this . inMap . get ( id ) ;
25
+ const time = t - startT ;
26
+ if ( ! this . resultMap . has ( `${ startStation } -${ stationName } ` ) ) {
27
+ this . resultMap . set ( `${ startStation } -${ stationName } ` , [ ] ) ;
28
+ }
29
+ this . resultMap . get ( `${ startStation } -${ stationName } ` ) . push ( time ) ;
30
+ this . inMap . delete ( id ) ;
31
+ } ;
32
+
33
+ /**
34
+ * @param {string } startStation
35
+ * @param {string } endStation
36
+ * @return {number }
37
+ */
38
+ UndergroundSystem . prototype . getAverageTime = function ( startStation , endStation ) {
39
+ const list = this . resultMap . get ( `${ startStation } -${ endStation } ` ) ;
40
+ const max = list . length ;
41
+ return list . reduce ( ( a , b ) => a + b , 0 ) / max ;
42
+ } ;
Original file line number Diff line number Diff line change
1
+ const MAX_INT = 10 ** 9 + 7 ;
2
+ const findGoodStrings = ( n , s1 , s2 , evil ) => {
3
+ let ans = 0 ;
4
+
5
+ if ( n === 0 ) {
6
+ return 0 ;
7
+ }
8
+
9
+ let c1 = 0
10
+ for ( let i = s1 [ 0 ] . charCodeAt ( 0 ) + 1 ; i < s2 [ 0 ] . charCodeAt ( 0 ) ; i ++ ) {
11
+ if ( evil . indexOf ( String . fromCharCode ( i ) ) === - 1 ) {
12
+ c1 ++ ;
13
+ }
14
+ }
15
+ ans += getCount ( c1 , evil . length , n - 1 ) ;
16
+
17
+ for ( let i = 1 ; i < s1 . length ; i ++ ) {
18
+ const item = s1 [ i ] ;
19
+ let count = 0 ;
20
+ let start = item . charCodeAt ( 0 ) ;
21
+ let max1 = s1 [ i - 1 ] === s2 [ i - 1 ] ? s2 [ i ] . charCodeAt ( 0 ) : 122 ;
22
+ while ( start <= max1 ) {
23
+ if ( evil . indexOf ( String . fromCharCode ( start ) ) === - 1 ) {
24
+ count ++ ;
25
+ }
26
+ start ++ ;
27
+ }
28
+ ans += getCount ( count , evil . length , n - i - 1 ) ;
29
+
30
+ if ( s2 [ i - 1 ] !== s1 [ i - 1 ] ) {
31
+ const item2 = s2 [ i ] ;
32
+ let count2 = 0 ;
33
+ let start2 = item2 . charCodeAt ( 0 ) ;
34
+ while ( start2 >= s1 [ i ] . charCodeAt ( 0 ) ) {
35
+ if ( evil . indexOf ( String . fromCharCode ( start2 ) ) === - 1 ) {
36
+ count2 ++ ;
37
+ }
38
+ start2 -- ;
39
+ }
40
+
41
+ ans += getCount ( count2 , evil . length , n - i - 1 ) ;
42
+ }
43
+
44
+ }
45
+ return ans ;
46
+ }
47
+
48
+ function getCount ( count , length , n ) {
49
+ let ans = count ;
50
+ while ( n > 0 ) {
51
+ ans *= ( 26 - length ) ;
52
+ ans %= MAX_INT ;
53
+ n -- ;
54
+ }
55
+ return ans ;
56
+ }
You can’t perform that action at this time.
0 commit comments