diff --git a/solution/0300-0399/0383.Ransom Note/README.md b/solution/0300-0399/0383.Ransom Note/README.md index 5c720a3b45638..44cabea0cfd21 100644 --- a/solution/0300-0399/0383.Ransom Note/README.md +++ b/solution/0300-0399/0383.Ransom Note/README.md @@ -136,7 +136,7 @@ func canConstruct(ransomNote string, magazine string) bool { ```ts function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/0300-0399/0383.Ransom Note/README_EN.md b/solution/0300-0399/0383.Ransom Note/README_EN.md index 2c792b73cd809..e3a3698e4ef77 100644 --- a/solution/0300-0399/0383.Ransom Note/README_EN.md +++ b/solution/0300-0399/0383.Ransom Note/README_EN.md @@ -113,7 +113,7 @@ func canConstruct(ransomNote string, magazine string) bool { ```ts function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/0300-0399/0383.Ransom Note/Solution.ts b/solution/0300-0399/0383.Ransom Note/Solution.ts index 66a65595b63e0..7fe6ae0705da8 100644 --- a/solution/0300-0399/0383.Ransom Note/Solution.ts +++ b/solution/0300-0399/0383.Ransom Note/Solution.ts @@ -1,5 +1,5 @@ function canConstruct(ransomNote: string, magazine: string): boolean { - const cnt = new Array(26).fill(0); + const cnt: number[] = Array(26).fill(0); for (const c of magazine) { ++cnt[c.charCodeAt(0) - 97]; } diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md index bb4fbf59fc392..c626bbeccbab7 100644 --- a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README.md @@ -50,7 +50,17 @@ ```python - +class Solution: + def missingInteger(self, nums: List[int]) -> int: + s, n = nums[0], len(nums) + j = 1 + while j < len(nums) and nums[j] == nums[j - 1] + 1: + s += nums[j] + j += 1 + vis = set(nums) + for x in count(s): + if x not in vis: + return x ``` ### **Java** @@ -58,19 +68,87 @@ ```java - +class Solution { + public int missingInteger(int[] nums) { + int s = nums[0], j = 1; + while (j < nums.length && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + boolean[] vis = new boolean[51]; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int missingInteger(vector& nums) { + int s = nums[0], j = 1; + while (j < nums.size() && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + bool vis[51]{}; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= 51 || !vis[x]) { + return x; + } + } + } +}; ``` ### **Go** ```go +func missingInteger(nums []int) int { + s, j := nums[0], 1 + for j < len(nums) && nums[j] == nums[j-1]+1 { + s, j = s+nums[j], j+1 + } + vis := [51]bool{} + for _, x := range nums { + vis[x] = true + } + for x := s; ; x++ { + if x >= len(vis) || !vis[x] { + return x + } + } +} +``` +### **TypeScript** + +```ts +function missingInteger(nums: number[]): number { + let [s, j] = [nums[0], 1]; + const n = nums.length; + while (j < n && nums[j] === nums[j - 1] + 1) { + s += nums[j++]; + } + const vis: boolean[] = Array(51).fill(false); + for (const x of nums) { + vis[x] = true; + } + for (let x = s; ; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } +} ``` ### **...** diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README_EN.md b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README_EN.md index a1edd49e3820e..9667644b4f00a 100644 --- a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README_EN.md +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/README_EN.md @@ -42,25 +42,103 @@ ### **Python3** ```python - +class Solution: + def missingInteger(self, nums: List[int]) -> int: + s, n = nums[0], len(nums) + j = 1 + while j < len(nums) and nums[j] == nums[j - 1] + 1: + s += nums[j] + j += 1 + vis = set(nums) + for x in count(s): + if x not in vis: + return x ``` ### **Java** ```java - +class Solution { + public int missingInteger(int[] nums) { + int s = nums[0], j = 1; + while (j < nums.length && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + boolean[] vis = new boolean[51]; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int missingInteger(vector& nums) { + int s = nums[0], j = 1; + while (j < nums.size() && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + bool vis[51]{}; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= 51 || !vis[x]) { + return x; + } + } + } +}; ``` ### **Go** ```go +func missingInteger(nums []int) int { + s, j := nums[0], 1 + for j < len(nums) && nums[j] == nums[j-1]+1 { + s, j = s+nums[j], j+1 + } + vis := [51]bool{} + for _, x := range nums { + vis[x] = true + } + for x := s; ; x++ { + if x >= len(vis) || !vis[x] { + return x + } + } +} +``` +### **TypeScript** + +```ts +function missingInteger(nums: number[]): number { + let [s, j] = [nums[0], 1]; + const n = nums.length; + while (j < n && nums[j] === nums[j - 1] + 1) { + s += nums[j++]; + } + const vis: boolean[] = Array(51).fill(false); + for (const x of nums) { + vis[x] = true; + } + for (let x = s; ; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } +} ``` ### **...** diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.cpp b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.cpp new file mode 100644 index 0000000000000..6081ba54c6b44 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int missingInteger(vector& nums) { + int s = nums[0], j = 1; + while (j < nums.size() && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + bool vis[51]{}; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= 51 || !vis[x]) { + return x; + } + } + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.go b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.go new file mode 100644 index 0000000000000..26f4a6d360c78 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.go @@ -0,0 +1,15 @@ +func missingInteger(nums []int) int { + s, j := nums[0], 1 + for j < len(nums) && nums[j] == nums[j-1]+1 { + s, j = s+nums[j], j+1 + } + vis := [51]bool{} + for _, x := range nums { + vis[x] = true + } + for x := s; ; x++ { + if x >= len(vis) || !vis[x] { + return x + } + } +} \ No newline at end of file diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.java b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.java new file mode 100644 index 0000000000000..14710bee62193 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public int missingInteger(int[] nums) { + int s = nums[0], j = 1; + while (j < nums.length && nums[j] == nums[j - 1] + 1) { + s += nums[j++]; + } + boolean[] vis = new boolean[51]; + for (int x : nums) { + vis[x] = true; + } + for (int x = s;; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } + } +} \ No newline at end of file diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.py b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.py new file mode 100644 index 0000000000000..70bc58d87facf --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def missingInteger(self, nums: List[int]) -> int: + s, n = nums[0], len(nums) + j = 1 + while j < len(nums) and nums[j] == nums[j - 1] + 1: + s += nums[j] + j += 1 + vis = set(nums) + for x in count(s): + if x not in vis: + return x diff --git a/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.ts b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.ts new file mode 100644 index 0000000000000..97932f3652710 --- /dev/null +++ b/solution/10000-10099/10031.Smallest Missing Integer Greater Than Sequential Prefix Sum/Solution.ts @@ -0,0 +1,16 @@ +function missingInteger(nums: number[]): number { + let [s, j] = [nums[0], 1]; + const n = nums.length; + while (j < n && nums[j] === nums[j - 1] + 1) { + s += nums[j++]; + } + const vis: boolean[] = Array(51).fill(false); + for (const x of nums) { + vis[x] = true; + } + for (let x = s; ; ++x) { + if (x >= vis.length || !vis[x]) { + return x; + } + } +} diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README.md b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README.md index 5d2aec6532e0b..9630bdd603fe3 100644 --- a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README.md +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README.md @@ -61,7 +61,15 @@ ```python - +class Solution: + def minOperations(self, nums: List[int], k: int) -> int: + ans = 0 + for i in range(20): + v = 0 + for x in nums: + v ^= x >> i & 1 + ans += (k >> i & 1) != v + return ans ``` ### **Java** @@ -69,19 +77,69 @@ ```java - +class Solution { + public int minOperations(int[] nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minOperations(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +}; ``` ### **Go** ```go +func minOperations(nums []int, k int) (ans int) { + for i := 0; i < 20; i++ { + v := 0 + for _, x := range nums { + v ^= x >> i & 1 + } + ans += k>>i&1 ^ v + } + return +} +``` +### **TypeScript** + +```ts +function minOperations(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < 20; ++i) { + let v = 0; + for (const x of nums) { + v ^= (x >> i) & 1; + } + ans += ((k >> i) & 1) ^ v; + } + return ans; +} ``` ### **...** diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md index cc60b43f70e3a..1d44e8a3661ef 100644 --- a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md @@ -53,25 +53,83 @@ It can be shown that we cannot make the XOR equal to k in less than 2 operations ### **Python3** ```python - +class Solution: + def minOperations(self, nums: List[int], k: int) -> int: + ans = 0 + for i in range(20): + v = 0 + for x in nums: + v ^= x >> i & 1 + ans += (k >> i & 1) != v + return ans ``` ### **Java** ```java - +class Solution { + public int minOperations(int[] nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minOperations(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +}; ``` ### **Go** ```go +func minOperations(nums []int, k int) (ans int) { + for i := 0; i < 20; i++ { + v := 0 + for _, x := range nums { + v ^= x >> i & 1 + } + ans += k>>i&1 ^ v + } + return +} +``` +### **TypeScript** + +```ts +function minOperations(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < 20; ++i) { + let v = 0; + for (const x of nums) { + v ^= (x >> i) & 1; + } + ans += ((k >> i) & 1) ^ v; + } + return ans; +} ``` ### **...** diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp new file mode 100644 index 0000000000000..7717cfa06fc7d --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int minOperations(vector& nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go new file mode 100644 index 0000000000000..a8c6b5278ac66 --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go @@ -0,0 +1,10 @@ +func minOperations(nums []int, k int) (ans int) { + for i := 0; i < 20; i++ { + v := 0 + for _, x := range nums { + v ^= x >> i & 1 + } + ans += k>>i&1 ^ v + } + return +} \ No newline at end of file diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java new file mode 100644 index 0000000000000..5c13d4f76094b --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int minOperations(int[] nums, int k) { + int ans = 0; + for (int i = 0; i < 20; ++i) { + int v = 0; + for (int x : nums) { + v ^= (x >> i & 1); + } + ans += k >> i & 1 ^ v; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py new file mode 100644 index 0000000000000..92fdc83805505 --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def minOperations(self, nums: List[int], k: int) -> int: + ans = 0 + for i in range(20): + v = 0 + for x in nums: + v ^= x >> i & 1 + ans += (k >> i & 1) != v + return ans diff --git a/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts new file mode 100644 index 0000000000000..142deff264ddb --- /dev/null +++ b/solution/10000-10099/10032.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts @@ -0,0 +1,11 @@ +function minOperations(nums: number[], k: number): number { + let ans = 0; + for (let i = 0; i < 20; ++i) { + let v = 0; + for (const x of nums) { + v ^= (x >> i) & 1; + } + ans += ((k >> i) & 1) ^ v; + } + return ans; +} diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README.md b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README.md index 09ca5b0e33469..ed84d0115a5e6 100644 --- a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README.md +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README.md @@ -79,7 +79,20 @@ ```python - +class Solution: + def minimumOperationsToMakeEqual(self, x: int, y: int) -> int: + @cache + def dfs(x: int) -> int: + if y >= x: + return y - x + ans = x - y + ans = min(ans, x % 5 + 1 + dfs(x // 5)) + ans = min(ans, 5 - x % 5 + 1 + dfs(x // 5 + 1)) + ans = min(ans, x % 11 + 1 + dfs(x // 11)) + ans = min(ans, 11 - x % 11 + 1 + dfs(x // 11 + 1)) + return ans + + return dfs(x) ``` ### **Java** @@ -87,19 +100,105 @@ ```java - +class Solution { + private Map f = new HashMap<>(); + private int y; + + public int minimumOperationsToMakeEqual(int x, int y) { + this.y = y; + return dfs(x); + } + + private int dfs(int x) { + if (y >= x) { + return y - x; + } + if (f.containsKey(x)) { + return f.get(x); + } + int ans = x - y; + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + ans = Math.min(ans, Math.min(a, Math.min(b, Math.min(c, d)))); + f.put(x, ans); + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minimumOperationsToMakeEqual(int x, int y) { + unordered_map f; + function dfs = [&](int x) { + if (y >= x) { + return y - x; + } + if (f.count(x)) { + return f[x]; + } + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + return f[x] = min({x - y, a, b, c, d}); + }; + return dfs(x); + } +}; ``` ### **Go** ```go +func minimumOperationsToMakeEqual(x int, y int) int { + f := map[int]int{} + var dfs func(int) int + dfs = func(x int) int { + if y >= x { + return y - x + } + if v, ok := f[x]; ok { + return v + } + a := x%5 + 1 + dfs(x/5) + b := 5 - x%5 + 1 + dfs(x/5+1) + c := x%11 + 1 + dfs(x/11) + d := 11 - x%11 + 1 + dfs(x/11+1) + f[x] = min(x-y, a, b, c, d) + return f[x] + } + return dfs(x) +} +``` +### **TypeScript** + +```ts +function minimumOperationsToMakeEqual(x: number, y: number): number { + const f: Map = new Map(); + const dfs = (x: number): number => { + if (y >= x) { + return y - x; + } + if (f.has(x)) { + return f.get(x)!; + } + const a = (x % 5) + 1 + dfs((x / 5) | 0); + const b = 5 - (x % 5) + 1 + dfs(((x / 5) | 0) + 1); + const c = (x % 11) + 1 + dfs((x / 11) | 0); + const d = 11 - (x % 11) + 1 + dfs(((x / 11) | 0) + 1); + const ans = Math.min(x - y, a, b, c, d); + f.set(x, ans); + return ans; + }; + return dfs(x); +} ``` ### **...** diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README_EN.md b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README_EN.md index 7377bf7787b80..e88607cad372f 100644 --- a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README_EN.md +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/README_EN.md @@ -71,25 +71,124 @@ It can be shown that 5 is the minimum number of operations required to make 25 e ### **Python3** ```python - +class Solution: + def minimumOperationsToMakeEqual(self, x: int, y: int) -> int: + @cache + def dfs(x: int) -> int: + if y >= x: + return y - x + ans = x - y + ans = min(ans, x % 5 + 1 + dfs(x // 5)) + ans = min(ans, 5 - x % 5 + 1 + dfs(x // 5 + 1)) + ans = min(ans, x % 11 + 1 + dfs(x // 11)) + ans = min(ans, 11 - x % 11 + 1 + dfs(x // 11 + 1)) + return ans + + return dfs(x) ``` ### **Java** ```java - +class Solution { + private Map f = new HashMap<>(); + private int y; + + public int minimumOperationsToMakeEqual(int x, int y) { + this.y = y; + return dfs(x); + } + + private int dfs(int x) { + if (y >= x) { + return y - x; + } + if (f.containsKey(x)) { + return f.get(x); + } + int ans = x - y; + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + ans = Math.min(ans, Math.min(a, Math.min(b, Math.min(c, d)))); + f.put(x, ans); + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int minimumOperationsToMakeEqual(int x, int y) { + unordered_map f; + function dfs = [&](int x) { + if (y >= x) { + return y - x; + } + if (f.count(x)) { + return f[x]; + } + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + return f[x] = min({x - y, a, b, c, d}); + }; + return dfs(x); + } +}; ``` ### **Go** ```go +func minimumOperationsToMakeEqual(x int, y int) int { + f := map[int]int{} + var dfs func(int) int + dfs = func(x int) int { + if y >= x { + return y - x + } + if v, ok := f[x]; ok { + return v + } + a := x%5 + 1 + dfs(x/5) + b := 5 - x%5 + 1 + dfs(x/5+1) + c := x%11 + 1 + dfs(x/11) + d := 11 - x%11 + 1 + dfs(x/11+1) + f[x] = min(x-y, a, b, c, d) + return f[x] + } + return dfs(x) +} +``` +### **TypeScript** + +```ts +function minimumOperationsToMakeEqual(x: number, y: number): number { + const f: Map = new Map(); + const dfs = (x: number): number => { + if (y >= x) { + return y - x; + } + if (f.has(x)) { + return f.get(x)!; + } + const a = (x % 5) + 1 + dfs((x / 5) | 0); + const b = 5 - (x % 5) + 1 + dfs(((x / 5) | 0) + 1); + const c = (x % 11) + 1 + dfs((x / 11) | 0); + const d = 11 - (x % 11) + 1 + dfs(((x / 11) | 0) + 1); + const ans = Math.min(x - y, a, b, c, d); + f.set(x, ans); + return ans; + }; + return dfs(x); +} ``` ### **...** diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.cpp b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.cpp new file mode 100644 index 0000000000000..5f739d9aa3f3f --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minimumOperationsToMakeEqual(int x, int y) { + unordered_map f; + function dfs = [&](int x) { + if (y >= x) { + return y - x; + } + if (f.count(x)) { + return f[x]; + } + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + return f[x] = min({x - y, a, b, c, d}); + }; + return dfs(x); + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.go b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.go new file mode 100644 index 0000000000000..f06dea6c22c6f --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.go @@ -0,0 +1,19 @@ +func minimumOperationsToMakeEqual(x int, y int) int { + f := map[int]int{} + var dfs func(int) int + dfs = func(x int) int { + if y >= x { + return y - x + } + if v, ok := f[x]; ok { + return v + } + a := x%5 + 1 + dfs(x/5) + b := 5 - x%5 + 1 + dfs(x/5+1) + c := x%11 + 1 + dfs(x/11) + d := 11 - x%11 + 1 + dfs(x/11+1) + f[x] = min(x-y, a, b, c, d) + return f[x] + } + return dfs(x) +} \ No newline at end of file diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.java b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.java new file mode 100644 index 0000000000000..8faf975eabce1 --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.java @@ -0,0 +1,26 @@ +class Solution { + private Map f = new HashMap<>(); + private int y; + + public int minimumOperationsToMakeEqual(int x, int y) { + this.y = y; + return dfs(x); + } + + private int dfs(int x) { + if (y >= x) { + return y - x; + } + if (f.containsKey(x)) { + return f.get(x); + } + int ans = x - y; + int a = x % 5 + 1 + dfs(x / 5); + int b = 5 - x % 5 + 1 + dfs(x / 5 + 1); + int c = x % 11 + 1 + dfs(x / 11); + int d = 11 - x % 11 + 1 + dfs(x / 11 + 1); + ans = Math.min(ans, Math.min(a, Math.min(b, Math.min(c, d)))); + f.put(x, ans); + return ans; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.py b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.py new file mode 100644 index 0000000000000..bcfbce832d4ef --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def minimumOperationsToMakeEqual(self, x: int, y: int) -> int: + @cache + def dfs(x: int) -> int: + if y >= x: + return y - x + ans = x - y + ans = min(ans, x % 5 + 1 + dfs(x // 5)) + ans = min(ans, 5 - x % 5 + 1 + dfs(x // 5 + 1)) + ans = min(ans, x % 11 + 1 + dfs(x // 11)) + ans = min(ans, 11 - x % 11 + 1 + dfs(x // 11 + 1)) + return ans + + return dfs(x) diff --git a/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.ts b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.ts new file mode 100644 index 0000000000000..27a3b8492f4cd --- /dev/null +++ b/solution/10000-10099/10033.Minimum Number of Operations to Make X and Y Equal/Solution.ts @@ -0,0 +1,19 @@ +function minimumOperationsToMakeEqual(x: number, y: number): number { + const f: Map = new Map(); + const dfs = (x: number): number => { + if (y >= x) { + return y - x; + } + if (f.has(x)) { + return f.get(x)!; + } + const a = (x % 5) + 1 + dfs((x / 5) | 0); + const b = 5 - (x % 5) + 1 + dfs(((x / 5) | 0) + 1); + const c = (x % 11) + 1 + dfs((x / 11) | 0); + const d = 11 - (x % 11) + 1 + dfs(((x / 11) | 0) + 1); + const ans = Math.min(x - y, a, b, c, d); + f.set(x, ans); + return ans; + }; + return dfs(x); +}