Skip to content

[pull] main from doocs:main #214

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 5 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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

667 changes: 667 additions & 0 deletions solution/0200-0299/0220.Contains Duplicate III/README.md

Large diffs are not rendered by default.

667 changes: 667 additions & 0 deletions solution/0200-0299/0220.Contains Duplicate III/README_EN.md

Large diffs are not rendered by default.

662 changes: 662 additions & 0 deletions solution/0200-0299/0220.Contains Duplicate III/Solution.ts

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions solution/0200-0299/0229.Majority Element II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ public class Solution {
}
```

### **PHP**

```php
class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
function majorityElement($nums) {
$rs = [];
$n = count($nums);
for ($i = 0; $i < $n; $i++) {
$hashmap[$nums[$i]] += 1;
if ($hashmap[$nums[$i]] > $n / 3)
array_push($rs, $nums[$i]);
}
return array_unique($rs);
}
}
```

### **...**

```
Expand Down
21 changes: 21 additions & 0 deletions solution/0200-0299/0229.Majority Element II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ public class Solution {
}
```

### **PHP**

```php
class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
function majorityElement($nums) {
$rs = [];
$n = count($nums);
for ($i = 0; $i < $n; $i++) {
$hashmap[$nums[$i]] += 1;
if ($hashmap[$nums[$i]] > $n / 3)
array_push($rs, $nums[$i]);
}
return array_unique($rs);
}
}
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions solution/0200-0299/0229.Majority Element II/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
function majorityElement($nums) {
$rs = [];
$n = count($nums);
for ($i = 0; $i < $n; $i++) {
$hashmap[$nums[$i]] += 1;
if ($hashmap[$nums[$i]] > $n / 3)
array_push($rs, $nums[$i]);
}
return array_unique($rs);
}
}
52 changes: 52 additions & 0 deletions solution/2600-2699/2604.Minimum Time to Eat All Grains/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,58 @@ func abs(x int) int {
}
```

### **TypeScript**

```ts
function minimumTime(hens: number[], grains: number[]): number {
hens.sort((a, b) => a - b);
grains.sort((a, b) => a - b);
const m = grains.length;
let l = 0;
let r = Math.abs(hens[0] - grains[0]) + grains[m - 1] - grains[0] + 1;

const check = (t: number): boolean => {
let j = 0;
for (const x of hens) {
if (j === m) {
return true;
}
const y = grains[j];
if (y <= x) {
const d = x - y;
if (d > t) {
return false;
}
while (j < m && grains[j] <= x) {
++j;
}
while (
j < m &&
Math.min(d, grains[j] - x) + grains[j] - y <= t
) {
++j;
}
} else {
while (j < m && grains[j] - x <= t) {
++j;
}
}
}
return j === m;
};

while (l < r) {
const mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,58 @@ func abs(x int) int {
}
```

### **TypeScript**

```ts
function minimumTime(hens: number[], grains: number[]): number {
hens.sort((a, b) => a - b);
grains.sort((a, b) => a - b);
const m = grains.length;
let l = 0;
let r = Math.abs(hens[0] - grains[0]) + grains[m - 1] - grains[0] + 1;

const check = (t: number): boolean => {
let j = 0;
for (const x of hens) {
if (j === m) {
return true;
}
const y = grains[j];
if (y <= x) {
const d = x - y;
if (d > t) {
return false;
}
while (j < m && grains[j] <= x) {
++j;
}
while (
j < m &&
Math.min(d, grains[j] - x) + grains[j] - y <= t
) {
++j;
}
} else {
while (j < m && grains[j] - x <= t) {
++j;
}
}
}
return j === m;
};

while (l < r) {
const mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
```

### **...**

```
Expand Down
47 changes: 47 additions & 0 deletions solution/2600-2699/2604.Minimum Time to Eat All Grains/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
function minimumTime(hens: number[], grains: number[]): number {
hens.sort((a, b) => a - b);
grains.sort((a, b) => a - b);
const m = grains.length;
let l = 0;
let r = Math.abs(hens[0] - grains[0]) + grains[m - 1] - grains[0] + 1;

const check = (t: number): boolean => {
let j = 0;
for (const x of hens) {
if (j === m) {
return true;
}
const y = grains[j];
if (y <= x) {
const d = x - y;
if (d > t) {
return false;
}
while (j < m && grains[j] <= x) {
++j;
}
while (
j < m &&
Math.min(d, grains[j] - x) + grains[j] - y <= t
) {
++j;
}
} else {
while (j < m && grains[j] - x <= t) {
++j;
}
}
}
return j === m;
};

while (l < r) {
const mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,100 @@ func min(a, b int) int {
}
```

### **TypeScript**

```ts
function minNumber(nums1: number[], nums2: number[]): number {
let ans = 100;
for (const a of nums1) {
for (const b of nums2) {
if (a == b) {
ans = Math.min(ans, a);
} else {
ans = Math.min(ans, a * 10 + b, b * 10 + a);
}
}
}
return ans;
}
```

```ts
function minNumber(nums1: number[], nums2: number[]): number {
const s1: boolean[] = new Array(10).fill(false);
const s2: boolean[] = new Array(10).fill(false);
for (const x of nums1) {
s1[x] = true;
}
for (const x of nums2) {
s2[x] = true;
}
let a = 0;
let b = 0;
for (let i = 1; i < 10; ++i) {
if (s1[i] && s2[i]) {
return i;
}
if (a == 0 && s1[i]) {
a = i;
}
if (b == 0 && s2[i]) {
b = i;
}
}
return Math.min(a * 10 + b, b * 10 + a);
}
```

```ts
function minNumber(nums1: number[], nums2: number[]): number {
let mask1: number = 0;
let mask2: number = 0;
for (const x of nums1) {
mask1 |= 1 << x;
}
for (const x of nums2) {
mask2 |= 1 << x;
}
const mask = mask1 & mask2;
if (mask !== 0) {
return numberOfTrailingZeros(mask);
}
const a = numberOfTrailingZeros(mask1);
const b = numberOfTrailingZeros(mask2);
return Math.min(a * 10 + b, b * 10 + a);
}

function numberOfTrailingZeros(i: number): number {
let y = 0;
if (i === 0) {
return 32;
}
let n = 31;
y = i << 16;
if (y != 0) {
n = n - 16;
i = y;
}
y = i << 8;
if (y != 0) {
n = n - 8;
i = y;
}
y = i << 4;
if (y != 0) {
n = n - 4;
i = y;
}
y = i << 2;
if (y != 0) {
n = n - 2;
i = y;
}
return n - ((i << 1) >>> 31);
}
```

### **...**

```
Expand Down
Loading