From 0ddb1b7f4782743f9d108b11992c631e88ee636e Mon Sep 17 00:00:00 2001 From: yanglbme <szuyanglb@outlook.com> Date: Tue, 5 Dec 2023 14:05:57 +0800 Subject: [PATCH 1/3] feat: add solutions to lc problems: No.1427,1428 * No.1427.Perform String Shifts * No.1428.Leftmost Column with at Least a One --- .../1427.Perform String Shifts/README.md | 28 ++- .../1427.Perform String Shifts/README_EN.md | 30 ++- .../1427.Perform String Shifts/Solution.java | 26 +- .../1427.Perform String Shifts/Solution.py | 14 +- .../1427.Perform String Shifts/Solution.ts | 11 + .../README.md | 234 ++++++++++++------ .../README_EN.md | 233 +++++++++++------ .../Solution.cpp | 66 +++-- .../Solution.cs | 29 +++ .../Solution.go | 33 ++- .../Solution.java | 62 +++-- .../Solution.py | 44 ++-- .../Solution.rs | 37 +++ .../Solution.ts | 27 ++ 14 files changed, 577 insertions(+), 297 deletions(-) create mode 100644 solution/1400-1499/1427.Perform String Shifts/Solution.ts create mode 100644 solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cs create mode 100644 solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.rs create mode 100644 solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.ts diff --git a/solution/1400-1499/1427.Perform String Shifts/README.md b/solution/1400-1499/1427.Perform String Shifts/README.md index 6041513aeafea..910c04ac18a4f 100644 --- a/solution/1400-1499/1427.Perform String Shifts/README.md +++ b/solution/1400-1499/1427.Perform String Shifts/README.md @@ -58,9 +58,9 @@ **方法一:模拟** -遍历 `shift`,累加(减)得到最终偏移量 $x$,取模后对字符串 `s` 进行左移或右移。 +我们不妨记字符串 $s$ 的长度为 $n$。接下来遍历数组 $shift$,累加得到最终的偏移量 $x$,然后将 $x$ 对 $n$ 取模,最终结果就是将 $s$ 的前 $n - x$ 个字符移动到末尾。 -时间复杂度 $O(n+m)$。其中 $n$ 为字符串 `s` 的长度,$m$ 为 `shift` 的长度。 +时间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是字符串 $s$ 的长度和数组 $shift$ 的长度。空间复杂度 $O(1)$。 <!-- tabs:start --> @@ -71,11 +71,7 @@ ```python class Solution: def stringShift(self, s: str, shift: List[List[int]]) -> str: - x = 0 - for a, b in shift: - if a == 0: - b = -b - x += b + x = sum((b if a else -b) for a, b in shift) x %= len(s) return s[-x:] + s[:-x] ``` @@ -90,7 +86,7 @@ class Solution { int x = 0; for (var e : shift) { if (e[0] == 0) { - e[1] = -e[1]; + e[1] *= -1; } x += e[1]; } @@ -138,6 +134,22 @@ func stringShift(s string, shift [][]int) string { } ``` +### **TypeScript** + +```ts +function stringShift(s: string, shift: number[][]): string { + let x = 0; + for (const [a, b] of shift) { + x += a === 0 ? -b : b; + } + x %= s.length; + if (x < 0) { + x += s.length; + } + return s.slice(-x) + s.slice(0, -x); +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1427.Perform String Shifts/README_EN.md b/solution/1400-1499/1427.Perform String Shifts/README_EN.md index 8aaf653884c1e..d4edfaf0dceea 100644 --- a/solution/1400-1499/1427.Perform String Shifts/README_EN.md +++ b/solution/1400-1499/1427.Perform String Shifts/README_EN.md @@ -50,6 +50,12 @@ ## Solutions +**Solution 1: Simulation** + +We can denote the length of the string $s$ as $n$. Next, we traverse the array $shift$, accumulate to get the final offset $x$, then take $x$ modulo $n$, the final result is to move the first $n - x$ characters of $s$ to the end. + +The time complexity is $O(n + m)$, where $n$ and $m$ are the lengths of the string $s$ and the array $shift$ respectively. The space complexity is $O(1)$. + <!-- tabs:start --> ### **Python3** @@ -57,11 +63,7 @@ ```python class Solution: def stringShift(self, s: str, shift: List[List[int]]) -> str: - x = 0 - for a, b in shift: - if a == 0: - b = -b - x += b + x = sum((b if a else -b) for a, b in shift) x %= len(s) return s[-x:] + s[:-x] ``` @@ -74,7 +76,7 @@ class Solution { int x = 0; for (var e : shift) { if (e[0] == 0) { - e[1] = -e[1]; + e[1] *= -1; } x += e[1]; } @@ -122,6 +124,22 @@ func stringShift(s string, shift [][]int) string { } ``` +### **TypeScript** + +```ts +function stringShift(s: string, shift: number[][]): string { + let x = 0; + for (const [a, b] of shift) { + x += a === 0 ? -b : b; + } + x %= s.length; + if (x < 0) { + x += s.length; + } + return s.slice(-x) + s.slice(0, -x); +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1427.Perform String Shifts/Solution.java b/solution/1400-1499/1427.Perform String Shifts/Solution.java index 97b76d90707cb..d7ffd1e9a332c 100644 --- a/solution/1400-1499/1427.Perform String Shifts/Solution.java +++ b/solution/1400-1499/1427.Perform String Shifts/Solution.java @@ -1,14 +1,14 @@ -class Solution { - public String stringShift(String s, int[][] shift) { - int x = 0; - for (var e : shift) { - if (e[0] == 0) { - e[1] = -e[1]; - } - x += e[1]; - } - int n = s.length(); - x = (x % n + n) % n; - return s.substring(n - x) + s.substring(0, n - x); - } +class Solution { + public String stringShift(String s, int[][] shift) { + int x = 0; + for (var e : shift) { + if (e[0] == 0) { + e[1] *= -1; + } + x += e[1]; + } + int n = s.length(); + x = (x % n + n) % n; + return s.substring(n - x) + s.substring(0, n - x); + } } \ No newline at end of file diff --git a/solution/1400-1499/1427.Perform String Shifts/Solution.py b/solution/1400-1499/1427.Perform String Shifts/Solution.py index 64dfb8318c356..d274495f65eb3 100644 --- a/solution/1400-1499/1427.Perform String Shifts/Solution.py +++ b/solution/1400-1499/1427.Perform String Shifts/Solution.py @@ -1,9 +1,5 @@ -class Solution: - def stringShift(self, s: str, shift: List[List[int]]) -> str: - x = 0 - for a, b in shift: - if a == 0: - b = -b - x += b - x %= len(s) - return s[-x:] + s[:-x] +class Solution: + def stringShift(self, s: str, shift: List[List[int]]) -> str: + x = sum((b if a else -b) for a, b in shift) + x %= len(s) + return s[-x:] + s[:-x] diff --git a/solution/1400-1499/1427.Perform String Shifts/Solution.ts b/solution/1400-1499/1427.Perform String Shifts/Solution.ts new file mode 100644 index 0000000000000..d66bf204a11f4 --- /dev/null +++ b/solution/1400-1499/1427.Perform String Shifts/Solution.ts @@ -0,0 +1,11 @@ +function stringShift(s: string, shift: number[][]): string { + let x = 0; + for (const [a, b] of shift) { + x += a === 0 ? -b : b; + } + x %= s.length; + if (x < 0) { + x += s.length; + } + return s.slice(-x) + s.slice(0, -x); +} diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/README.md b/solution/1400-1499/1428.Leftmost Column with at Least a One/README.md index 5f34e82bd6eed..8b71b03e34679 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/README.md +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/README.md @@ -76,7 +76,11 @@ <!-- 这里可写通用的实现逻辑 --> -二分查找。 +**方法一:二分查找** + +我们先调用 `BinaryMatrix.dimensions()` 得到矩阵的行数 $m$ 和列数 $n$,然后对于每一行,我们使用二分查找来找到最左边的 $1$ 所在的列数 $j$,找出所有行中最小的满足 $j$ 的值即为答案。如果不存在这样的列,则返回 $-1$。 + +时间复杂度 $O(m \times \log n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。需要遍历每一行,每一行内使用二分查找,时间复杂度为 $O(\log n)$。空间复杂度 $O(1)$。 <!-- tabs:start --> @@ -95,23 +99,13 @@ class Solution: - def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int: - rows, cols = binaryMatrix.dimensions() - res = -1 - for row in range(rows): - left, right = 0, cols - 1 - while left < right: - mid = (left + right) >> 1 - if binaryMatrix.get(row, mid) == 1: - right = mid - else: - left = mid + 1 - if binaryMatrix.get(row, left) == 1: - if res == -1: - res = left - else: - res = min(res, left) - return res + def leftMostColumnWithOne(self, binaryMatrix: "BinaryMatrix") -> int: + m, n = binaryMatrix.dimensions() + ans = n + for i in range(m): + j = bisect_left(range(n), 1, key=lambda k: binaryMatrix.get(i, k)) + ans = min(ans, j) + return -1 if ans >= n else ans ``` ### **Java** @@ -130,28 +124,22 @@ class Solution: class Solution { public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { - List<Integer> scale = binaryMatrix.dimensions(); - int rows = scale.get(0), cols = scale.get(1); - int res = -1; - for (int row = 0; row < rows; ++row) { - int left = 0, right = cols - 1; - while (left < right) { - int mid = (left + right) >> 1; - if (binaryMatrix.get(row, mid) == 1) { - right = mid; - } else { - left = mid + 1; - } - } - if (binaryMatrix.get(row, left) == 1) { - if (res == -1) { - res = left; + List<Integer> e = binaryMatrix.dimensions(); + int m = e.get(0), n = e.get(1); + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid) == 1) { + r = mid; } else { - res = Math.min(res, left); + l = mid + 1; } } + ans = Math.min(ans, l); } - return res; + return ans >= n ? -1 : ans; } } ``` @@ -171,29 +159,23 @@ class Solution { class Solution { public: - int leftMostColumnWithOne(BinaryMatrix& binaryMatrix) { - vector<int> scale = binaryMatrix.dimensions(); - int rows = scale[0], cols = scale[1]; - int res = -1; - for (int row = 0; row < rows; ++row) { - int left = 0, right = cols - 1; - while (left < right) { - int mid = left + right >> 1; - if (binaryMatrix.get(row, mid) == 1) { - right = mid; - } else { - left = mid + 1; - } - } - if (binaryMatrix.get(row, left) == 1) { - if (res == -1) { - res = left; + int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { + auto e = binaryMatrix.dimensions(); + int m = e[0], n = e[1]; + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid)) { + r = mid; } else { - res = min(res, left); + l = mid + 1; } } + ans = min(ans, l); } - return res; + return ans >= n ? -1 : ans; } }; ``` @@ -211,28 +193,134 @@ public: */ func leftMostColumnWithOne(binaryMatrix BinaryMatrix) int { - scale := binaryMatrix.Dimensions() - rows, cols := scale[0], scale[1] - res := -1 - for row := 0; row < rows; row++ { - left, right := 0, cols-1 - for left < right { - mid := (left + right) >> 1 - if binaryMatrix.Get(row, mid) == 1 { - right = mid + e := binaryMatrix.Dimensions() + m, n := e[0], e[1] + ans := n + for i := 0; i < m; i++ { + l, r := 0, n + for l < r { + mid := (l + r) >> 1 + if binaryMatrix.Get(i, mid) == 1 { + r = mid } else { - left = mid + 1 - } - } - if binaryMatrix.Get(row, left) == 1 { - if res == -1 { - res = left - } else { - res = min(res, left) + l = mid + 1 } } + ans = min(ans, l) + } + if ans >= n { + return -1 } - return res + return ans +} +``` + +### **TypeScript** + +```ts +/** + * // This is the BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * class BinaryMatrix { + * get(row: number, col: number): number {} + * + * dimensions(): number[] {} + * } + */ + +function leftMostColumnWithOne(binaryMatrix: BinaryMatrix) { + const [m, n] = binaryMatrix.dimensions(); + let ans = n; + for (let i = 0; i < m; ++i) { + let [l, r] = [0, n]; + while (l < r) { + const mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid) === 1) { + r = mid; + } else { + l = mid + 1; + } + } + ans = Math.min(ans, l); + } + return ans >= n ? -1 : ans; +} +``` + +### **Rust** + +```rust + +/** + * // This is the BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * struct BinaryMatrix; + * impl BinaryMatrix { + * fn get(row: i32, col: i32) -> i32; + * fn dimensions() -> Vec<i32>; + * }; + */ + +impl Solution { + pub fn left_most_column_with_one(binaryMatrix: &BinaryMatrix) -> i32 { + let e = binaryMatrix.dimensions(); + let m = e[0] as usize; + let n = e[1] as usize; + let mut ans = n; + + for i in 0..m { + let (mut l, mut r) = (0, n); + while l < r { + let mid = (l + r) / 2; + if binaryMatrix.get(i as i32, mid as i32) == 1 { + r = mid; + } else { + l = mid + 1; + } + } + ans = ans.min(l); + } + + if ans >= n { + -1 + } else { + ans as i32 + } + } +} +``` + +### **C#** + +```cs +/** + * // This is BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * class BinaryMatrix { + * public int Get(int row, int col) {} + * public IList<int> Dimensions() {} + * } + */ + +class Solution { + public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix) { + var e = binaryMatrix.Dimensions(); + int m = e[0], n = e[1]; + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.Get(i, mid) == 1) { + r = mid; + } else { + l = mid + 1; + } + } + ans = Math.Min(ans, l); + } + return ans >= n ? -1 : ans; + } } ``` diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md b/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md index 455684b9cdafa..90c948c2b0132 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md @@ -54,7 +54,11 @@ ## Solutions -Binary search. +**Solution 1: Binary Search** + +First, we call `BinaryMatrix.dimensions()` to get the number of rows $m$ and columns $n$ of the matrix. Then for each row, we use binary search to find the column number $j$ where the leftmost $1$ is located. The smallest $j$ value that satisfies all rows is the answer. If there is no such column, return $-1$. + +The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. We need to traverse each row, and use binary search within each row, which has a time complexity of $O(\log n)$. The space complexity is $O(1)$. <!-- tabs:start --> @@ -71,23 +75,13 @@ Binary search. class Solution: - def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int: - rows, cols = binaryMatrix.dimensions() - res = -1 - for row in range(rows): - left, right = 0, cols - 1 - while left < right: - mid = (left + right) >> 1 - if binaryMatrix.get(row, mid) == 1: - right = mid - else: - left = mid + 1 - if binaryMatrix.get(row, left) == 1: - if res == -1: - res = left - else: - res = min(res, left) - return res + def leftMostColumnWithOne(self, binaryMatrix: "BinaryMatrix") -> int: + m, n = binaryMatrix.dimensions() + ans = n + for i in range(m): + j = bisect_left(range(n), 1, key=lambda k: binaryMatrix.get(i, k)) + ans = min(ans, j) + return -1 if ans >= n else ans ``` ### **Java** @@ -104,28 +98,22 @@ class Solution: class Solution { public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { - List<Integer> scale = binaryMatrix.dimensions(); - int rows = scale.get(0), cols = scale.get(1); - int res = -1; - for (int row = 0; row < rows; ++row) { - int left = 0, right = cols - 1; - while (left < right) { - int mid = (left + right) >> 1; - if (binaryMatrix.get(row, mid) == 1) { - right = mid; - } else { - left = mid + 1; - } - } - if (binaryMatrix.get(row, left) == 1) { - if (res == -1) { - res = left; + List<Integer> e = binaryMatrix.dimensions(); + int m = e.get(0), n = e.get(1); + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid) == 1) { + r = mid; } else { - res = Math.min(res, left); + l = mid + 1; } } + ans = Math.min(ans, l); } - return res; + return ans >= n ? -1 : ans; } } ``` @@ -145,29 +133,23 @@ class Solution { class Solution { public: - int leftMostColumnWithOne(BinaryMatrix& binaryMatrix) { - vector<int> scale = binaryMatrix.dimensions(); - int rows = scale[0], cols = scale[1]; - int res = -1; - for (int row = 0; row < rows; ++row) { - int left = 0, right = cols - 1; - while (left < right) { - int mid = left + right >> 1; - if (binaryMatrix.get(row, mid) == 1) { - right = mid; - } else { - left = mid + 1; - } - } - if (binaryMatrix.get(row, left) == 1) { - if (res == -1) { - res = left; + int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { + auto e = binaryMatrix.dimensions(); + int m = e[0], n = e[1]; + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid)) { + r = mid; } else { - res = min(res, left); + l = mid + 1; } } + ans = min(ans, l); } - return res; + return ans >= n ? -1 : ans; } }; ``` @@ -185,28 +167,133 @@ public: */ func leftMostColumnWithOne(binaryMatrix BinaryMatrix) int { - scale := binaryMatrix.Dimensions() - rows, cols := scale[0], scale[1] - res := -1 - for row := 0; row < rows; row++ { - left, right := 0, cols-1 - for left < right { - mid := (left + right) >> 1 - if binaryMatrix.Get(row, mid) == 1 { - right = mid + e := binaryMatrix.Dimensions() + m, n := e[0], e[1] + ans := n + for i := 0; i < m; i++ { + l, r := 0, n + for l < r { + mid := (l + r) >> 1 + if binaryMatrix.Get(i, mid) == 1 { + r = mid } else { - left = mid + 1 - } - } - if binaryMatrix.Get(row, left) == 1 { - if res == -1 { - res = left - } else { - res = min(res, left) + l = mid + 1 } } + ans = min(ans, l) + } + if ans >= n { + return -1 } - return res + return ans +} +``` + +### **TypeScript** + +```ts +/** + * // This is the BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * class BinaryMatrix { + * get(row: number, col: number): number {} + * + * dimensions(): number[] {} + * } + */ + +function leftMostColumnWithOne(binaryMatrix: BinaryMatrix) { + const [m, n] = binaryMatrix.dimensions(); + let ans = n; + for (let i = 0; i < m; ++i) { + let [l, r] = [0, n]; + while (l < r) { + const mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid) === 1) { + r = mid; + } else { + l = mid + 1; + } + } + ans = Math.min(ans, l); + } + return ans >= n ? -1 : ans; +} +``` + +### **Rust** + +```rust +/** + * // This is the BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * struct BinaryMatrix; + * impl BinaryMatrix { + * fn get(row: i32, col: i32) -> i32; + * fn dimensions() -> Vec<i32>; + * }; + */ + +impl Solution { + pub fn left_most_column_with_one(binaryMatrix: &BinaryMatrix) -> i32 { + let e = binaryMatrix.dimensions(); + let m = e[0] as usize; + let n = e[1] as usize; + let mut ans = n; + + for i in 0..m { + let (mut l, mut r) = (0, n); + while l < r { + let mid = (l + r) / 2; + if binaryMatrix.get(i as i32, mid as i32) == 1 { + r = mid; + } else { + l = mid + 1; + } + } + ans = ans.min(l); + } + + if ans >= n { + -1 + } else { + ans as i32 + } + } +} +``` + +### **C#** + +```cs +/** + * // This is BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * class BinaryMatrix { + * public int Get(int row, int col) {} + * public IList<int> Dimensions() {} + * } + */ + +class Solution { + public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix) { + var e = binaryMatrix.Dimensions(); + int m = e[0], n = e[1]; + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.Get(i, mid) == 1) { + r = mid; + } else { + l = mid + 1; + } + } + ans = Math.Min(ans, l); + } + return ans >= n ? -1 : ans; + } } ``` diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp index feffac2a65305..0dbb6e1b3b280 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp @@ -1,37 +1,31 @@ -/** - * // This is the BinaryMatrix's API interface. - * // You should not implement it, or speculate about its implementation - * class BinaryMatrix { - * public: - * int get(int row, int col); - * vector<int> dimensions(); - * }; - */ - -class Solution { -public: - int leftMostColumnWithOne(BinaryMatrix& binaryMatrix) { - vector<int> scale = binaryMatrix.dimensions(); - int rows = scale[0], cols = scale[1]; - int res = -1; - for (int row = 0; row < rows; ++row) { - int left = 0, right = cols - 1; - while (left < right) { - int mid = left + right >> 1; - if (binaryMatrix.get(row, mid) == 1) { - right = mid; - } else { - left = mid + 1; - } - } - if (binaryMatrix.get(row, left) == 1) { - if (res == -1) { - res = left; - } else { - res = min(res, left); - } - } - } - return res; - } +/** + * // This is the BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * class BinaryMatrix { + * public: + * int get(int row, int col); + * vector<int> dimensions(); + * }; + */ + +class Solution { +public: + int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { + auto e = binaryMatrix.dimensions(); + int m = e[0], n = e[1]; + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid)) { + r = mid; + } else { + l = mid + 1; + } + } + ans = min(ans, l); + } + return ans >= n ? -1 : ans; + } }; \ No newline at end of file diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cs b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cs new file mode 100644 index 0000000000000..51ce96c39375d --- /dev/null +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cs @@ -0,0 +1,29 @@ +/** + * // This is BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * class BinaryMatrix { + * public int Get(int row, int col) {} + * public IList<int> Dimensions() {} + * } + */ + +class Solution { + public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix) { + var e = binaryMatrix.Dimensions(); + int m = e[0], n = e[1]; + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.Get(i, mid) == 1) { + r = mid; + } else { + l = mid + 1; + } + } + ans = Math.Min(ans, l); + } + return ans >= n ? -1 : ans; + } +} \ No newline at end of file diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.go b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.go index 826d8a4c9ed03..79cfb031c4156 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.go +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.go @@ -8,26 +8,23 @@ */ func leftMostColumnWithOne(binaryMatrix BinaryMatrix) int { - scale := binaryMatrix.Dimensions() - rows, cols := scale[0], scale[1] - res := -1 - for row := 0; row < rows; row++ { - left, right := 0, cols-1 - for left < right { - mid := (left + right) >> 1 - if binaryMatrix.Get(row, mid) == 1 { - right = mid + e := binaryMatrix.Dimensions() + m, n := e[0], e[1] + ans := n + for i := 0; i < m; i++ { + l, r := 0, n + for l < r { + mid := (l + r) >> 1 + if binaryMatrix.Get(i, mid) == 1 { + r = mid } else { - left = mid + 1 - } - } - if binaryMatrix.Get(row, left) == 1 { - if res == -1 { - res = left - } else { - res = min(res, left) + l = mid + 1 } } + ans = min(ans, l) + } + if ans >= n { + return -1 } - return res + return ans } \ No newline at end of file diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.java b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.java index 35c35a5acc94b..812806f5916a4 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.java +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.java @@ -1,35 +1,29 @@ -/** - * // This is the BinaryMatrix's API interface. - * // You should not implement it, or speculate about its implementation - * interface BinaryMatrix { - * public int get(int row, int col) {} - * public List<Integer> dimensions {} - * }; - */ - -class Solution { - public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { - List<Integer> scale = binaryMatrix.dimensions(); - int rows = scale.get(0), cols = scale.get(1); - int res = -1; - for (int row = 0; row < rows; ++row) { - int left = 0, right = cols - 1; - while (left < right) { - int mid = (left + right) >> 1; - if (binaryMatrix.get(row, mid) == 1) { - right = mid; - } else { - left = mid + 1; - } - } - if (binaryMatrix.get(row, left) == 1) { - if (res == -1) { - res = left; - } else { - res = Math.min(res, left); - } - } - } - return res; - } +/** + * // This is the BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * interface BinaryMatrix { + * public int get(int row, int col) {} + * public List<Integer> dimensions {} + * }; + */ + +class Solution { + public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { + List<Integer> e = binaryMatrix.dimensions(); + int m = e.get(0), n = e.get(1); + int ans = n; + for (int i = 0; i < m; ++i) { + int l = 0, r = n; + while (l < r) { + int mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid) == 1) { + r = mid; + } else { + l = mid + 1; + } + } + ans = Math.min(ans, l); + } + return ans >= n ? -1 : ans; + } } \ No newline at end of file diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.py b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.py index 13543fb951eb3..50d0d102db641 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.py +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.py @@ -1,27 +1,17 @@ -# """ -# This is BinaryMatrix's API interface. -# You should not implement it, or speculate about its implementation -# """ -# class BinaryMatrix(object): -# def get(self, row: int, col: int) -> int: -# def dimensions(self) -> list[]: - - -class Solution: - def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int: - rows, cols = binaryMatrix.dimensions() - res = -1 - for row in range(rows): - left, right = 0, cols - 1 - while left < right: - mid = (left + right) >> 1 - if binaryMatrix.get(row, mid) == 1: - right = mid - else: - left = mid + 1 - if binaryMatrix.get(row, left) == 1: - if res == -1: - res = left - else: - res = min(res, left) - return res +# """ +# This is BinaryMatrix's API interface. +# You should not implement it, or speculate about its implementation +# """ +# class BinaryMatrix(object): +# def get(self, row: int, col: int) -> int: +# def dimensions(self) -> list[]: + + +class Solution: + def leftMostColumnWithOne(self, binaryMatrix: "BinaryMatrix") -> int: + m, n = binaryMatrix.dimensions() + ans = n + for i in range(m): + j = bisect_left(range(n), 1, key=lambda k: binaryMatrix.get(i, k)) + ans = min(ans, j) + return -1 if ans >= n else ans diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.rs b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.rs new file mode 100644 index 0000000000000..e00ac8144ebdd --- /dev/null +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.rs @@ -0,0 +1,37 @@ +/** + * // This is the BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * struct BinaryMatrix; + * impl BinaryMatrix { + * fn get(row: i32, col: i32) -> i32; + * fn dimensions() -> Vec<i32>; + * }; + */ + +impl Solution { + pub fn left_most_column_with_one(binaryMatrix: &BinaryMatrix) -> i32 { + let e = binaryMatrix.dimensions(); + let m = e[0] as usize; + let n = e[1] as usize; + let mut ans = n; + + for i in 0..m { + let (mut l, mut r) = (0, n); + while l < r { + let mid = (l + r) / 2; + if binaryMatrix.get(i as i32, mid as i32) == 1 { + r = mid; + } else { + l = mid + 1; + } + } + ans = ans.min(l); + } + + if ans >= n { + -1 + } else { + ans as i32 + } + } +} diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.ts b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.ts new file mode 100644 index 0000000000000..799704ec6c5bb --- /dev/null +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.ts @@ -0,0 +1,27 @@ +/** + * // This is the BinaryMatrix's API interface. + * // You should not implement it, or speculate about its implementation + * class BinaryMatrix { + * get(row: number, col: number): number {} + * + * dimensions(): number[] {} + * } + */ + +function leftMostColumnWithOne(binaryMatrix: BinaryMatrix) { + const [m, n] = binaryMatrix.dimensions(); + let ans = n; + for (let i = 0; i < m; ++i) { + let [l, r] = [0, n]; + while (l < r) { + const mid = (l + r) >> 1; + if (binaryMatrix.get(i, mid) === 1) { + r = mid; + } else { + l = mid + 1; + } + } + ans = Math.min(ans, l); + } + return ans >= n ? -1 : ans; +} From 2ccd62d50146e1b6fd75e65c817b3ef50202a701 Mon Sep 17 00:00:00 2001 From: yanglbme <szuyanglb@outlook.com> Date: Tue, 5 Dec 2023 14:08:44 +0800 Subject: [PATCH 2/3] fix: cpp code --- .../1428.Leftmost Column with at Least a One/README.md | 3 +-- .../1428.Leftmost Column with at Least a One/README_EN.md | 3 ++- .../1428.Leftmost Column with at Least a One/Solution.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/README.md b/solution/1400-1499/1428.Leftmost Column with at Least a One/README.md index 8b71b03e34679..102a52e3eabd8 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/README.md +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/README.md @@ -159,7 +159,7 @@ class Solution { class Solution { public: - int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { + int leftMostColumnWithOne(BinaryMatrix& binaryMatrix) { auto e = binaryMatrix.dimensions(); int m = e[0], n = e[1]; int ans = n; @@ -250,7 +250,6 @@ function leftMostColumnWithOne(binaryMatrix: BinaryMatrix) { ### **Rust** ```rust - /** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md b/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md index 90c948c2b0132..cac2561ab95ee 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md @@ -133,7 +133,7 @@ class Solution { class Solution { public: - int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { + int leftMostColumnWithOne(BinaryMatrix& binaryMatrix) { auto e = binaryMatrix.dimensions(); int m = e[0], n = e[1]; int ans = n; @@ -224,6 +224,7 @@ function leftMostColumnWithOne(binaryMatrix: BinaryMatrix) { ### **Rust** ```rust + /** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp index 0dbb6e1b3b280..1725dceff57dc 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp @@ -10,7 +10,7 @@ class Solution { public: - int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { + int leftMostColumnWithOne(BinaryMatrix& binaryMatrix) { auto e = binaryMatrix.dimensions(); int m = e[0], n = e[1]; int ans = n; From 24cbc2d687020e295957659ca3c5d1a409278fe5 Mon Sep 17 00:00:00 2001 From: Libin YANG <contact@yanglibin.info> Date: Tue, 5 Dec 2023 14:13:05 +0800 Subject: [PATCH 3/3] Update README_EN.md --- .../1428.Leftmost Column with at Least a One/README_EN.md | 1 - 1 file changed, 1 deletion(-) diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md b/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md index cac2561ab95ee..866588ae5104f 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/README_EN.md @@ -224,7 +224,6 @@ function leftMostColumnWithOne(binaryMatrix: BinaryMatrix) { ### **Rust** ```rust - /** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation