File tree Expand file tree Collapse file tree 14 files changed +379
-0
lines changed
Expand file tree Collapse file tree 14 files changed +379
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def countTriples (self , n : int ) -> int :
3+ res = 0
4+
5+ # Iterate over all possible pairs (a, b)
6+ for a in range (1 , n + 1 ):
7+ for b in range (1 , n + 1 ):
8+ # Calculate c^2 = a^2 + b^2
9+ c_squared = a * a + b * b
10+ c = int (c_squared ** 0.5 )
11+
12+ # Check if c is a perfect square and within range
13+ if c * c == c_squared and 1 <= c <= n :
14+ res += 1
15+
16+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxBottlesDrunk (self , numBottles : int , numExchange : int ) -> int :
3+ res = 0
4+ empty = 0
5+ exchange = numExchange
6+
7+ # Drink all initial bottles
8+ res += numBottles
9+ empty = numBottles
10+
11+ # Continue exchanging while possible
12+ while empty >= exchange :
13+ # Exchange empty bottles for one full bottle
14+ empty -= exchange
15+ exchange += 1
16+ res += 1
17+ empty += 1
18+
19+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def minEnd (self , n : int , x : int ) -> int :
3+ # We need to construct an array where:
4+ # 1. nums[i+1] > nums[i]
5+ # 2. AND of all elements = x
6+ # 3. Minimize nums[n-1]
7+
8+ # The idea: merge x with (n-1) by keeping x's bits and filling others with (n-1)'s bits
9+ res = x
10+ v = n - 1
11+ bit_pos = 0
12+
13+ while v > 0 :
14+ # Find a position where x has 0
15+ while (res >> bit_pos ) & 1 :
16+ bit_pos += 1
17+
18+ # Set that bit if v has 1
19+ if v & 1 :
20+ res |= 1 << bit_pos
21+
22+ v >>= 1
23+ bit_pos += 1
24+
25+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def countMatchingSubarrays (self , nums : List [int ], pattern : List [int ]) -> int :
3+ n = len (nums )
4+ m = len (pattern )
5+ res = 0
6+
7+ # Check each possible starting position
8+ for i in range (n - m ):
9+ match = True
10+ # Check if subarray starting at i matches pattern
11+ for k in range (m ):
12+ if pattern [k ] == 1 :
13+ if nums [i + k + 1 ] <= nums [i + k ]:
14+ match = False
15+ break
16+ elif pattern [k ] == 0 :
17+ if nums [i + k + 1 ] != nums [i + k ]:
18+ match = False
19+ break
20+ else : # pattern[k] == -1
21+ if nums [i + k + 1 ] >= nums [i + k ]:
22+ match = False
23+ break
24+
25+ if match :
26+ res += 1
27+
28+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def minOperations (self , nums : List [int ], k : int ) -> int :
3+ # Calculate XOR of all elements
4+ xor_all = 0
5+ for num in nums :
6+ xor_all ^= num
7+
8+ # Count different bits between xor_all and k
9+ diff = xor_all ^ k
10+ res = bin (diff ).count ("1" )
11+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxOperations (self , nums : List [int ]) -> int :
3+ n = len (nums )
4+
5+ # Try all possible first operation scores
6+ possible_scores = [
7+ nums [0 ] + nums [1 ], # first two
8+ nums [- 1 ] + nums [- 2 ], # last two
9+ nums [0 ] + nums [- 1 ], # first and last
10+ ]
11+
12+ res = 0
13+
14+ for target_score in possible_scores :
15+ # DP: dp[l][r] = max operations on subarray nums[l:r+1]
16+ from functools import lru_cache
17+
18+ @lru_cache (maxsize = None )
19+ def dp (l , r ):
20+ if r - l < 1 : # Need at least 2 elements
21+ return 0
22+
23+ max_ops = 0
24+
25+ # Try first two
26+ if l + 1 <= r and nums [l ] + nums [l + 1 ] == target_score :
27+ max_ops = max (max_ops , 1 + dp (l + 2 , r ))
28+
29+ # Try last two
30+ if l <= r - 1 and nums [r - 1 ] + nums [r ] == target_score :
31+ max_ops = max (max_ops , 1 + dp (l , r - 2 ))
32+
33+ # Try first and last
34+ if l < r and nums [l ] + nums [r ] == target_score :
35+ max_ops = max (max_ops , 1 + dp (l + 1 , r - 1 ))
36+
37+ return max_ops
38+
39+ res = max (res , dp (0 , n - 1 ))
40+
41+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def largestPalindrome (self , n : int , k : int ) -> str :
3+ # Generate largest n-digit number
4+ max_num = 10 ** n - 1
5+ min_num = 10 ** (n - 1 )
6+
7+ # Try from largest to smallest
8+ for num in range (max_num , min_num - 1 , - 1 ):
9+ num_str = str (num )
10+ # Check if it's a palindrome
11+ if num_str == num_str [::- 1 ]:
12+ # Check if divisible by k
13+ if num % k == 0 :
14+ return num_str
15+
16+ return ""
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def minValidStrings (self , words : List [str ], target : str ) -> int :
3+ n = len (target )
4+ # dp[i] = minimum valid strings to form target[0:i]
5+ dp = [float ("inf" )] * (n + 1 )
6+ dp [0 ] = 0
7+
8+ # Build set of all possible prefixes from words
9+ prefixes = set ()
10+ for word in words :
11+ for i in range (1 , len (word ) + 1 ):
12+ prefixes .add (word [:i ])
13+
14+ # DP: for each position, try all matching prefixes
15+ for i in range (n ):
16+ if dp [i ] == float ("inf" ):
17+ continue
18+
19+ for j in range (i + 1 , n + 1 ):
20+ substr = target [i :j ]
21+ if substr in prefixes :
22+ dp [j ] = min (dp [j ], dp [i ] + 1 )
23+
24+ return dp [n ] if dp [n ] != float ("inf" ) else - 1
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maximumLength (self , nums : List [int ]) -> int :
3+ n = len (nums )
4+ res = 0
5+
6+ # Try all 4 parity patterns: all even, all odd, even-odd, odd-even
7+ patterns = [
8+ lambda x : x % 2 == 0 , # all even
9+ lambda x : x % 2 == 1 , # all odd
10+ lambda x , i : (x % 2 == 0 ) if i % 2 == 0 else (x % 2 == 1 ), # even-odd
11+ lambda x , i : (x % 2 == 1 ) if i % 2 == 0 else (x % 2 == 0 ), # odd-even
12+ ]
13+
14+ # For simple patterns (all even, all odd)
15+ for pattern in patterns [:2 ]:
16+ count = sum (1 for num in nums if pattern (num ))
17+ res = max (res , count )
18+
19+ # For alternating patterns
20+ # Try even-odd pattern
21+ count_even_odd = 0
22+ last_parity = None
23+ for num in nums :
24+ if last_parity is None :
25+ if num % 2 == 0 :
26+ count_even_odd = 1
27+ last_parity = 0
28+ else :
29+ expected = 1 - last_parity
30+ if num % 2 == expected :
31+ count_even_odd += 1
32+ last_parity = expected
33+ res = max (res , count_even_odd )
34+
35+ # Try odd-even pattern
36+ count_odd_even = 0
37+ last_parity = None
38+ for num in nums :
39+ if last_parity is None :
40+ if num % 2 == 1 :
41+ count_odd_even = 1
42+ last_parity = 1
43+ else :
44+ expected = 1 - last_parity
45+ if num % 2 == expected :
46+ count_odd_even += 1
47+ last_parity = expected
48+ res = max (res , count_odd_even )
49+
50+ return res
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def lastNonEmptyString (self , s : str ) -> str :
3+ from collections import Counter
4+
5+ # Count frequency of each character
6+ count = Counter (s )
7+ max_freq = max (count .values ())
8+
9+ # Find characters with maximum frequency
10+ max_chars = [char for char , freq in count .items () if freq == max_freq ]
11+
12+ # Find last occurrence of each max frequency character
13+ last_positions = {}
14+ for i , char in enumerate (s ):
15+ if char in max_chars :
16+ last_positions [char ] = i
17+
18+ # Sort by last position and build result
19+ sorted_chars = sorted (max_chars , key = lambda x : last_positions [x ])
20+ res = "" .join (sorted_chars )
21+
22+ return res
You can’t perform that action at this time.
0 commit comments