diff --git a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README.md b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README.md index 72969f8bf3ca5..9d33beefad25c 100644 --- a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README.md +++ b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README.md @@ -81,32 +81,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3550.Sm -### 方法一 +### 方法一:枚举 + 数位和 + +我们可以从下标 $i = 0$ 开始,遍历数组中的每个元素 $x$,计算 $x$ 的数位和 $s$。如果 $s = i$,则返回下标 $i$。如果遍历完所有元素都没有找到满足条件的下标,则返回 -1。 + +时间复杂度 $o(n)$,其中 $n$ 是数组的长度。空间复杂度 $o(1)$,只使用了常数级别的额外空间。 #### Python3 ```python - +class Solution: + def smallestIndex(self, nums: List[int]) -> int: + for i, x in enumerate(nums): + s = 0 + while x: + s += x % 10 + x //= 10 + if s == i: + return i + return -1 ``` #### Java ```java - +class Solution { + public int smallestIndex(int[] nums) { + for (int i = 0; i < nums.length; ++i) { + int s = 0; + while (nums[i] != 0) { + s += nums[i] % 10; + nums[i] /= 10; + } + if (s == i) { + return i; + } + } + return -1; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int smallestIndex(vector& nums) { + for (int i = 0; i < nums.size(); ++i) { + int s = 0; + while (nums[i]) { + s += nums[i] % 10; + nums[i] /= 10; + } + if (s == i) { + return i; + } + } + return -1; + } +}; ``` #### Go ```go +func smallestIndex(nums []int) int { + for i, x := range nums { + s := 0 + for ; x > 0; x /= 10 { + s += x % 10 + } + if s == i { + return i + } + } + return -1 +} +``` +#### TypeScript + +```ts +function smallestIndex(nums: number[]): number { + for (let i = 0; i < nums.length; ++i) { + let s = 0; + for (; nums[i] > 0; nums[i] = Math.floor(nums[i] / 10)) { + s += nums[i] % 10; + } + if (s === i) { + return i; + } + } + return -1; +} ``` diff --git a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README_EN.md b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README_EN.md index 502fb4d21eb7b..85db916220898 100644 --- a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README_EN.md +++ b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/README_EN.md @@ -79,32 +79,102 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3550.Sm -### Solution 1 +### Solution 1: Enumeration + Digit Sum + +We can start from index $i = 0$ and iterate through each element $x$ in the array, calculating the digit sum $s$ of $x$. If $s = i$, return the index $i$. If no such index is found after traversing all elements, return -1. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$, as only constant extra space is used. #### Python3 ```python - +class Solution: + def smallestIndex(self, nums: List[int]) -> int: + for i, x in enumerate(nums): + s = 0 + while x: + s += x % 10 + x //= 10 + if s == i: + return i + return -1 ``` #### Java ```java - +class Solution { + public int smallestIndex(int[] nums) { + for (int i = 0; i < nums.length; ++i) { + int s = 0; + while (nums[i] != 0) { + s += nums[i] % 10; + nums[i] /= 10; + } + if (s == i) { + return i; + } + } + return -1; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int smallestIndex(vector& nums) { + for (int i = 0; i < nums.size(); ++i) { + int s = 0; + while (nums[i]) { + s += nums[i] % 10; + nums[i] /= 10; + } + if (s == i) { + return i; + } + } + return -1; + } +}; ``` #### Go ```go +func smallestIndex(nums []int) int { + for i, x := range nums { + s := 0 + for ; x > 0; x /= 10 { + s += x % 10 + } + if s == i { + return i + } + } + return -1 +} +``` +#### TypeScript + +```ts +function smallestIndex(nums: number[]): number { + for (let i = 0; i < nums.length; ++i) { + let s = 0; + for (; nums[i] > 0; nums[i] = Math.floor(nums[i] / 10)) { + s += nums[i] % 10; + } + if (s === i) { + return i; + } + } + return -1; +} ``` diff --git a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.cpp b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.cpp new file mode 100644 index 0000000000000..5a553360e97d5 --- /dev/null +++ b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int smallestIndex(vector& nums) { + for (int i = 0; i < nums.size(); ++i) { + int s = 0; + while (nums[i]) { + s += nums[i] % 10; + nums[i] /= 10; + } + if (s == i) { + return i; + } + } + return -1; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.go b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.go new file mode 100644 index 0000000000000..bace54ba82c67 --- /dev/null +++ b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.go @@ -0,0 +1,12 @@ +func smallestIndex(nums []int) int { + for i, x := range nums { + s := 0 + for ; x > 0; x /= 10 { + s += x % 10 + } + if s == i { + return i + } + } + return -1 +} \ No newline at end of file diff --git a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.java b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.java new file mode 100644 index 0000000000000..105bd6076db8b --- /dev/null +++ b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int smallestIndex(int[] nums) { + for (int i = 0; i < nums.length; ++i) { + int s = 0; + while (nums[i] != 0) { + s += nums[i] % 10; + nums[i] /= 10; + } + if (s == i) { + return i; + } + } + return -1; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.py b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.py new file mode 100644 index 0000000000000..f66da5e83fad3 --- /dev/null +++ b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def smallestIndex(self, nums: List[int]) -> int: + for i, x in enumerate(nums): + s = 0 + while x: + s += x % 10 + x //= 10 + if s == i: + return i + return -1 diff --git a/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.ts b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.ts new file mode 100644 index 0000000000000..0e2da64a36b12 --- /dev/null +++ b/solution/3500-3599/3550.Smallest Index With Digit Sum Equal to Index/Solution.ts @@ -0,0 +1,12 @@ +function smallestIndex(nums: number[]): number { + for (let i = 0; i < nums.length; ++i) { + let s = 0; + for (; nums[i] > 0; nums[i] = Math.floor(nums[i] / 10)) { + s += nums[i] % 10; + } + if (s === i) { + return i; + } + } + return -1; +} diff --git a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/README.md b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/README.md index beec72ca78871..fc289125c5d52 100644 --- a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/README.md +++ b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/README.md @@ -93,25 +93,195 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3551.Mi #### Python3 ```python - +class Solution: + def minSwaps(self, nums: List[int]) -> int: + def f(x: int) -> int: + s = 0 + while x: + s += x % 10 + x //= 10 + return s + + n = len(nums) + arr = sorted((f(x), x) for x in nums) + d = {a[1]: i for i, a in enumerate(arr)} + ans = n + vis = [False] * n + for i in range(n): + if not vis[i]: + ans -= 1 + j = i + while not vis[j]: + vis[j] = True + j = d[nums[j]] + return ans ``` #### Java ```java - +class Solution { + public int minSwaps(int[] nums) { + int n = nums.length; + int[][] arr = new int[n][2]; + for (int i = 0; i < n; i++) { + arr[i][0] = f(nums[i]); + arr[i][1] = nums[i]; + } + Arrays.sort(arr, (a, b) -> { + if (a[0] != b[0]) return Integer.compare(a[0], b[0]); + return Integer.compare(a[1], b[1]); + }); + Map d = new HashMap<>(); + for (int i = 0; i < n; i++) { + d.put(arr[i][1], i); + } + boolean[] vis = new boolean[n]; + int ans = n; + for (int i = 0; i < n; i++) { + if (!vis[i]) { + ans--; + int j = i; + while (!vis[j]) { + vis[j] = true; + j = d.get(nums[j]); + } + } + } + return ans; + } + + private int f(int x) { + int s = 0; + while (x != 0) { + s += x % 10; + x /= 10; + } + return s; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int f(int x) { + int s = 0; + while (x) { + s += x % 10; + x /= 10; + } + return s; + } + + int minSwaps(vector& nums) { + int n = nums.size(); + vector> arr(n); + for (int i = 0; i < n; ++i) arr[i] = {f(nums[i]), nums[i]}; + sort(arr.begin(), arr.end()); + unordered_map d; + for (int i = 0; i < n; ++i) d[arr[i].second] = i; + vector vis(n, 0); + int ans = n; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + --ans; + int j = i; + while (!vis[j]) { + vis[j] = 1; + j = d[nums[j]]; + } + } + } + return ans; + } +}; ``` #### Go ```go +func minSwaps(nums []int) int { + n := len(nums) + arr := make([][2]int, n) + for i := 0; i < n; i++ { + arr[i][0] = f(nums[i]) + arr[i][1] = nums[i] + } + sort.Slice(arr, func(i, j int) bool { + if arr[i][0] != arr[j][0] { + return arr[i][0] < arr[j][0] + } + return arr[i][1] < arr[j][1] + }) + d := make(map[int]int, n) + for i := 0; i < n; i++ { + d[arr[i][1]] = i + } + vis := make([]bool, n) + ans := n + for i := 0; i < n; i++ { + if !vis[i] { + ans-- + j := i + for !vis[j] { + vis[j] = true + j = d[nums[j]] + } + } + } + return ans +} + +func f(x int) int { + s := 0 + for x != 0 { + s += x % 10 + x /= 10 + } + return s +} +``` +#### TypeScript + +```ts +function f(x: number): number { + let s = 0; + while (x !== 0) { + s += x % 10; + x = Math.floor(x / 10); + } + return s; +} + +function minSwaps(nums: number[]): number { + const n = nums.length; + const arr: [number, number][] = new Array(n); + for (let i = 0; i < n; i++) { + arr[i] = [f(nums[i]), nums[i]]; + } + arr.sort((a, b) => (a[0] !== b[0] ? a[0] - b[0] : a[1] - b[1])); + const d = new Map(); + for (let i = 0; i < n; i++) { + d.set(arr[i][1], i); + } + const vis: boolean[] = new Array(n).fill(false); + let ans = n; + for (let i = 0; i < n; i++) { + if (!vis[i]) { + ans--; + let j = i; + while (!vis[j]) { + vis[j] = true; + j = d.get(nums[j])!; + } + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/README_EN.md b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/README_EN.md index 3eec1746d4a62..d7ccfc995cde9 100644 --- a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/README_EN.md +++ b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/README_EN.md @@ -91,25 +91,195 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3551.Mi #### Python3 ```python - +class Solution: + def minSwaps(self, nums: List[int]) -> int: + def f(x: int) -> int: + s = 0 + while x: + s += x % 10 + x //= 10 + return s + + n = len(nums) + arr = sorted((f(x), x) for x in nums) + d = {a[1]: i for i, a in enumerate(arr)} + ans = n + vis = [False] * n + for i in range(n): + if not vis[i]: + ans -= 1 + j = i + while not vis[j]: + vis[j] = True + j = d[nums[j]] + return ans ``` #### Java ```java - +class Solution { + public int minSwaps(int[] nums) { + int n = nums.length; + int[][] arr = new int[n][2]; + for (int i = 0; i < n; i++) { + arr[i][0] = f(nums[i]); + arr[i][1] = nums[i]; + } + Arrays.sort(arr, (a, b) -> { + if (a[0] != b[0]) return Integer.compare(a[0], b[0]); + return Integer.compare(a[1], b[1]); + }); + Map d = new HashMap<>(); + for (int i = 0; i < n; i++) { + d.put(arr[i][1], i); + } + boolean[] vis = new boolean[n]; + int ans = n; + for (int i = 0; i < n; i++) { + if (!vis[i]) { + ans--; + int j = i; + while (!vis[j]) { + vis[j] = true; + j = d.get(nums[j]); + } + } + } + return ans; + } + + private int f(int x) { + int s = 0; + while (x != 0) { + s += x % 10; + x /= 10; + } + return s; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int f(int x) { + int s = 0; + while (x) { + s += x % 10; + x /= 10; + } + return s; + } + + int minSwaps(vector& nums) { + int n = nums.size(); + vector> arr(n); + for (int i = 0; i < n; ++i) arr[i] = {f(nums[i]), nums[i]}; + sort(arr.begin(), arr.end()); + unordered_map d; + for (int i = 0; i < n; ++i) d[arr[i].second] = i; + vector vis(n, 0); + int ans = n; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + --ans; + int j = i; + while (!vis[j]) { + vis[j] = 1; + j = d[nums[j]]; + } + } + } + return ans; + } +}; ``` #### Go ```go +func minSwaps(nums []int) int { + n := len(nums) + arr := make([][2]int, n) + for i := 0; i < n; i++ { + arr[i][0] = f(nums[i]) + arr[i][1] = nums[i] + } + sort.Slice(arr, func(i, j int) bool { + if arr[i][0] != arr[j][0] { + return arr[i][0] < arr[j][0] + } + return arr[i][1] < arr[j][1] + }) + d := make(map[int]int, n) + for i := 0; i < n; i++ { + d[arr[i][1]] = i + } + vis := make([]bool, n) + ans := n + for i := 0; i < n; i++ { + if !vis[i] { + ans-- + j := i + for !vis[j] { + vis[j] = true + j = d[nums[j]] + } + } + } + return ans +} + +func f(x int) int { + s := 0 + for x != 0 { + s += x % 10 + x /= 10 + } + return s +} +``` +#### TypeScript + +```ts +function f(x: number): number { + let s = 0; + while (x !== 0) { + s += x % 10; + x = Math.floor(x / 10); + } + return s; +} + +function minSwaps(nums: number[]): number { + const n = nums.length; + const arr: [number, number][] = new Array(n); + for (let i = 0; i < n; i++) { + arr[i] = [f(nums[i]), nums[i]]; + } + arr.sort((a, b) => (a[0] !== b[0] ? a[0] - b[0] : a[1] - b[1])); + const d = new Map(); + for (let i = 0; i < n; i++) { + d.set(arr[i][1], i); + } + const vis: boolean[] = new Array(n).fill(false); + let ans = n; + for (let i = 0; i < n; i++) { + if (!vis[i]) { + ans--; + let j = i; + while (!vis[j]) { + vis[j] = true; + j = d.get(nums[j])!; + } + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.cpp b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.cpp new file mode 100644 index 0000000000000..0b7ad5548214b --- /dev/null +++ b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int f(int x) { + int s = 0; + while (x) { + s += x % 10; + x /= 10; + } + return s; + } + + int minSwaps(vector& nums) { + int n = nums.size(); + vector> arr(n); + for (int i = 0; i < n; ++i) arr[i] = {f(nums[i]), nums[i]}; + sort(arr.begin(), arr.end()); + unordered_map d; + for (int i = 0; i < n; ++i) d[arr[i].second] = i; + vector vis(n, 0); + int ans = n; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + --ans; + int j = i; + while (!vis[j]) { + vis[j] = 1; + j = d[nums[j]]; + } + } + } + return ans; + } +}; diff --git a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.go b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.go new file mode 100644 index 0000000000000..eae0ce2cd8391 --- /dev/null +++ b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.go @@ -0,0 +1,40 @@ +func minSwaps(nums []int) int { + n := len(nums) + arr := make([][2]int, n) + for i := 0; i < n; i++ { + arr[i][0] = f(nums[i]) + arr[i][1] = nums[i] + } + sort.Slice(arr, func(i, j int) bool { + if arr[i][0] != arr[j][0] { + return arr[i][0] < arr[j][0] + } + return arr[i][1] < arr[j][1] + }) + d := make(map[int]int, n) + for i := 0; i < n; i++ { + d[arr[i][1]] = i + } + vis := make([]bool, n) + ans := n + for i := 0; i < n; i++ { + if !vis[i] { + ans-- + j := i + for !vis[j] { + vis[j] = true + j = d[nums[j]] + } + } + } + return ans +} + +func f(x int) int { + s := 0 + for x != 0 { + s += x % 10 + x /= 10 + } + return s +} \ No newline at end of file diff --git a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.java b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.java new file mode 100644 index 0000000000000..1b525e61e8534 --- /dev/null +++ b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.java @@ -0,0 +1,40 @@ +class Solution { + public int minSwaps(int[] nums) { + int n = nums.length; + int[][] arr = new int[n][2]; + for (int i = 0; i < n; i++) { + arr[i][0] = f(nums[i]); + arr[i][1] = nums[i]; + } + Arrays.sort(arr, (a, b) -> { + if (a[0] != b[0]) return Integer.compare(a[0], b[0]); + return Integer.compare(a[1], b[1]); + }); + Map d = new HashMap<>(); + for (int i = 0; i < n; i++) { + d.put(arr[i][1], i); + } + boolean[] vis = new boolean[n]; + int ans = n; + for (int i = 0; i < n; i++) { + if (!vis[i]) { + ans--; + int j = i; + while (!vis[j]) { + vis[j] = true; + j = d.get(nums[j]); + } + } + } + return ans; + } + + private int f(int x) { + int s = 0; + while (x != 0) { + s += x % 10; + x /= 10; + } + return s; + } +} diff --git a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.py b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.py new file mode 100644 index 0000000000000..7902af6fe0cca --- /dev/null +++ b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.py @@ -0,0 +1,22 @@ +class Solution: + def minSwaps(self, nums: List[int]) -> int: + def f(x: int) -> int: + s = 0 + while x: + s += x % 10 + x //= 10 + return s + + n = len(nums) + arr = sorted((f(x), x) for x in nums) + d = {a[1]: i for i, a in enumerate(arr)} + ans = n + vis = [False] * n + for i in range(n): + if not vis[i]: + ans -= 1 + j = i + while not vis[j]: + vis[j] = True + j = d[nums[j]] + return ans diff --git a/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.ts b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.ts new file mode 100644 index 0000000000000..ef713df1a667a --- /dev/null +++ b/solution/3500-3599/3551.Minimum Swaps to Sort by Digit Sum/Solution.ts @@ -0,0 +1,34 @@ +function f(x: number): number { + let s = 0; + while (x !== 0) { + s += x % 10; + x = Math.floor(x / 10); + } + return s; +} + +function minSwaps(nums: number[]): number { + const n = nums.length; + const arr: [number, number][] = new Array(n); + for (let i = 0; i < n; i++) { + arr[i] = [f(nums[i]), nums[i]]; + } + arr.sort((a, b) => (a[0] !== b[0] ? a[0] - b[0] : a[1] - b[1])); + const d = new Map(); + for (let i = 0; i < n; i++) { + d.set(arr[i][1], i); + } + const vis: boolean[] = new Array(n).fill(false); + let ans = n; + for (let i = 0; i < n; i++) { + if (!vis[i]) { + ans--; + let j = i; + while (!vis[j]) { + vis[j] = true; + j = d.get(nums[j])!; + } + } + } + return ans; +}