Skip to content
Open
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
56 changes: 56 additions & 0 deletions articles/brick-wall.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,41 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[][]} wall
* @return {number}
*/
leastBricks(wall: number[][]): number {
const n = wall.length;
let m = 0;
for (const brick of wall[0]) {
m += brick;
}
const gaps: number[][] = Array.from({ length: n }, () => []);
for (let i = 0; i < n; i++) {
let gap = 0;
for (const brick of wall[i]) {
gap += brick;
gaps[i].push(gap);
}
}
let res = n;
for (let line = 1; line < m; line++) {
let cuts = 0;
for (let i = 0; i < n; i++) {
if (!gaps[i].includes(line)) {
cuts++;
}
}
res = Math.min(res, cuts);
}
return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -551,6 +586,27 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[][]} wall
* @return {number}
*/
leastBricks(wall: number[][]): number {
const countGap = new Map<number, number>();
countGap.set(0, 0);
for (const row of wall) {
let total = 0;
for (let i = 0; i < row.length - 1; i++) {
total += row[i];
countGap.set(total, (countGap.get(total) || 0) + 1);
}
}
return wall.length - Math.max(...countGap.values());
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
25 changes: 25 additions & 0 deletions articles/make-sum-divisible-by-p.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,31 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[]} nums
* @param {number} p
* @return {number}
*/
minSubarray(nums: number[], p: number): number {
const n = nums.length;
let totSum = nums.reduce((a, b) => a + b, 0);
if (totSum % p === 0) return 0;
for (let l = 1; l < n; l++) {
let curSum = 0;
for (let i = 0; i < n; i++) {
curSum += nums[i];
if (i >= l) curSum -= nums[i - l];
const remainSum = totSum - curSum;
if (remainSum % p === 0) return l;
}
}
return -1;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
91 changes: 91 additions & 0 deletions articles/minimum-index-of-a-valid-split.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,37 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[]} nums
* @return {number}
*/
minimumIndex(nums: number[]): number {
const n = nums.length;
for (let i = 0; i < n - 1; i++) {
const leftCnt: Record<number, number> = {};
for (let l = 0; l <= i; l++) {
leftCnt[nums[l]] = (leftCnt[nums[l]] || 0) + 1;
}
const rightCnt: Record<number, number> = {};
for (let r = i + 1; r < n; r++) {
rightCnt[nums[r]] = (rightCnt[nums[r]] || 0) + 1;
}
for (const num in leftCnt) {
if (
leftCnt[num] > Math.floor((i + 1) / 2) &&
(rightCnt[num] || 0) > Math.floor((n - i - 1) / 2)
) {
return i;
}
}
}
return -1;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -565,6 +596,34 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[]} nums
* @return {number}
*/
minimumIndex(nums: number[]): number {
const left: Record<number, number> = {};
const right: Record<number, number> = {};
const n = nums.length;
for (const num of nums) {
right[num] = (right[num] || 0) + 1;
}
for (let i = 0; i < n; i++) {
const num = nums[i];
left[num] = (left[num] || 0) + 1;
right[num] -= 1;
const leftLen = i + 1;
const rightLen = n - i - 1;
if (2 * left[num] > leftLen && 2 * right[num] > rightLen) {
return i;
}
}
return -1;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -894,6 +953,38 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[]} nums
* @return {number}
*/
minimumIndex(nums: number[]): number {
let majority = 0,
count = 0;
for (const num of nums) {
if (count === 0) majority = num;
count += num === majority ? 1 : -1;
}
let leftCnt = 0;
let rightCnt = nums.filter((x) => x === majority).length;
const n = nums.length;
for (let i = 0; i < n; i++) {
if (nums[i] === majority) {
leftCnt++;
rightCnt--;
}
const leftLen = i + 1;
const rightLen = n - i - 1;
if (2 * leftCnt > leftLen && 2 * rightCnt > rightLen) {
return i;
}
}
return -1;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,27 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {string} boxes
* @return {number[]}
*/
minOperations(boxes: string): number[] {
const n = boxes.length;
const res: number[] = new Array(n).fill(0);
for (let pos = 0; pos < n; pos++) {
for (let i = 0; i < n; i++) {
if (boxes[i] === '1') {
res[pos] += Math.abs(pos - i);
}
}
}
return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -477,6 +498,33 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {string} boxes
* @return {number[]}
*/
minOperations(boxes: string): number[] {
const n = boxes.length;
const res: number[] = new Array(n).fill(0);
const prefixCount: number[] = new Array(n + 1).fill(0);
const indexSum: number[] = new Array(n + 1).fill(0);
for (let i = 0; i < n; i++) {
prefixCount[i + 1] = prefixCount[i] + (boxes[i] === '1' ? 1 : 0);
indexSum[i + 1] = indexSum[i] + (boxes[i] === '1' ? i : 0);
}
for (let i = 0; i < n; i++) {
const left = prefixCount[i];
const leftSum = indexSum[i];
const right = prefixCount[n] - prefixCount[i + 1];
const rightSum = indexSum[n] - indexSum[i + 1];
res[i] = i * left - leftSum + (rightSum - i * right);
}
return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -736,6 +784,33 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {string} boxes
* @return {number[]}
*/
minOperations(boxes: string): number[] {
const n = boxes.length;
const res: number[] = new Array(n).fill(0);
let balls = 0,
moves = 0;
for (let i = 0; i < n; i++) {
res[i] = balls + moves;
moves += balls;
balls += Number(boxes[i]);
}
balls = moves = 0;
for (let i = n - 1; i >= 0; i--) {
res[i] += balls + moves;
moves += balls;
balls += Number(boxes[i]);
}
return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
56 changes: 56 additions & 0 deletions articles/minimum-number-of-swaps-to-make-the-string-balanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {string} s
* @return {number}
*/
minSwaps(s: string): number {
const stack: string[] = [];
for (const c of s) {
if (c === '[') {
stack.push(c);
} else if (stack.length > 0) {
stack.pop();
}
}
return Math.floor((stack.length + 1) / 2);
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -343,6 +363,25 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {string} s
* @return {number}
*/
minSwaps(s: string): number {
let close = 0,
maxClose = 0;
for (let i = 0; i < s.length; i++) {
if (s.charAt(i) == '[') close--;
else close++;
maxClose = Math.max(maxClose, close);
}
return Math.floor((maxClose + 1) / 2);
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -498,6 +537,23 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {string} s
* @return {number}
*/
minSwaps(s: string): number {
let stackSize = 0;
for (let i = 0; i < s.length; i++) {
if (s.charAt(i) == '[') stackSize++;
else if (stackSize > 0) stackSize--;
}
return Math.floor((stackSize + 1) / 2);
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
Loading