Skip to content

Commit

Permalink
Added Python solution for "2147. Number of Ways to Divide a Long Corr…
Browse files Browse the repository at this point in the history
…idor"
  • Loading branch information
chemandante committed Nov 29, 2023
1 parent 8e3689c commit 25993e9
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 80 deletions.
38 changes: 38 additions & 0 deletions 21/47/m2147.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Solved:
# (H) Number of Ways to Divide a Long Corridor
# https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/

import math

from typing import List


class Solution:
def numberOfWays(self, corridor: str) -> int:
"""
All we need - count all plants in the gap between pairs of seats `gap[i]`
Then the result is the product of all (gap[i] + 1).
There is no need to make an array of gaps - do all the job in one pass
Odd or zero number of seats makes the division impossible, so we return 0 in that case.
"""
res = 1 # Result
total_seats = 0 # Total seats, need for final check
seats, gap = 0, 0 # Current seat and gap counters
# We do everything in one pass, iterating char-by-char
for c in corridor:
if c == 'S':
total_seats += 1
if seats == 2: # If we found another seat after pair, mind the gap
res = (res * (gap + 1)) % 1000000007
seats, gap = 1, 0
else:
seats += 1 # Count seats till a pair will be found
else:
if seats == 2: # Count plants after last seat in pair
gap += 1

# Final check for the case where it's impossible to divide corridor
if total_seats == 0 or total_seats % 2 == 1:
res = 0

return res
94 changes: 47 additions & 47 deletions 2183/m2183.py → 21/83/m2183.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
# Solved:
# (H) Count Array Pairs Divisible by K
# https://leetcode.com/problems/count-array-pairs-divisible-by-k/

import math

from typing import List

class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
"""
Key idea to solve this problem is:
N1 * N2 is divisible by K if and only if GCD(N1,K) * GCD(N2,K) is divisible by K
So we will group all input numbers by value of GCD(number, k) and count them (using dictionary in Python).
We do not need to keep the exact input value, just GCD(N,K) and count of numbers with such GCD.
Then we iterate through this dictionary and try to determine when product of GCD's are divisible by K.
When we found such pair of GCD's we count number of possible combinations in two different ways.
One for N1 != N2 (product of number count C(N1) * C(N2), then divide by 2, because N1 * N2 = N2 * N1)
Another for N1 == N2 - in this case we calculate number of combinations from C(N1) by 2.
"""
gcd = {}
res_d = 0 #
res_s = 0
# Grouping input numbers by GCD(n,K) value and count them
for n in nums:
g = math.gcd(n, k)
gcd[g] = gcd.get(g, 0) + 1
# Calculating combinations
for n1, c1 in gcd.items():
kr = k // n1
for n2, c2 in gcd.items():
if n2 % kr == 0:
if n1 == n2:
res_s += c1 * (c1 - 1) // 2 # Number of combinations from c1 by 2
else:
res_d += c1 * c2

return res_d // 2 + res_s


x = Solution()
nums = [1,2,3,4,5]
k = 2

print(x.countPairs(nums, k))
# print(f"List length = {len(data.data)}")
# print(x.countPairs(data.data, data.k))
# Solved:
# (H) Count Array Pairs Divisible by K
# https://leetcode.com/problems/count-array-pairs-divisible-by-k/

import math

from typing import List

class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
"""
Key idea to solve this problem is:
N1 * N2 is divisible by K if and only if GCD(N1,K) * GCD(N2,K) is divisible by K
So we will group all input numbers by value of GCD(number, k) and count them (using dictionary in Python).
We do not need to keep the exact input value, just GCD(N,K) and count of numbers with such GCD.
Then we iterate through this dictionary and try to determine when product of GCD's are divisible by K.
When we found such pair of GCD's we count number of possible combinations in two different ways.
One for N1 != N2 (product of number count C(N1) * C(N2), then divide by 2, because N1 * N2 = N2 * N1)
Another for N1 == N2 - in this case we calculate number of combinations from C(N1) by 2.
"""
gcd = {}
res_d = 0 #
res_s = 0
# Grouping input numbers by GCD(n,K) value and count them
for n in nums:
g = math.gcd(n, k)
gcd[g] = gcd.get(g, 0) + 1
# Calculating combinations
for n1, c1 in gcd.items():
kr = k // n1
for n2, c2 in gcd.items():
if n2 % kr == 0:
if n1 == n2:
res_s += c1 * (c1 - 1) // 2 # Number of combinations from c1 by 2
else:
res_d += c1 * c2

return res_d // 2 + res_s


x = Solution()
nums = [1,2,3,4,5]
k = 2

print(x.countPairs(nums, k))
# print(f"List length = {len(data.data)}")
# print(x.countPairs(data.data, data.k))
66 changes: 33 additions & 33 deletions 2197/m2197.py → 21/97/m2197.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# Solved:
# (H) Replace Non-Coprime Numbers in Array
# https://leetcode.com/problems/replace-non-coprime-numbers-in-array/

import math
from typing import List


class Solution:
def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
idx = 0
idxEnd = len(nums) - 1
while idx < idxEnd:
x1 = nums[idx]
x2 = nums[idx + 1]
gcd = math.gcd(x1, x2)
if gcd != 1:
nums[idx] = x1 * x2 // gcd
del nums[idx + 1]
idxEnd -= 1
idx = idx - 1 if idx > 0 else idx
else:
idx += 1

return nums

# x = Solution()
#
# data = [6,4,3,2,7,6,2]
# print(x.replaceNonCoprimes(data))
# #exit(0)
# data = [287,41,49,287,899,23,23,20677,5,825]
# print(x.replaceNonCoprimes(data))
# Solved:
# (H) Replace Non-Coprime Numbers in Array
# https://leetcode.com/problems/replace-non-coprime-numbers-in-array/

import math
from typing import List


class Solution:
def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
idx = 0
idxEnd = len(nums) - 1
while idx < idxEnd:
x1 = nums[idx]
x2 = nums[idx + 1]
gcd = math.gcd(x1, x2)
if gcd != 1:
nums[idx] = x1 * x2 // gcd
del nums[idx + 1]
idxEnd -= 1
idx = idx - 1 if idx > 0 else idx
else:
idx += 1

return nums

# x = Solution()
#
# data = [6,4,3,2,7,6,2]
# print(x.replaceNonCoprimes(data))
# #exit(0)
# data = [287,41,49,287,899,23,23,20677,5,825]
# print(x.replaceNonCoprimes(data))

0 comments on commit 25993e9

Please sign in to comment.