Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d9da4c7

Browse files
committed
Rename file for correct name ordering
1 parent 579a0eb commit d9da4c7

File tree

165 files changed

+614
-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.

165 files changed

+614
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {number} val
11+
* @return {TreeNode}
12+
*/
13+
var searchBST = function(root, val) {
14+
function helper(root,val){
15+
if(root){
16+
if(val < root.val){
17+
return helper(root.left,val)
18+
} else if (val> root.val){
19+
return helper(root.right,val)
20+
}
21+
return root ;
22+
}
23+
}
24+
let result = helper(root,val)
25+
return result != undefined ? result : null
26+
};

solutions/0733_FloodFill.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def floodFill(self, image, sr, sc, newColor):
3+
"""
4+
:type image: List[List[int]]
5+
:type sr: int
6+
:type sc: int
7+
:type newColor: int
8+
:rtype: List[List[int]]
9+
"""
10+
def helper(i,j, target, newColor):
11+
if i< 0 or i >= len(image) or j < 0 or j >= len(image[0]):
12+
return
13+
14+
if image[i][j] != target or image[i][j] == newColor:
15+
return
16+
else:
17+
image[i][j] = newColor
18+
helper(i+1,j,target,newColor)
19+
helper(i-1,j,target,newColor)
20+
helper(i,j+1,target,newColor)
21+
helper(i,j-1,target,newColor)
22+
23+
helper(sr,sc,image[sr][sc],newColor)
24+
return image

solutions/0767_ReorganizeString.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {string} S
3+
* @return {string}
4+
*/
5+
//NOT OPTIMIZED SOLUTION
6+
var reorganizeString = function(S) {
7+
let result = '';
8+
let dict = {};
9+
for(let i=0;i<S.length;i++){
10+
if(dict[S[i]]){
11+
dict[S[i]]++
12+
} else{
13+
dict[S[i]] = 1;
14+
}
15+
}
16+
let sortable = [];
17+
for(let key in dict){
18+
sortable.push([key,dict[key]])
19+
}
20+
sortable = sortable.sort((a,b)=>{
21+
return b[1]-a[1]
22+
})
23+
let index=0;
24+
while(sortable.length > 0 ){
25+
if(result[index-1]==sortable[0][0]){
26+
//next after max
27+
result = result + sortable[1][0]
28+
sortable[1][1] = sortable[1][1] - 1;
29+
if(sortable[1][1] < 1){
30+
sortable.splice(1,1); //if char count <= 0 remove it from array
31+
}
32+
} else {
33+
result = result + sortable[0][0]
34+
sortable[0][1] = sortable[0][1] - 1;
35+
if(sortable[0][1] < 1){
36+
sortable.splice(0,1); //if char count <= 0 remove it from array
37+
}
38+
}
39+
index++;
40+
sortable = sortable.sort((a,b)=>{
41+
return b[1]-a[1]
42+
})
43+
if(sortable.length == 1 && sortable[0][0] == result[index-1]){
44+
return ''
45+
}
46+
}
47+
return result;
48+
};

solutions/0771_JewelsandStones.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string} J
3+
* @param {string} S
4+
* @return {number}
5+
*/
6+
var numJewelsInStones = function(J, S) {
7+
let jewels_dict = {};
8+
let count = 0;
9+
for(let i=0;i<J.length;i++){
10+
jewels_dict[J[i]] = true;
11+
}
12+
for(let i=0;i<S.length;i++){
13+
if(jewels_dict[S[i]]){
14+
count = count+1;
15+
}
16+
}
17+
return count
18+
};

solutions/0779_K-thSymbolinGrammar.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number} N
3+
* @param {number} K
4+
* @return {number}
5+
*/
6+
var kthGrammar = function(N, K) {
7+
function helper(n,k){
8+
if(n===1){
9+
if(k===1){
10+
return 0;
11+
} else {
12+
return 1;
13+
}
14+
}
15+
let half = Math.pow(2,n-1);
16+
if(k<=half){
17+
return helper(n-1,k)
18+
} else {
19+
let result = helper(n-1, k-half)
20+
if(result == 0){
21+
return 1
22+
} else {
23+
return 0
24+
}
25+
}
26+
}
27+
return helper(N,K)
28+
};

solutions/0912_SortanArray.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var sortArray = function(nums) {
6+
function mergeSort(nums){
7+
if(nums.length <=1){
8+
return nums
9+
}
10+
let half = nums.length/2;
11+
let left_side = mergeSort(nums.slice(0,half))
12+
let right_side = mergeSort(nums.slice(half))
13+
return merge(left_side, right_side)
14+
}
15+
16+
function merge(left,right){
17+
let lPointer = 0;
18+
let rPointer = 0;
19+
let result = [];
20+
while(lPointer < left.length && rPointer <right.length){
21+
if(left[lPointer]<right[rPointer]){
22+
result.push(left[lPointer])
23+
lPointer++;
24+
} else {
25+
result.push(right[rPointer]);
26+
rPointer++;
27+
}
28+
}
29+
30+
return result.concat(left.slice(lPointer)).concat(right.slice(rPointer));
31+
}
32+
33+
return mergeSort(nums);
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} S
3+
* @return {number}
4+
*/
5+
var minAddToMakeValid = function(S) {
6+
var balance = 0;
7+
var result = 0;
8+
for(let i = 0;i<S.length;i++){
9+
if(S[i] === '('){
10+
balance = balance + 1;
11+
} else {
12+
balance = balance - 1;
13+
}
14+
// if we should add
15+
if(balance == -1){
16+
result = result + 1;
17+
balance = balance + 1;
18+
}
19+
}
20+
return result+balance
21+
};

solutions/0934_ShortestBridge.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @param {number[][]} A
3+
* @return {number}
4+
*/
5+
var shortestBridge = function(A) {
6+
let queue = []
7+
function dfs(board, i, j){
8+
if(i<0 || i>=board.length || j < 0 || j >= board[0].length || board[i][j]!=1){
9+
return;
10+
}
11+
//mark 1 island as visited
12+
board[i][j]="#";
13+
// add each point of current island for future searching.
14+
queue.push([i,j])
15+
dfs(board,i+1,j);
16+
dfs(board,i-1,j);
17+
dfs(board,i,j+1);
18+
dfs(board,i,j-1);
19+
}
20+
21+
let found = false;
22+
for(let i=0;i<A.length;i++){
23+
if(found){
24+
break;
25+
}
26+
for(let j=0;j<A[0].length;j++){
27+
if(A[i][j]==1){
28+
dfs(A,i,j)
29+
found = true;
30+
break;
31+
}
32+
}
33+
}
34+
let bridge_count = 0;
35+
let rows = A.length;
36+
let cols = A[0].length;
37+
while(queue.length != 0){
38+
let size = queue.length;
39+
// from each point of first island try to find shortest path to another island
40+
for(let i = 0; i<size; i++){
41+
let current = queue.shift();
42+
let curr_i = current[0];
43+
let curr_j = current[1];
44+
45+
//if we find bounderies for second island -> return bridge count;
46+
if(curr_i > 0 && A[curr_i-1][curr_j] == 1 ||
47+
curr_i < rows-1 && A[curr_i+1][curr_j] == 1 ||
48+
curr_j > 0 && A[curr_i][curr_j-1] == 1 ||
49+
curr_j < cols-1 && A[curr_i][curr_j+1] == 1){
50+
return bridge_count;
51+
}
52+
53+
// else mark all zeros as visited
54+
if(curr_i > 0 && A[curr_i-1][curr_j]==0){
55+
// Mark as visited and try to find from this coordinate on the next step;
56+
queue.push([curr_i-1,curr_j])
57+
A[curr_i-1][curr_j] = "#"
58+
}
59+
if(curr_i < rows-1 && A[curr_i+1][curr_j]==0){
60+
// Mark as visited and try to find from this coordinate on the next step;
61+
queue.push([curr_i+1,curr_j])
62+
A[curr_i+1][curr_j] = "#"
63+
}
64+
if(curr_j > 0 && A[curr_i][curr_j-1]==0){
65+
// Mark as visited and try to find from this coordinate on the next step;
66+
queue.push([curr_i,curr_j-1])
67+
A[curr_i][curr_j-1] = '#'
68+
}
69+
if(curr_j < cols-1 && A[curr_i][curr_j+1]==0){
70+
// Mark as visited and try to find from this coordinate on the next step;
71+
queue.push([curr_i,curr_j+1])
72+
A[curr_i][curr_j+1] = '#'
73+
}
74+
}
75+
//if we can't find another island, built 1 bridge and continue
76+
bridge_count++;
77+
}
78+
return bridge_count
79+
};

solutions/0938_RangeSUMofBST.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {number} L
11+
* @param {number} R
12+
* @return {number}
13+
*/
14+
var rangeSumBST = function(root, L, R) {
15+
function dfs(root, L, R){
16+
if(!root){
17+
return 0
18+
} else if(!root.left && !root.right){
19+
if(root.val >= L && root.val<=R){
20+
return root.val
21+
} else {
22+
return 0
23+
}
24+
} else {
25+
if(root.val >= L && root.val <= R){
26+
return root.val + dfs(root.left, L, R) + dfs(root.right, L, R)
27+
} else {
28+
return 0 + dfs(root.left, L, R) + dfs(root.right, L, R)
29+
}
30+
31+
}
32+
}
33+
return dfs(root, L, R)
34+
};

solutions/0965_UnivaluedBinaryTree.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {boolean}
11+
*/
12+
var isUnivalTree = function(root) {
13+
let dict = {};
14+
15+
function helper(root){
16+
if(Object.keys(dict).length > 1){
17+
return false
18+
}
19+
if(root){
20+
if(!dict[root.val]){
21+
dict[root.val] = true;
22+
}
23+
return helper(root.left) && helper(root.right);
24+
}
25+
return true
26+
}
27+
28+
return helper(root)
29+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[][]} points
3+
* @param {number} K
4+
* @return {number[][]}
5+
*/
6+
var kClosest = function(points, K) {
7+
//calc distance
8+
function distance(a){
9+
//assume that b is 0;
10+
return Math.sqrt(Math.pow(a[0],2) + Math.pow(a[1],2));
11+
}
12+
let result = [];
13+
points.forEach(point => {
14+
let obj = {key: point, distance: distance(point)}
15+
result.push(obj)
16+
})
17+
result = result.sort((a,b)=>{
18+
return a.distance - b.distance
19+
}).map((a)=>{
20+
return a.key;
21+
})
22+
return result.slice(0,K)
23+
};

0 commit comments

Comments
 (0)