diff --git a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README.md b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README.md index 04c05024478da..83c7858aafa51 100644 --- a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README.md +++ b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README.md @@ -65,19 +65,113 @@ ```python - +class Solution: + def minOrAfterOperations(self, nums: List[int], k: int) -> int: + ans = 0 + rans = 0 + for i in range(29, -1, -1): + test = ans + (1 << i) + cnt = 0 + val = 0 + for num in nums: + if val == 0: + val = test & num + else: + val &= test & num + if val: + cnt += 1 + if cnt > k: + rans += 1 << i + else: + ans += 1 << i + return rans ``` ```java - +class Solution { + public int minOrAfterOperations(int[] nums, int k) { + int ans = 0, rans = 0; + for (int i = 29; i >= 0; i--) { + int test = ans + (1 << i); + int cnt = 0; + int val = 0; + for (int num : nums) { + if (val == 0) { + val = test & num; + } else { + val &= test & num; + } + if (val != 0) { + cnt++; + } + } + if (cnt > k) { + rans += (1 << i); + } else { + ans += (1 << i); + } + } + return rans; + } +} ``` ```cpp - +class Solution { +public: + int minOrAfterOperations(vector& nums, int k) { + int ans = 0, rans = 0; + for (int i = 29; i >= 0; i--) { + int test = ans + (1 << i); + int cnt = 0; + int val = 0; + for (auto it : nums) { + if (val == 0) { + val = test & it; + } else { + val &= test & it; + } + if (val) { + cnt++; + } + } + if (cnt > k) { + rans += (1 << i); + } else { + ans += (1 << i); + } + } + return rans; + } +}; ``` ```go - +func minOrAfterOperations(nums []int, k int) int { + ans := 0 + rans := 0 + for i := 29; i >= 0; i-- { + test := ans + (1 << i) + cnt := 0 + val := 0 + for _, num := range nums { + if val == 0 { + val = test & num + } else { + val &= test & num + } + if val != 0 { + cnt++ + } + } + if cnt > k { + rans += (1 << i) + } else { + ans += (1 << i) + } + } + return rans +} ``` diff --git a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README_EN.md b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README_EN.md index 6803513199684..c5d883980ef75 100644 --- a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README_EN.md +++ b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README_EN.md @@ -61,19 +61,113 @@ It can be shown that 15 is the minimum possible value of the bitwise OR of the r ```python - +class Solution: + def minOrAfterOperations(self, nums: List[int], k: int) -> int: + ans = 0 + rans = 0 + for i in range(29, -1, -1): + test = ans + (1 << i) + cnt = 0 + val = 0 + for num in nums: + if val == 0: + val = test & num + else: + val &= test & num + if val: + cnt += 1 + if cnt > k: + rans += 1 << i + else: + ans += 1 << i + return rans ``` ```java - +class Solution { + public int minOrAfterOperations(int[] nums, int k) { + int ans = 0, rans = 0; + for (int i = 29; i >= 0; i--) { + int test = ans + (1 << i); + int cnt = 0; + int val = 0; + for (int num : nums) { + if (val == 0) { + val = test & num; + } else { + val &= test & num; + } + if (val != 0) { + cnt++; + } + } + if (cnt > k) { + rans += (1 << i); + } else { + ans += (1 << i); + } + } + return rans; + } +} ``` ```cpp - +class Solution { +public: + int minOrAfterOperations(vector& nums, int k) { + int ans = 0, rans = 0; + for (int i = 29; i >= 0; i--) { + int test = ans + (1 << i); + int cnt = 0; + int val = 0; + for (auto it : nums) { + if (val == 0) { + val = test & it; + } else { + val &= test & it; + } + if (val) { + cnt++; + } + } + if (cnt > k) { + rans += (1 << i); + } else { + ans += (1 << i); + } + } + return rans; + } +}; ``` ```go - +func minOrAfterOperations(nums []int, k int) int { + ans := 0 + rans := 0 + for i := 29; i >= 0; i-- { + test := ans + (1 << i) + cnt := 0 + val := 0 + for _, num := range nums { + if val == 0 { + val = test & num + } else { + val &= test & num + } + if val != 0 { + cnt++ + } + } + if cnt > k { + rans += (1 << i) + } else { + ans += (1 << i) + } + } + return rans +} ``` diff --git a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.cpp b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.cpp new file mode 100644 index 0000000000000..5a321badeba37 --- /dev/null +++ b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int minOrAfterOperations(vector& nums, int k) { + int ans = 0, rans = 0; + for (int i = 29; i >= 0; i--) { + int test = ans + (1 << i); + int cnt = 0; + int val = 0; + for (auto it : nums) { + if (val == 0) { + val = test & it; + } else { + val &= test & it; + } + if (val) { + cnt++; + } + } + if (cnt > k) { + rans += (1 << i); + } else { + ans += (1 << i); + } + } + return rans; + } +}; diff --git a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.go b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.go new file mode 100644 index 0000000000000..9df187137ce73 --- /dev/null +++ b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.go @@ -0,0 +1,25 @@ +func minOrAfterOperations(nums []int, k int) int { + ans := 0 + rans := 0 + for i := 29; i >= 0; i-- { + test := ans + (1 << i) + cnt := 0 + val := 0 + for _, num := range nums { + if val == 0 { + val = test & num + } else { + val &= test & num + } + if val != 0 { + cnt++ + } + } + if cnt > k { + rans += (1 << i) + } else { + ans += (1 << i) + } + } + return rans +} diff --git a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.java b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.java new file mode 100644 index 0000000000000..5c795bc8c74fb --- /dev/null +++ b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.java @@ -0,0 +1,26 @@ +class Solution { + public int minOrAfterOperations(int[] nums, int k) { + int ans = 0, rans = 0; + for (int i = 29; i >= 0; i--) { + int test = ans + (1 << i); + int cnt = 0; + int val = 0; + for (int num : nums) { + if (val == 0) { + val = test & num; + } else { + val &= test & num; + } + if (val != 0) { + cnt++; + } + } + if (cnt > k) { + rans += (1 << i); + } else { + ans += (1 << i); + } + } + return rans; + } +} diff --git a/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.py b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.py new file mode 100644 index 0000000000000..a9c94fb9810ad --- /dev/null +++ b/solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/Solution.py @@ -0,0 +1,20 @@ +class Solution: + def minOrAfterOperations(self, nums: List[int], k: int) -> int: + ans = 0 + rans = 0 + for i in range(29, -1, -1): + test = ans + (1 << i) + cnt = 0 + val = 0 + for num in nums: + if val == 0: + val = test & num + else: + val &= test & num + if val: + cnt += 1 + if cnt > k: + rans += 1 << i + else: + ans += 1 << i + return rans