-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4b1065
commit 329711a
Showing
2 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
2024-11-November-LeetCoding-Challenge/Find if Array Can Be Sorted.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def canSortArray(self, nums: List[int]) -> bool: | ||
prev_max, prev_count = 0, 0 | ||
curr_min, curr_max = 0, 0 | ||
for num in nums: | ||
count = num.bit_count() | ||
if count == prev_count: | ||
curr_min = min(curr_min, num) | ||
curr_max = max(curr_max, num) | ||
elif curr_min < prev_max: | ||
return False | ||
else: | ||
prev_max = curr_max | ||
curr_min, curr_max = num, num | ||
prev_count = count | ||
return curr_min >= prev_max | ||
|
||
|
||
def main(): | ||
nums = [8, 4, 2, 30, 15] | ||
assert Solution().canSortArray(nums) is True | ||
|
||
nums = [1, 2, 3, 4, 5] | ||
assert Solution().canSortArray(nums) is True | ||
|
||
nums = [3, 16, 8, 4, 2] | ||
assert Solution().canSortArray(nums) is False | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters