Skip to content

Commit 8ad399a

Browse files
committed
Updated Project format
1 parent e15565b commit 8ad399a

File tree

8,988 files changed

+237396
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

8,988 files changed

+237396
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
bool isValid(vector<vector<int>>& grid,int r,int c,int nr,int nc,int m,int n){
3+
if(nr>=0 && nc>=0 && nr<m && nc<n && grid[nr][nc]==-1){
4+
if(grid[r][c]==0)
5+
grid[nr][nc]=1;
6+
else
7+
grid[nr][nc]=grid[r][c]+1;
8+
return 1;
9+
}
10+
return 0;
11+
}
12+
public:
13+
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
14+
queue<pair<int,int>> q;
15+
int m = mat.size();
16+
int n = mat[0].size();
17+
for(int i=0;i<m;i++){
18+
for(int j=0;j<n;j++){
19+
if(mat[i][j]==0)
20+
q.push({i,j});
21+
else
22+
mat[i][j]=-1;
23+
}
24+
}
25+
while(!q.empty()){
26+
auto per = q.front();
27+
int r = per.first;
28+
int c = per.second;
29+
// if()
30+
q.pop();
31+
if(isValid(mat,r,c,r-1,c,m,n)){
32+
q.push({r-1,c});
33+
}
34+
if(isValid(mat,r,c,r+1,c,m,n)){
35+
q.push({r+1,c});
36+
}
37+
if(isValid(mat,r,c,r,c-1,m,n)){
38+
q.push({r,c-1});
39+
}
40+
if(isValid(mat,r,c,r,c+1,m,n)){
41+
q.push({r,c+1});
42+
}
43+
44+
}
45+
return mat;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
// BFS
3+
// We add all 0 to the queue in the 0th level of the BFS. From there, every subsequent pair of indexes added would be 1 in the mat. THis way levels can represent the distance of a one from the closest 0 to it.
4+
boolean visited[][];
5+
6+
// Could also convert the indexes to a single number by mat[0].length * i + j.
7+
class Pair{
8+
int x;
9+
int y;
10+
Pair(int x, int y){
11+
this.x = x;
12+
this.y = y;
13+
}
14+
}
15+
public int[][] updateMatrix(int[][] mat) {
16+
int level = 0;
17+
visited = new boolean[mat.length][mat[0].length];
18+
Queue<Pair> q = new ArrayDeque<>();
19+
// Addition of all pairs in mat that have 0.
20+
for(int i = 0; i < mat.length; i++){
21+
for(int j = 0; j < mat[0].length; j++){
22+
if(mat[i][j] == 0){
23+
visited[i][j] = true;
24+
q.add(new Pair(i, j));
25+
}
26+
}
27+
}
28+
while(q.size()>0){
29+
int size = q.size();
30+
while(size-- > 0){
31+
Pair p = q.remove();
32+
mat[p.x][p.y] = level;
33+
if(p.x > 0 && visited[p.x - 1][p.y] == false){
34+
visited[p.x-1][p.y] = true;
35+
q.add(new Pair(p.x-1, p.y));
36+
}
37+
if(p.x < mat.length - 1 && visited[p.x + 1][p.y] == false){
38+
visited[p.x+1][p.y] = true;
39+
q.add(new Pair(p.x+1, p.y));
40+
41+
}
42+
if(p.y > 0 && visited[p.x][p.y-1] == false){
43+
visited[p.x][p.y-1] = true;
44+
q.add(new Pair(p.x, p.y-1));
45+
46+
}
47+
if(p.y < mat[0].length-1 && visited[p.x][p.y + 1] == false){
48+
visited[p.x][p.y+1] = true;
49+
q.add(new Pair(p.x, p.y + 1));
50+
}
51+
}
52+
level++;
53+
}
54+
return mat;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number[][]}
4+
*/
5+
var updateMatrix = function(mat) {
6+
const rows = mat.length,
7+
cols = mat[0].length;
8+
9+
if (!rows) return mat;
10+
11+
const queue = new Queue();
12+
const dist = Array.from({length: rows}, () => new Array(cols).fill(Infinity));
13+
14+
for (let i = 0; i < rows; i++) {
15+
for (let j = 0; j < cols; j++) {
16+
if (!mat[i][j]) {
17+
dist[i][j] = 0;
18+
queue.enqueue([i, j]);
19+
}
20+
}
21+
}
22+
23+
const dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]];
24+
25+
while (!queue.isEmpty()) {
26+
const [ row, col ] = queue.dequeue();
27+
for (let i = 0; i < 4; i++) {
28+
const nRow = row + dirs[i][0],
29+
nCol = col + dirs[i][1];
30+
31+
if (nRow < 0 || nCol < 0 || nRow >= rows || nCol >= cols) continue;
32+
33+
if (dist[nRow][nCol] > dist[row][col] + 1) {
34+
dist[nRow][nCol] = dist[row][col] + 1;
35+
queue.enqueue([nRow, nCol]);
36+
}
37+
}
38+
}
39+
40+
return dist;
41+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
3+
rows = len(mat)
4+
cols = len(mat[0])
5+
6+
for i in range(rows):
7+
for j in range(cols):
8+
if mat[i][j] != 0:
9+
top = mat[i-1][j] if i>0 else float('inf')
10+
left = mat[i][j-1] if j>0 else float('inf')
11+
mat[i][j] = 1 + min(top, left)
12+
13+
for i in range(rows-1, -1, -1):
14+
for j in range(cols-1, -1, -1):
15+
if mat[i][j] != 0:
16+
right = mat[i][j+1] if j<cols-1 else float('inf')
17+
bottom = mat[i+1][j] if i<rows-1 else float('inf')
18+
mat[i][j] = min(min(right, bottom)+1, mat[i][j])
19+
20+
21+
return mat
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "542",
3+
"name": "01 Matrix",
4+
"url": "https://leetcode.com/problems/01-matrix",
5+
"difficulty": "medium",
6+
"premium": false,
7+
"topic": "algorithms",
8+
"solution": {
9+
"c++": true,
10+
"python": true,
11+
"java": true,
12+
"javascript": true
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool isOneBitCharacter(vector<int>& bits)
4+
{
5+
int n = bits.size();
6+
if(n == 1)
7+
return true;
8+
9+
int i = 0;
10+
while(i <= n - 2)
11+
{
12+
if(bits[i] == 0)
13+
i++;
14+
else
15+
i = i + 2;
16+
}
17+
if(i <= n-1)
18+
return true;
19+
else
20+
return false;
21+
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean isOneBitCharacter(int[] bits) {
3+
int ones = 0;
4+
//Starting from one but last, as last one is always 0.
5+
for (int i = bits.length - 2; i >= 0 && bits[i] != 0 ; i--) {
6+
ones++;
7+
}
8+
if (ones % 2 > 0) return false;
9+
return true;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number[]} bits
3+
* @return {boolean}
4+
*/
5+
var isOneBitCharacter = function(bits) {
6+
let i = 0;
7+
while (i < bits.length - 1) {
8+
if (bits[i] === 1) i++;
9+
i++;
10+
}
11+
return bits[i] === 0;
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Dev: Chumicat
2+
# Date: 2019/11/30
3+
# Submission: https://leetcode.com/submissions/detail/282638543/
4+
# (Time, Space) Complexity : O(n), O(1)
5+
6+
class Solution:
7+
def isOneBitCharacter(self, bits: List[int]) -> bool:
8+
"""
9+
:type bits: List[int]
10+
:rtype: bool
11+
"""
12+
# Important Rules:
13+
# 1. If bit n is 0, bit n+1 must be a new char
14+
# 2. If bits end with 1, last bit must be a two bit char
15+
# However, this case had been rejected by question
16+
# 3. If 1s in row and end with 0,
17+
# we can use count or 1s to check last char
18+
# If count is even, last char is "0"
19+
# If count is odd, last char is "10"
20+
# Strategy:
21+
# 1. We don't care last element, since it must be 0.
22+
# 2. We check from reversed, and count 1s in a row
23+
# 3. Once 0 occur or list end, We stop counting
24+
# 4. We use count to determin result
25+
# 5. Since we will mod count by 2, we simplify it to bool
26+
ret = True
27+
for bit in bits[-2::-1]:
28+
if bit: ret = not ret
29+
else: break
30+
return ret
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "717",
3+
"name": "1-bit and 2-bit Characters",
4+
"url": "https://leetcode.com/problems/1-bit-and-2-bit-characters",
5+
"difficulty": "easy",
6+
"premium": false,
7+
"topic": "algorithms",
8+
"solution": {
9+
"c++": true,
10+
"python": true,
11+
"java": true,
12+
"javascript": true
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool find132pattern(vector<int>& nums) {
4+
// Initialise a empty stack "s"...
5+
stack<int> s;
6+
// To keep track of minimum element...
7+
int min = INT_MIN;
8+
// Run a Loop from last to first index...
9+
for (int i = nums.size() - 1; i >= 0; i--) {
10+
// If min is greater than nums[i], return true...
11+
if (nums[i] < min)
12+
return true;
13+
// If stack is not empty & nums[i] is greater than the top element of stack, then pop the element...
14+
while (!s.empty() && nums[i] > s.top()) {
15+
min = s.top();
16+
s.pop();
17+
}
18+
// Otherwise, push nums[i] into stack...
19+
s.push(nums[i]);
20+
}
21+
// If the condition is not satisfied, return false.
22+
return false;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean find132pattern(int[] nums) {
3+
int min = Integer.MIN_VALUE;
4+
int peak = nums.length;
5+
for (int i = nums.length - 1; i >= 0; i--) {
6+
// We find a "132" pattern if nums[i] < min, so return true...
7+
if (nums[i] < min)
8+
return true;
9+
// If peak < nums.length & nums[i] is greater than the peak element...
10+
while (peak < nums.length && nums[i] > nums[peak])
11+
min = nums[peak++];
12+
// Now we have nums[i] <= nums[peak]
13+
// We push nums[i] to the "stack"
14+
peak--;
15+
nums[peak] = nums[i];
16+
}
17+
return false;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var find132pattern = function(nums) {
2+
let m = -Infinity
3+
// Initialise a empty stack...
4+
const stack = []
5+
// Run a Loop from last to first index...
6+
for (let i = nums.length - 1; i >= 0; i--) {
7+
// If nums[i] is greater than the top element of stack, then pop the element...
8+
while (nums[i] > stack[stack.length - 1]) {
9+
m = stack.pop()
10+
}
11+
// If m is greater than nums[i], return true...
12+
if (nums[i] < m) {
13+
return true
14+
}
15+
// Otherwise, push nums[i] into stack...
16+
stack.push(nums[i])
17+
}
18+
// If the condition is not satisfied, return false.
19+
return false
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def find132pattern(self, nums):
3+
# Base Condition...
4+
if len(nums) < 3:
5+
return False
6+
m = float('-inf')
7+
# Initialise a empty stack...
8+
stack = []
9+
# Run a Loop from last to first index...
10+
for i in range(len(nums)-1, -1, -1):
11+
# If m is greater than nums[i], return true...
12+
if nums[i] < m:
13+
return True
14+
# If stack is not empty & nums[i] is greater than the top element of stack, then pop the element...
15+
else:
16+
while stack and stack[-1] < nums[i]:
17+
m = stack.pop()
18+
# Otherwise, append nums[i] into stack...
19+
stack.append(nums[i])
20+
# If the condition is not satisfied, return false.
21+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": "456",
3+
"name": "132 Pattern",
4+
"url": "https://leetcode.com/problems/132-pattern",
5+
"difficulty": "medium",
6+
"premium": false,
7+
"topic": "algorithms",
8+
"solution": {
9+
"c++": true,
10+
"python": true,
11+
"java": true,
12+
"javascript": true
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int minSteps(int n) {
4+
for (int j = n/2; j >= 1; j--) {
5+
if (n % j == 0) {
6+
return minSteps(j) + n/j;
7+
}
8+
}
9+
return 0;
10+
}
11+
};

0 commit comments

Comments
 (0)