Skip to content

Commit

Permalink
contest: biweekly 122
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwin-nair98 committed Jan 21, 2024
1 parent 278eb62 commit 075e2a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 3010_Divide Array Min Cost 1/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def minimumCost(self, nums: List[int]) -> int:
minCost = nums[0]

min1, min2 = 51, 51
for i in range(1, len(nums)):
if nums[i] < min1:
min2 = min1
min1 = nums[i]
elif nums[i] < min2:
min2 = nums[i]

return minCost + min1 + min2
20 changes: 20 additions & 0 deletions 3011_Can Sort Array/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def countSetBits(self, num: int) -> int:
count = 0
while(num):
count += num & 1
num >>= 1
return count

def canSortArray(self, nums: List[int]) -> bool:
n = len(nums)
for i in range(n):
for j in range(n-i-1):
if nums[j] > nums[j+1]:
# Need to swap
if self.countSetBits(nums[j]) == self.countSetBits(nums[j+1]):
# Can swap
nums[j], nums[j+1] = nums[j+1], nums[j]
else:
return False
return True

0 comments on commit 075e2a9

Please sign in to comment.