From d568ef58f825f339dbe9d816865e4adc0b1c93f7 Mon Sep 17 00:00:00 2001 From: jalil_kartal Date: Mon, 15 Jul 2024 12:39:08 +0200 Subject: [PATCH] working on leetcode --- Interview QA/LeetCode75/43_dominant_index.py | 21 ++++++++------------ Interview QA/LeetCode75/46_counting_bits.py | 9 ++++++--- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Interview QA/LeetCode75/43_dominant_index.py b/Interview QA/LeetCode75/43_dominant_index.py index 5d3ef12..21b0a94 100644 --- a/Interview QA/LeetCode75/43_dominant_index.py +++ b/Interview QA/LeetCode75/43_dominant_index.py @@ -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])) \ No newline at end of file + print(sol.dominant_index([3,9,1,0])) \ No newline at end of file diff --git a/Interview QA/LeetCode75/46_counting_bits.py b/Interview QA/LeetCode75/46_counting_bits.py index 9d24fbb..806a5bf 100644 --- a/Interview QA/LeetCode75/46_counting_bits.py +++ b/Interview QA/LeetCode75/46_counting_bits.py @@ -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__":