Skip to content

[pull] main from doocs:main #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions solution/0300-0399/0350.Intersection of Two Arrays II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
```

### **...**

```
Expand Down
25 changes: 25 additions & 0 deletions solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
```

### **...**

```
Expand Down
20 changes: 20 additions & 0 deletions solution/0300-0399/0350.Intersection of Two Arrays II/Solution.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
42 changes: 42 additions & 0 deletions solution/2600-2699/2611.Mice and Cheese/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

### **...**

```
Expand Down
Loading