Skip to content

Commit fa71158

Browse files
committed
feat: Solved 15 Leetcode problems
1 parent 2b2cc9c commit fa71158

20 files changed

+334
-0
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} numBottles
3+
* @param {number} numExchange
4+
* @return {number}
5+
*/
6+
var numWaterBottles = function(numBottles, numExchange) {
7+
let maxWaterBottles=numBottles;
8+
9+
while((numBottles-=numExchange)>=0){
10+
maxWaterBottles++
11+
numBottles++
12+
}
13+
14+
return maxWaterBottles
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param {string} haystack
3+
* @param {string} needle
4+
* @return {number}
5+
*/
6+
var strStr = function(haystack, needle) {
7+
return haystack.indexOf(needle)
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var numberGame = function(nums) {
6+
nums.sort((a,b)=>a-b)
7+
8+
for(let i=0, temp;i<nums.length;i+=2)
9+
{
10+
temp=nums[i]
11+
nums[i]=nums[i+1]
12+
nums[i+1]=temp
13+
}
14+
15+
return nums
16+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
/**
3+
* @param {string} s
4+
* @return {number}
5+
*/
6+
var romanToInt = function(s) {
7+
const conv={
8+
'I':1,
9+
'V':5,
10+
'X':10,
11+
'L':50,
12+
'C':100,
13+
'D':500,
14+
'M':1000
15+
}
16+
17+
let total=0
18+
19+
for(let i=0, next, c;i<s.length;i++){
20+
next=(i<s.length-1)?s[i+1]:null
21+
c=s[i]
22+
23+
if(c==='I')
24+
total+=(next!==null&&(next==='V'||next==='X'))?-1:1
25+
else if(c==='X')
26+
total+=(next!==null&&(next==='L'||next==='C'))?-10:10
27+
else if(c==='C')
28+
total+=(next!==null&&(next==='D'||next==='M'))?-100:100
29+
else
30+
total+=conv[c]
31+
}
32+
return total
33+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var searchInsert = function(nums, target) {
7+
let low=0,high= nums.length-1,mid, isUpper
8+
9+
while(high>=low)
10+
{
11+
mid= Math.floor((high+low)/2)
12+
13+
if(nums[mid]===target)
14+
return mid
15+
16+
else if(nums[mid]>target){
17+
high=mid-1
18+
isUpper=false
19+
}
20+
21+
else{
22+
low=mid+1
23+
isUpper=true
24+
}
25+
}
26+
27+
return isUpper?mid+1:mid
28+
};

random/2024-july/easy/two-sums.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function(nums, target) {
7+
const map =new Map()
8+
9+
nums.forEach((num, i)=>{
10+
const val = map.get(num)
11+
12+
if(val===undefined)
13+
map.set(num, [i])
14+
else
15+
{
16+
val.push(i)
17+
map.set(num, val)
18+
}
19+
})
20+
21+
for(let i=0;i<nums.length;i++){
22+
const diff=target-nums[i]
23+
const idxs= map.get(diff)
24+
25+
if(idxs===undefined)
26+
continue
27+
28+
if(diff===nums[i]){
29+
if(idxs.length<2)
30+
continue
31+
return idxs.slice(0,2)
32+
}
33+
34+
return [i, idxs[0]]
35+
}
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var calculate = function(s) {
6+
s=s.replace(' ','')
7+
let length =s.length,isPlusSign=false
8+
9+
const getLastNumberIdx=()=>{
10+
let idx
11+
12+
for(let i=length-1;i>=0;i--)
13+
if(i===0)
14+
idx=0
15+
else if(s[i]==='+'||s[i]==='-'||s[i]==='*'||s[i]==='/')
16+
{
17+
idx=i+1
18+
break
19+
}
20+
21+
let n = s.substring(idx, length)
22+
length=idx
23+
return n
24+
}
25+
26+
const numbers=[], operators=[]
27+
let accum,n, prevOp=null, currOp=null
28+
29+
const hasGreaterPrecedence=(prevOp, currOp)=>{
30+
if(currOp==='/'||prevOp===null)
31+
return true
32+
33+
return currOp!=='+'
34+
}
35+
36+
const checkSign=(n)=>{
37+
if(length===0||s[length-1]!=='-')
38+
return n
39+
40+
length--
41+
isPlusSign=true
42+
return -n
43+
}
44+
45+
const peekCurrSign=()=>isPlusSign?'+':s[length-1]
46+
47+
const getCurrSign=()=>{
48+
const sign = peekCurrSign()
49+
50+
if(!isPlusSign)
51+
length--
52+
else
53+
isPlusSign=false
54+
55+
return sign
56+
}
57+
58+
accum=getLastNumberIdx()
59+
if(accum===null)
60+
return null
61+
62+
accum=checkSign(parseInt(accum))
63+
numbers.push(accum)
64+
65+
while(length>0)
66+
{
67+
prevOp=null
68+
69+
while(length>0&&hasGreaterPrecedence(prevOp, peekCurrSign()))
70+
{
71+
currOp=getCurrSign()
72+
operators.push(currOp)
73+
74+
n=getLastNumberIdx()
75+
if(n===null)
76+
return null
77+
78+
n=checkSign(parseInt(n))
79+
numbers.push(n)
80+
81+
prevOp=currOp
82+
}
83+
84+
accum=numbers.pop()
85+
86+
while(operators.length>0){
87+
// Testing
88+
//console.log(`[${numbers}] [${operators}] [${accum}] [${s}]`)
89+
90+
n = numbers.pop()
91+
currOp=operators.pop()
92+
93+
if(currOp==='+')
94+
accum+=n
95+
96+
else if(currOp==='-')
97+
accum-=n
98+
else if(currOp==='*')
99+
accum*=n
100+
101+
else if(currOp==='/')
102+
accum=Math.trunc(accum/n)
103+
}
104+
numbers.push(accum)
105+
}
106+
107+
return accum
108+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var largestPerimeter = function(nums) {
6+
let longestSize, perimeter
7+
8+
nums.sort((a,b)=>a-b)
9+
10+
while(nums.length>=3){
11+
longestSide = nums.pop()
12+
perimeter = nums.reduce((accum, value)=>accum+value)
13+
14+
if(perimeter>longestSide)
15+
return perimeter+longestSide
16+
}
17+
return -1
18+
};

random/2024-july/medium/three-sum.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var threeSum = function(nums) {
6+
nums.sort((a,b)=>a-b)
7+
8+
if(nums[0]>0||nums.length<2)
9+
return []
10+
11+
const triplets=[], idxsMap =new Map(),tripletsMap = new Map()
12+
let val
13+
14+
nums.forEach((num, i)=>{
15+
val = idxsMap.get(num)
16+
17+
if(val===undefined)
18+
idxsMap.set(num, [i])
19+
else
20+
{
21+
val.push(i)
22+
idxsMap.set(num, val)
23+
}
24+
})
25+
26+
for(let i=0, j, innerMap, diff, isValid;i<nums.length&&nums[i]<=0;i++)
27+
for(j=i+1;j<nums.length;j++){
28+
diff=0-nums[i]-nums[j]
29+
30+
if(diff<nums[i]||diff<nums[j])
31+
break
32+
33+
val = idxsMap.get(diff)
34+
35+
if(val!==undefined){
36+
if(tripletsMap.get(nums[i])===undefined)
37+
tripletsMap.set(nums[i], new Map())
38+
innerMap=tripletsMap.get(nums[i])
39+
40+
if(innerMap.get(nums[j])===undefined)
41+
innerMap.set(nums[j], new Map())
42+
innerMap=innerMap.get(nums[j])
43+
44+
if(innerMap.get(diff)!==undefined)
45+
continue
46+
47+
for(let k of val){
48+
isValid = k!==i&&k!==j
49+
50+
if(isValid){
51+
triplets.push([nums[i], nums[j], diff])
52+
break
53+
}
54+
}
55+
innerMap.set(diff, isValid)
56+
}
57+
}
58+
59+
return triplets
60+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Write your MySQL query statement below
2+
SELECT a1.machine_id, ROUND(AVG(a1.avg_activity_time),3) AS processing_time FROM (SELECT a1.machine_id, a1.process_id, MAX(a1.timestamp)-MIN(a1.timestamp) AS avg_activity_time FROM Activity AS a1 GROUP BY a1.machine_id, a1.process_id) AS a1 GROUP BY a1.machine_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Write your MySQL query statement below
2+
SELECT v.customer_id AS customer_id, COUNT(v.visit_id) AS count_no_trans FROM Visits AS v LEFT JOIN Transactions AS t ON v.visit_id = t.visit_id WHERE t.transaction_id IS NULL GROUP BY v.customer_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Write your MySQL query statement below
2+
SELECT e.name, b.bonus FROM Employee AS e LEFT JOIN Bonus AS b ON b.empId=e.empId WHERE b.bonus IS NULL OR b.bonus<1000;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Write your MySQL query statement below
2+
SELECT p.product_name, s.year, s.price FROM Sales AS s INNER JOIN Product AS p ON s.product_id=p.product_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Write your MySQL query statement below
2+
SELECT w2.id FROM (SELECT w1.id AS id, w1.recordDate AS second_recordDate,w1.temperature AS second_temperature,LAG(w1.recordDate) OVER (ORDER BY w1.recordDate) AS first_recordDate ,LAG(w1.temperature) OVER (ORDER BY w1.recordDate) AS first_temperature FROM Weather AS w1) AS w2 WHERE w2.second_temperature IS NOT NULL AND DATE_ADD(w2.second_recordDate, INTERVAL -1 DAY) = w2.first_recordDate AND w2.second_temperature>w2.first_temperature ORDER BY w2.id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Write your PostgreSQL query statement below
2+
SELECT st.student_id, st.student_name, su.subject_name, COUNT(ex.subject_name) AS attended_exams FROM Students AS st CROSS JOIN Subjects AS su LEFT JOIN Examinations AS ex ON st.student_id = ex.student_id AND su.subject_name=ex.subject_name GROUP BY st.student_id, st.student_name,su.subject_name,ex.subject_name ORDER BY st.student_id, su.subject_name;

0 commit comments

Comments
 (0)