Skip to content
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
2. 題目來源:
- LeetCode
- CodeWars
- HackerRank

雖是JavaScript,但實際上使用Node.js,因此不需要打開瀏覽器便可執行JS的環境
CMD打上node app.js
CMD打上node {檔案名稱.js},EG.node index.js <br>
而Python,則是打上 python3 {檔案名稱.py} EG.python3 index.py
19 changes: 19 additions & 0 deletions javascript/LeetCode/Array/3683.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 3683. Earliest Time to Finish One Task
*
* task = [start time,finsh time]
* 回傳task最早完成的時間
* @param {number[][]} tasks
* @return {number}
*/
var earliestTime = function(tasks) {
let ans = Infinity;
for(let i = 0;i < tasks.length;i++) {
ans = Math.min(ans,tasks[i][0] + tasks[i][1]);
}
return ans;
};
let tasks = [[1,6],[2,3]];
// 5
// The first task starts at time t = 1 and finishes at time 1 + 6 = 7. The second task finishes at time 2 + 3 = 5. You can finish one task at time 5.
console.log(earliestTime(tasks));
37 changes: 37 additions & 0 deletions javascript/LeetCode/math/3658.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 3658. GCD of Odd and Even Sums
*
* GCD 是 最大公因數(Greatest Common Divisor)的縮寫,指的是能夠整除兩個或以上非零整數的最大正整數。 例如,8和12的最大公因數是4,因為4是8和12的公因數中最大的那個
*
* sumOdd = 從1開始,n個奇數的總和
* sumEven = 從1開始,n個偶數總和
* gcd(sumOdd,sumEven) = answer
*
* @param {number} n
* @return {number}
*/
var gcdOfOddEvenSums = function(n) {
// ans 能夠整除sumOdd & sumEven
// let sumOdd = [],sumEven = [];
// for(let i = 1;i <= n*2;i++) {
// if(i % 2 === 0 && sumOdd.length <= n){
// sumEven.push(i);
// }else{
// sumOdd.push(i);
// }
// }
// let odd = sumOdd.reduce((a,b)=>a+b,0);
// let even = sumEven.reduce((a,b)=>a+b,0);
// return Math.abs(odd-even);

// solution 2
let sumEven = n * (n + 1);
let sumOdd = n * n;
return Math.abs(sumOdd - sumEven);
};
let n = 4;
// 4
// Sum of the first 4 odd numbers sumOdd = 1 + 3 + 5 + 7 = 16
// Sum of the first 4 even numbers sumEven = 2 + 4 + 6 + 8 = 20
// Hence, GCD(sumOdd, sumEven) = GCD(16, 20) = 4.
console.log(gcdOfOddEvenSums(n))
3 changes: 1 addition & 2 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,8 +1024,7 @@ var closetPair = function(arr1,arr2,x) {
}

}
// let arr1 = [1,4,5,7],arr2 = [10,20,30,40],x = 32;
// let arr1 = [1,4,5,7],arr2 = [10,20,30,40],x = 32;
// [1,30];
// console.log(closetPair(arr1,arr2,x));


130 changes: 14 additions & 116 deletions python/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from math import sqrt
from operator import le
from typing import List
import math


# from numpy import diff
Expand All @@ -22,38 +23,29 @@

# oop
# from file name(module) import the class.
from oop.Animals import Animals
from oop.Tortoises import Tortoises
from oop.Lion import Lion

greek = Tortoises("福氣","7個月大","veggie","地中海型陸龜")
greek.eat()
greek.environment()

lion = Lion("獅子","未知","肉食")
lion.environment()

# from oop.Fruit import Fruit
# from oop.Melon import Melon
# fruits = Fruit("Apple",3,5)
# print(fruits.calculate())
# print("價格:",f"{fruits.calculate()}")

# fruits = Fruit()
# fruits.make_watering()

# v = Melon()
# v = Melon("西瓜",65,10,30)
# v.make_seedling()
# v.palnt()



def diagonalDifference(arr):
'''
Diagonal Difference
Complete the 'diagonalDifference' function below.

The function is expected to return an INTEGER.
The function accepts 2D_INTEGER_ARRAY arr as parameter.
'''
left = 0
right = 0
for i in range(len(arr)):
left += arr[i][i]
right += arr[i][len(arr) - 1 - i]
return abs(left - right)




'''
1415. The k-th Lexicographical String of All Happy Strings of Length n

Expand Down Expand Up @@ -221,53 +213,6 @@ def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
# print(a.getFinalState(nums,k,multiplier))



'''
3407. Substring Matching Pattern

You are given a string s and a pattern string p, where p contains exactly one '*' character.
The '*' in p can be replaced with any sequence of zero or more characters.
Return true if p can be made a substring of s, and false otherwise.

Hints:
1. Divide the pattern in two strings and search in the string.

Example 1:
Input: s = "leetcode", p = "ee*e"
Output: true
Explanation:
By replacing the '*' with "tcod", the substring "eetcode" matches the pattern.

Example 2:
Input: s = "car", p = "c*v"
Output: false
Explanation:
There is no substring matching the pattern.

Example 3:
Input: s = "luck", p = "u*"
Output: true
Explanation:
The substrings "u", "uc", and "uck" match the pattern.

Constraints:
1 <= s.length <= 50
1 <= p.length <= 50
s contains only lowercase English letters.
p contains only lowercase English letters and exactly one '*'

參數為一個字串s和字串p,p內有一個"*"符號,而該符號可被替換成任一或多個字母
若p的*號在替換成字母後可變成s的子字串,則回傳true
否則false
'''
def hasMatch(s: str, p: str) -> bool:
# 將字串拆成兩部分再搜尋
return False
# s = "leetcode"
# p = "ee*e"
# True
# print(hasMatch(s,p))

'''
2523. Closest Prime Numbers in Range

Expand Down Expand Up @@ -296,8 +241,6 @@ def hasMatch(s: str, p: str) -> bool:

Constraints:
1 <= left <= right <= 106


'''
def closestPrimes(left: int, right: int) -> List[int]:
'''
Expand Down Expand Up @@ -353,48 +296,3 @@ def isPrime(element:int):
# print(closestPrimes(left,right))


'''
2873. Maximum Value of an Ordered Triplet I

You are given a 0-indexed integer array nums.
Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].

Hints:
1.Use three nested loops to find all the triplets.

Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.

Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:

Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.


Constraints:
3 <= nums.length <= 100
1 <= nums[i] <= 106

'''
def maximumTripletValue(nums: List[int]) -> int:
# i < j < k
# (nums[i] - nums[j]) * nums[k]
ans = 0

return ans


nums = [12,6,1,2,7]
# 77
# (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
print(maximumTripletValue(nums))
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.
51 changes: 51 additions & 0 deletions python/leetcode/list/2873.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import List
'''
2873. Maximum Value of an Ordered Triplet I

You are given a 0-indexed integer array nums.
Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].

Hints:
1.Use three nested loops to find all the triplets.

Example 1:
Input: nums = [12,6,1,2,7]
Output: 77
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
It can be shown that there are no ordered triplets of indices with a value greater than 77.

Example 2:
Input: nums = [1,10,3,4,19]
Output: 133
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
It can be shown that there are no ordered triplets of indices with a value greater than 133.
Example 3:

Input: nums = [1,2,3]
Output: 0
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.


Constraints:
3 <= nums.length <= 100
1 <= nums[i] <= 106

'''
def maximumTripletValue(nums: List[int]) -> int:
# i < j < k
# (nums[i] - nums[j]) * nums[k]
sortedSum = (nums[0] - nums[1]) * nums[2]
for i in range(len(nums)):
for j in range(i+1,len(nums)):
for k in range(j+1,len(nums)):
total = (nums[i] - nums[j]) * nums[k]
if(total > sortedSum):
sortedSum = total
return sortedSum if sortedSum > 0 else 0


nums = [12,6,1,2,7]
# 77
# (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
print(maximumTripletValue(nums))
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.
19 changes: 19 additions & 0 deletions python/leetcode/math/3683.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List
import math

'''
3683. Earliest Time to Finish One Task

task = [start time,finsh time]
回傳task最早完成的時間
'''
def earliestTime(tasks: List[List[int]]) -> int:
ans = math.inf
for i in range(len(tasks)):
ans = min(ans,tasks[i][0] + tasks[i][1])

return ans

tasks = [[1,6],[2,3]]
# 5
print(earliestTime(tasks))
53 changes: 53 additions & 0 deletions python/leetcode/string/3407.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'''
3407. Substring Matching Pattern

You are given a string s and a pattern string p, where p contains exactly one '*' character.
The '*' in p can be replaced with any sequence of zero or more characters.
Return true if p can be made a substring of s, and false otherwise.

Hints:
1. Divide the pattern in two strings and search in the string.

Example 1:
Input: s = "leetcode", p = "ee*e"
Output: true
Explanation:
By replacing the '*' with "tcod", the substring "eetcode" matches the pattern.

Example 2:
Input: s = "car", p = "c*v"
Output: false
Explanation:
There is no substring matching the pattern.

Example 3:
Input: s = "luck", p = "u*"
Output: true
Explanation:
The substrings "u", "uc", and "uck" match the pattern.

Constraints:
1 <= s.length <= 50
1 <= p.length <= 50
s contains only lowercase English letters.
p contains only lowercase English letters and exactly one '*'

參數為一個字串s和字串p,p內有一個"*"符號,而該符號可被替換成任一或多個字母
若p的*號在替換成字母後可變成s的子字串,則回傳true
否則false
'''
def hasMatch(s: str, p: str) -> bool:
# 將字串拆成兩部分再搜尋
left,right = p.split("*")
left_part = s.find(left)
right_part = s.rfind(right)

if left_part == -1 or right_part == -1:
return False
return left_part + len(left) <= right_part

s = "luck"
p = "u*"
# True
# 將s的tcod塞進p的*內,p等同是s的子字串
print(hasMatch(s,p))
Loading