File tree 12 files changed +270
-0
lines changed
1544.Make The String Great
1545.Find Kth Bit in Nth Binary String
1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target
1550.Three Consecutive Odds
1551.Minimum Operations to Make Array Equal
1553.Minimum Number of Days to Eat N Oranges
1560.Most Visited Sector in a Circular Track
1561.Maximum Number of Coins You Can Get
1562.Find Latest Group of Size M
1566.Detect Pattern of Length M Repeated K or More Times
1567.Maximum Length of Subarray With Positive Product
1569.Number of Ways to Reorder Array to Get Same BST
12 files changed +270
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String makeGood (String s ) {
3
+ return help (s );
4
+ }
5
+
6
+ private String help (String s ) {
7
+ if (s == null || s .length () == 0 || s .length () == 1 ) {
8
+ return s ;
9
+ }
10
+ char [] chars = s .toCharArray ();
11
+ for (int i = 1 ; i < chars .length ; i ++) {
12
+ if (Math .abs (chars [i ] - chars [i - 1 ]) == Math .abs ('A' - 'a' )) {
13
+ return help (newString (chars , i ));
14
+ }
15
+ }
16
+ return s ;
17
+ }
18
+
19
+ private String newString (char [] chars , int i ) {
20
+ StringBuilder tmp = new StringBuilder ();
21
+ for (int j = 0 ; j < chars .length ; j ++) {
22
+ if (j != i && j != i - 1 ) {
23
+ tmp .append (chars [j ]);
24
+ }
25
+ }
26
+ return tmp .toString ();
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public char findKthBit (int n , int k ) {
3
+ if (k == 1 || n == 1 ) {
4
+ return '0' ;
5
+ }
6
+ Set <Integer > set = new HashSet <>();
7
+ int len = calcLength (n , set );
8
+ if (set .contains (k )) {
9
+ return '1' ;
10
+ }
11
+ // 中间,返回1
12
+ if (k < len / 2 ) {
13
+ return findKthBit (n - 1 , k );
14
+ } else {
15
+ if (set .contains (len - k )) {
16
+ return '1' ;
17
+ }
18
+ return r (findKthBit (n - 1 , len - k + 1 ));
19
+ }
20
+ }
21
+
22
+ private char r (char b ) {
23
+ if (b == '0' ) {
24
+ return '1' ;
25
+ }
26
+ return '0' ;
27
+ }
28
+
29
+ private int calcLength (int n , Set <Integer > set ) {
30
+ if (n == 1 ) {
31
+ return 1 ;
32
+ }
33
+
34
+ int ans = 2 * calcLength (n - 1 , set ) + 1 ;
35
+ set .add (ans + 1 );
36
+ return ans ;
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maxNonOverlapping (int [] nums , int target ) {
3
+ Set <Integer > set = new HashSet <>();
4
+ set .add (0 );
5
+ int sum = 0 , ans = 0 ;
6
+ for (int num : nums ) {
7
+ sum += num ;
8
+ if (set .contains (sum - target )) {
9
+ ans ++;
10
+ set .clear ();
11
+ sum = 0 ;
12
+ }
13
+
14
+ set .add (sum );
15
+ }
16
+ return ans ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean threeConsecutiveOdds (int [] arr ) {
3
+ int count = 0 ;
4
+ for (int n : arr ) {
5
+ if (n % 2 == 0 ) {
6
+ count = 0 ;
7
+ } else {
8
+ count ++;
9
+ if (count >= 3 ) {
10
+ return true ;
11
+ }
12
+ }
13
+ }
14
+ return false ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minOperations (int n ) {
3
+ int ans = 0 ;
4
+ for (int i = 0 ; i < n / 2 ; i ++) {
5
+ int curr = 2 * i + 1 ;
6
+ ans += Math .abs (n - curr );
7
+ }
8
+ return ans ;
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private Map <Integer , Integer > map = new HashMap <>();
3
+
4
+ public int minDays (int n ) {
5
+ if (n < 2 ) {
6
+ return n ;
7
+ }
8
+ if (!map .containsKey (n )) {
9
+ map .put (n , Math .min (minDays (n / 2 ) + 1 + n % 2 , minDays (n / 3 ) + 1 + n % 3 ));
10
+ }
11
+ return map .get (n );
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <Integer > mostVisited (int n , int [] rounds ) {
3
+ int [] ans = new int [n ];
4
+ for (int i = 0 ; i < rounds .length ; i ++) {
5
+ rounds [i ]--;
6
+ }
7
+ ans [rounds [0 ]]++;
8
+ for (int i = 0 ; i < rounds .length - 1 ; i ++) {
9
+ int start = rounds [i ];
10
+ int end = rounds [i + 1 ];
11
+ if (end <= start ) {
12
+ end += n ;
13
+ }
14
+ for (int j = start + 1 ; j <= end ; j ++) {
15
+ ans [j % n ]++;
16
+ }
17
+ }
18
+
19
+ int max = Arrays .stream (ans ).max ().orElse (-1 );
20
+ List <Integer > list = new ArrayList <>();
21
+ for (int i = 0 ; i < ans .length ; i ++) {
22
+ if (ans [i ] == max ) {
23
+ list .add (i + 1 );
24
+ }
25
+ }
26
+ return list ;
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maxCoins (int [] piles ) {
3
+ Arrays .sort (piles );
4
+ int start = 0 , end = piles .length - 1 , ans = 0 ;
5
+ while (start < end ) {
6
+ ans += piles [end - 1 ];
7
+ start ++;
8
+ end -= 2 ;
9
+ }
10
+ return ans ;
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findLatestStep (int [] arr , int m ) {
3
+ // 倒序遍历 arr,转换为第一次出现 m 个的步骤
4
+ if (arr .length == m ) {
5
+ return m ;
6
+ }
7
+ TreeSet <Integer > set = new TreeSet <>();
8
+ set .add (0 );
9
+ set .add (arr .length + 1 );
10
+ for (int i = arr .length - 1 ; i >= 0 ; i --) {
11
+ int index = arr [i ];
12
+ int l = set .lower (index );
13
+ int h = set .higher (index );
14
+ if (index - l - 1 == m || h - index - 1 == m ) {
15
+ return i ;
16
+ }
17
+ set .add (index );
18
+ }
19
+ return -1 ;
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean containsPattern (int [] arr , int m , int k ) {
3
+ if (arr .length < m * k ) {
4
+ return false ;
5
+ }
6
+ for (int i = 0 ; i <= arr .length - m * k ; i ++) {
7
+ boolean match = true ;
8
+ for (int j = i + m ; j < i + m * k ; j ++) {
9
+ if (arr [j ] != arr [j - m ]) {
10
+ match = false ;
11
+ break ;
12
+ }
13
+ }
14
+ if (match ) {
15
+ return true ;
16
+ }
17
+ }
18
+ return false ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int getMaxLen (int [] nums ) {
3
+ // p[i] = n[i-1] + 1, nums[i] < 0
4
+ // p[i] = p[i-1] + 1, nums[i] > 0
5
+ // p[i] = 0, nums[i] = 0
6
+
7
+ // n[i] = p[i-1] + 1, nums[i] < 0
8
+ // n[i] = n[i-1] + 1, nums[i] > 0
9
+ // n[i] = 0, nums[i] = 0
10
+ int [] p = new int [nums .length ];
11
+ int [] n = new int [nums .length ];
12
+ p [0 ] = nums [0 ] > 0 ? 1 : 0 ;
13
+ n [0 ] = nums [0 ] < 0 ? 1 : 0 ;
14
+ int res = Math .max (p [0 ], 0 );
15
+ for (int i = 1 ; i < nums .length ; i ++) {
16
+ if (nums [i ] > 0 ) {
17
+ p [i ] = p [i - 1 ] + 1 ;
18
+ n [i ] = n [i - 1 ] == 0 ? 0 : n [i - 1 ] + 1 ;
19
+ } else if (nums [i ] == 0 ) {
20
+ p [i ] = 0 ;
21
+ n [i ] = 0 ;
22
+ } else {
23
+ p [i ] = n [i - 1 ] == 0 ? 0 : n [i - 1 ] + 1 ;
24
+ n [i ] = p [i - 1 ] + 1 ;
25
+ }
26
+ res = Math .max (res , p [i ]);
27
+ }
28
+ return res ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ int mod = (int ) 1e9 + 7 ;
3
+
4
+ public int numOfWays (int [] nums ) {
5
+ if (nums .length < 2 ) {
6
+ return 0 ;
7
+ }
8
+ return dfs (Arrays .stream (nums ).boxed ().collect (Collectors .toList ()), calc (nums .length )) - 1 ;
9
+ }
10
+
11
+ private int dfs (List <Integer > list , int [][] c ) {
12
+ if (list .isEmpty ()) {
13
+ return 1 ;
14
+ }
15
+ List <Integer > left = list .stream ().filter (n -> n < list .get (0 ))
16
+ .collect (Collectors .toList ());
17
+ List <Integer > right = list .stream ().filter (n -> n > list .get (0 ))
18
+ .collect (Collectors .toList ());
19
+ return (int ) ((long ) c [list .size () - 1 ][left .size ()] * dfs (left , c ) % mod * dfs (right , c )
20
+ % mod );
21
+ }
22
+
23
+ private int [][] calc (int n ) {
24
+ int [][] c = new int [n ][n ];
25
+ for (int i = 0 ; i < n ; i ++) {
26
+ for (int j = 0 ; j <= i ; j ++) {
27
+ if (j == 0 ) {
28
+ c [i ][j ] = 1 ;
29
+ } else {
30
+ c [i ][j ] = (c [i - 1 ][j ] + c [i - 1 ][j - 1 ]) % mod ;
31
+ }
32
+ }
33
+ }
34
+ return c ;
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments