Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working on leetcode #180

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions Interview QA/LeetCode75/43_dominant_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@ def dominant_index(self, nums: List[int]) -> int:
if len(nums) == 0:
return -1

max_index = 0
max_value = nums[0]
max_val = max(nums)
temp = []

# Find the index of the largest element
for i in range(1, len(nums)):
if nums[i] > max_value:
max_value = nums[i]
max_index = i

# Check if the largest element is at least twice as large as all other elements
for i in range(len(nums)):
if i != max_index and nums[i] * 2 > max_value:
return -1
if nums[i] != max_val:
temp.append(nums[i])
if (max(temp)*2) <= max_val:
return nums.index(max_val)

return max_index
return -1


if __name__ == "__main__":
sol = Solution()
print(sol.dominant_index([3,6,1,0]))
print(sol.dominant_index([3,9,1,0]))
9 changes: 6 additions & 3 deletions Interview QA/LeetCode75/46_counting_bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

class Solution:
def count_bits(self, n: int) -> List[int]:
ans = [0] * (n + 1)
res = [0] * (n + 1)
offset = 1

for i in range(1, n + 1):
ans[i] = ans[i >> 1] + (i & 1)
if offset * 2 == i:
offset = i
res[i] = 1 + res[i - offset]

return ans
return res


if __name__ == "__main__":
Expand Down
Loading