Skip to content

Commit

Permalink
Nov 6
Browse files Browse the repository at this point in the history
  • Loading branch information
siddydutta committed Nov 6, 2024
1 parent f4b1065 commit 329711a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
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()
4 changes: 2 additions & 2 deletions 2024-11-November-LeetCoding-Challenge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
| November 3 | [796. Rotate String](https://leetcode.com/problems/rotate-string/) | Easy | Solved |
| November 4 | [3163. String Compression III](https://leetcode.com/problems/string-compression-iii/) | Medium | Solved |
| November 5 | [2914. Minimum Number of Changes to Make Binary String Beautiful](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/) | Medium | Solved |
| November 6 | []() | | |
| November 6 | [3011. Find if Array Can Be Sorted](https://leetcode.com/problems/find-if-array-can-be-sorted/) | Medium | Unsolved |
| November 7 | []() | | |
| November 8 | []() | | |
| November 9 | []() | | |
Expand Down Expand Up @@ -38,5 +38,5 @@
| Level | Problems | Solved | Unsolved |
| --- | --- | --- | --- |
| Easy | 3 | 3 | 0 |
| Medium | 2 | 2 | 0 |
| Medium | 3 | 2 | 1 |
| Hard | 0 | 0 | 0 |

0 comments on commit 329711a

Please sign in to comment.