diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md index 9d9097bcb1be2..0052672972760 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md @@ -199,6 +199,31 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersect($nums1, $nums2) { + $rs = []; + for ($i = 0; $i < count($nums1); $i++) { + $hashtable[$nums1[$i]] += 1; + } + for ($j = 0; $j < count($nums2); $j++) { + if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { + array_push($rs, $nums2[$j]); + $hashtable[$nums2[$j]] -= 1; + } + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md index 06f53600c244c..d29f596855805 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md @@ -188,6 +188,31 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersect($nums1, $nums2) { + $rs = []; + for ($i = 0; $i < count($nums1); $i++) { + $hashtable[$nums1[$i]] += 1; + } + for ($j = 0; $j < count($nums2); $j++) { + if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { + array_push($rs, $nums2[$j]); + $hashtable[$nums2[$j]] -= 1; + } + } + return $rs; + } +} +``` + ### **...** ``` diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php new file mode 100644 index 0000000000000..fb73f75c413f9 --- /dev/null +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php @@ -0,0 +1,20 @@ +class Solution { + /** + * @param Integer[] $nums1 + * @param Integer[] $nums2 + * @return Integer[] + */ + function intersect($nums1, $nums2) { + $rs = []; + for ($i = 0; $i < count($nums1); $i++) { + $hashtable[$nums1[$i]] += 1; + } + for ($j = 0; $j < count($nums2); $j++) { + if (isset($hashtable[$nums2[$j]]) && $hashtable[$nums2[$j]] > 0) { + array_push($rs, $nums2[$j]); + $hashtable[$nums2[$j]] -= 1; + } + } + return $rs; + } +} \ No newline at end of file diff --git a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README.md b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README.md index ef7050b29102d..a0601ed5fd62e 100644 --- a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README.md +++ b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README.md @@ -204,7 +204,7 @@ class Solution { public: int findTheLongestBalancedSubstring(string s) { int zero = 0, one = 0; - int ans = 0, n = s.size(); + int ans = 0; for (char& c : s) { if (c == '0') { if (one > 0) { @@ -287,6 +287,54 @@ func min(a, b int) int { } ``` +### **TypeScript** + +```ts +function findTheLongestBalancedSubstring(s: string): number { + const n = s.length; + let ans = 0; + const check = (i: number, j: number): boolean => { + let cnt = 0; + for (let k = i; k <= j; ++k) { + if (s[k] === '1') { + ++cnt; + } else if (cnt > 0) { + return false; + } + } + return cnt * 2 === j - i + 1; + }; + for (let i = 0; i < n; ++i) { + for (let j = i + 1; j < n; j += 2) { + if (check(i, j)) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; +} +``` + +```ts +function findTheLongestBalancedSubstring(s: string): number { + let zero = 0; + let one = 0; + let ans = 0; + for (const c of s) { + if (c === '0') { + if (one > 0) { + zero = 0; + one = 0; + } + ++zero; + } else { + ans = Math.max(ans, 2 * Math.min(zero, ++one)); + } + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md index 2fbb227073514..f0b86441a3248 100644 --- a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md +++ b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md @@ -175,7 +175,7 @@ class Solution { public: int findTheLongestBalancedSubstring(string s) { int zero = 0, one = 0; - int ans = 0, n = s.size(); + int ans = 0; for (char& c : s) { if (c == '0') { if (one > 0) { @@ -258,6 +258,54 @@ func min(a, b int) int { } ``` +### **TypeScript** + +```ts +function findTheLongestBalancedSubstring(s: string): number { + const n = s.length; + let ans = 0; + const check = (i: number, j: number): boolean => { + let cnt = 0; + for (let k = i; k <= j; ++k) { + if (s[k] === '1') { + ++cnt; + } else if (cnt > 0) { + return false; + } + } + return cnt * 2 === j - i + 1; + }; + for (let i = 0; i < n; ++i) { + for (let j = i + 1; j < n; j += 2) { + if (check(i, j)) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; +} +``` + +```ts +function findTheLongestBalancedSubstring(s: string): number { + let zero = 0; + let one = 0; + let ans = 0; + for (const c of s) { + if (c === '0') { + if (one > 0) { + zero = 0; + one = 0; + } + ++zero; + } else { + ans = Math.max(ans, 2 * Math.min(zero, ++one)); + } + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.cpp b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.cpp index 14c80f3bde080..a12d1cff4b094 100644 --- a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.cpp +++ b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: int findTheLongestBalancedSubstring(string s) { int zero = 0, one = 0; - int ans = 0, n = s.size(); + int ans = 0; for (char& c : s) { if (c == '0') { if (one > 0) { diff --git a/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.ts b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.ts new file mode 100644 index 0000000000000..c10af75d357ca --- /dev/null +++ b/solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/Solution.ts @@ -0,0 +1,17 @@ +function findTheLongestBalancedSubstring(s: string): number { + let zero = 0; + let one = 0; + let ans = 0; + for (const c of s) { + if (c === '0') { + if (one > 0) { + zero = 0; + one = 0; + } + ++zero; + } else { + ans = Math.max(ans, 2 * Math.min(zero, ++one)); + } + } + return ans; +} diff --git a/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README.md b/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README.md index 11e7ba650a158..f1bc8145a1522 100644 --- a/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README.md +++ b/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README.md @@ -154,6 +154,28 @@ func findMatrix(nums []int) (ans [][]int) { } ``` +### **TypeScript** + +```ts +function findMatrix(nums: number[]): number[][] { + const ans: number[][] = []; + const n = nums.length; + const cnt: number[] = new Array(n + 1).fill(0); + for (const x of nums) { + ++cnt[x]; + } + for (let x = 1; x <= n; ++x) { + for (let j = 0; j < cnt[x]; ++j) { + if (ans.length <= j) { + ans.push([]); + } + ans[j].push(x); + } + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README_EN.md b/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README_EN.md index 69aaf9075352e..5374832238679 100644 --- a/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README_EN.md +++ b/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/README_EN.md @@ -136,6 +136,28 @@ func findMatrix(nums []int) (ans [][]int) { } ``` +### **TypeScript** + +```ts +function findMatrix(nums: number[]): number[][] { + const ans: number[][] = []; + const n = nums.length; + const cnt: number[] = new Array(n + 1).fill(0); + for (const x of nums) { + ++cnt[x]; + } + for (let x = 1; x <= n; ++x) { + for (let j = 0; j < cnt[x]; ++j) { + if (ans.length <= j) { + ans.push([]); + } + ans[j].push(x); + } + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/Solution.ts b/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/Solution.ts new file mode 100644 index 0000000000000..7889ec4dee54a --- /dev/null +++ b/solution/2600-2699/2610.Convert an Array Into a 2D Array With Conditions/Solution.ts @@ -0,0 +1,17 @@ +function findMatrix(nums: number[]): number[][] { + const ans: number[][] = []; + const n = nums.length; + const cnt: number[] = new Array(n + 1).fill(0); + for (const x of nums) { + ++cnt[x]; + } + for (let x = 1; x <= n; ++x) { + for (let j = 0; j < cnt[x]; ++j) { + if (ans.length <= j) { + ans.push([]); + } + ans[j].push(x); + } + } + return ans; +} diff --git a/solution/2600-2699/2611.Mice and Cheese/README.md b/solution/2600-2699/2611.Mice and Cheese/README.md index 1ee3902d4abfe..a8d93a3c609fc 100644 --- a/solution/2600-2699/2611.Mice and Cheese/README.md +++ b/solution/2600-2699/2611.Mice and Cheese/README.md @@ -207,6 +207,48 @@ func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) { } ``` +### **TypeScript** + +```ts +function miceAndCheese( + reward1: number[], + reward2: number[], + k: number, +): number { + const n = reward1.length; + const idx = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => reward1[j] - reward2[j] - (reward1[i] - reward2[i])); + let ans = 0; + for (let i = 0; i < k; ++i) { + ans += reward1[idx[i]]; + } + for (let i = k; i < n; ++i) { + ans += reward2[idx[i]]; + } + return ans; +} +``` + +```ts +function miceAndCheese( + reward1: number[], + reward2: number[], + k: number, +): number { + const n = reward1.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + ans += reward2[i]; + reward1[i] -= reward2[i]; + } + reward1.sort((a, b) => b - a); + for (let i = 0; i < k; ++i) { + ans += reward1[i]; + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/2600-2699/2611.Mice and Cheese/README_EN.md b/solution/2600-2699/2611.Mice and Cheese/README_EN.md index a487eb6281372..d89549c7161c3 100644 --- a/solution/2600-2699/2611.Mice and Cheese/README_EN.md +++ b/solution/2600-2699/2611.Mice and Cheese/README_EN.md @@ -189,6 +189,48 @@ func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) { } ``` +### **TypeScript** + +```ts +function miceAndCheese( + reward1: number[], + reward2: number[], + k: number, +): number { + const n = reward1.length; + const idx = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => reward1[j] - reward2[j] - (reward1[i] - reward2[i])); + let ans = 0; + for (let i = 0; i < k; ++i) { + ans += reward1[idx[i]]; + } + for (let i = k; i < n; ++i) { + ans += reward2[idx[i]]; + } + return ans; +} +``` + +```ts +function miceAndCheese( + reward1: number[], + reward2: number[], + k: number, +): number { + const n = reward1.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + ans += reward2[i]; + reward1[i] -= reward2[i]; + } + reward1.sort((a, b) => b - a); + for (let i = 0; i < k; ++i) { + ans += reward1[i]; + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/2600-2699/2611.Mice and Cheese/Solution.ts b/solution/2600-2699/2611.Mice and Cheese/Solution.ts new file mode 100644 index 0000000000000..f8a9ab9790d87 --- /dev/null +++ b/solution/2600-2699/2611.Mice and Cheese/Solution.ts @@ -0,0 +1,17 @@ +function miceAndCheese( + reward1: number[], + reward2: number[], + k: number, +): number { + const n = reward1.length; + const idx = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => reward1[j] - reward2[j] - (reward1[i] - reward2[i])); + let ans = 0; + for (let i = 0; i < k; ++i) { + ans += reward1[idx[i]]; + } + for (let i = k; i < n; ++i) { + ans += reward2[idx[i]]; + } + return ans; +}