diff --git a/README.md b/README.md index 0bd94f0..ab4a28d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ 2. 題目來源: - LeetCode - CodeWars +- HackerRank 雖是JavaScript,但實際上使用Node.js,因此不需要打開瀏覽器便可執行JS的環境 -CMD打上node app.js \ No newline at end of file +CMD打上node {檔案名稱.js},EG.node index.js
+而Python,則是打上 python3 {檔案名稱.py} EG.python3 index.py \ No newline at end of file diff --git a/javascript/LeetCode/Array/3683.js b/javascript/LeetCode/Array/3683.js new file mode 100644 index 0000000..2b2f437 --- /dev/null +++ b/javascript/LeetCode/Array/3683.js @@ -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)); \ No newline at end of file diff --git a/javascript/LeetCode/math/3658.js b/javascript/LeetCode/math/3658.js new file mode 100644 index 0000000..546f4a8 --- /dev/null +++ b/javascript/LeetCode/math/3658.js @@ -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)) \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index 944814e..67404b1 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -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)); - diff --git a/python/index.py b/python/index.py index c2d78b5..0c4be74 100644 --- a/python/index.py +++ b/python/index.py @@ -10,6 +10,7 @@ from math import sqrt from operator import le from typing import List +import math # from numpy import diff @@ -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 @@ -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 @@ -296,8 +241,6 @@ def hasMatch(s: str, p: str) -> bool: Constraints: 1 <= left <= right <= 106 - - ''' def closestPrimes(left: int, right: int) -> List[int]: ''' @@ -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)) \ No newline at end of file diff --git a/python/leetcode/array/1347.py b/python/leetcode/list/1347.py similarity index 100% rename from python/leetcode/array/1347.py rename to python/leetcode/list/1347.py diff --git a/python/leetcode/array/1403.py b/python/leetcode/list/1403.py similarity index 100% rename from python/leetcode/array/1403.py rename to python/leetcode/list/1403.py diff --git a/python/leetcode/array/1470.py b/python/leetcode/list/1470.py similarity index 100% rename from python/leetcode/array/1470.py rename to python/leetcode/list/1470.py diff --git a/python/leetcode/array/1512.py b/python/leetcode/list/1512.py similarity index 100% rename from python/leetcode/array/1512.py rename to python/leetcode/list/1512.py diff --git a/python/leetcode/array/162.py b/python/leetcode/list/162.py similarity index 100% rename from python/leetcode/array/162.py rename to python/leetcode/list/162.py diff --git a/python/leetcode/array/1929.py b/python/leetcode/list/1929.py similarity index 100% rename from python/leetcode/array/1929.py rename to python/leetcode/list/1929.py diff --git a/python/leetcode/array/2570.py b/python/leetcode/list/2570.py similarity index 100% rename from python/leetcode/array/2570.py rename to python/leetcode/list/2570.py diff --git a/python/leetcode/array/2610.py b/python/leetcode/list/2610.py similarity index 100% rename from python/leetcode/array/2610.py rename to python/leetcode/list/2610.py diff --git a/python/leetcode/array/283.py b/python/leetcode/list/283.py similarity index 100% rename from python/leetcode/array/283.py rename to python/leetcode/list/283.py diff --git a/python/leetcode/list/2873.py b/python/leetcode/list/2873.py new file mode 100644 index 0000000..0451f41 --- /dev/null +++ b/python/leetcode/list/2873.py @@ -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)) \ No newline at end of file diff --git a/python/leetcode/array/2913.py b/python/leetcode/list/2913.py similarity index 100% rename from python/leetcode/array/2913.py rename to python/leetcode/list/2913.py diff --git a/python/leetcode/array/2942.py b/python/leetcode/list/2942.py similarity index 100% rename from python/leetcode/array/2942.py rename to python/leetcode/list/2942.py diff --git a/python/leetcode/array/2956.py b/python/leetcode/list/2956.py similarity index 100% rename from python/leetcode/array/2956.py rename to python/leetcode/list/2956.py diff --git a/python/leetcode/array/2965.py b/python/leetcode/list/2965.py similarity index 100% rename from python/leetcode/array/2965.py rename to python/leetcode/list/2965.py diff --git a/python/leetcode/array/455.py b/python/leetcode/list/455.py similarity index 100% rename from python/leetcode/array/455.py rename to python/leetcode/list/455.py diff --git a/python/leetcode/array/605.py b/python/leetcode/list/605.py similarity index 100% rename from python/leetcode/array/605.py rename to python/leetcode/list/605.py diff --git a/python/leetcode/array/744.py b/python/leetcode/list/744.py similarity index 100% rename from python/leetcode/array/744.py rename to python/leetcode/list/744.py diff --git a/python/leetcode/array/875.py b/python/leetcode/list/875.py similarity index 100% rename from python/leetcode/array/875.py rename to python/leetcode/list/875.py diff --git a/python/leetcode/math/3683.py b/python/leetcode/math/3683.py new file mode 100644 index 0000000..029e30c --- /dev/null +++ b/python/leetcode/math/3683.py @@ -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)) \ No newline at end of file diff --git a/python/leetcode/string/3407.py b/python/leetcode/string/3407.py new file mode 100644 index 0000000..08012d6 --- /dev/null +++ b/python/leetcode/string/3407.py @@ -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)) \ No newline at end of file diff --git a/python/oop/Animals.py b/python/oop/Animals.py new file mode 100644 index 0000000..b217be2 --- /dev/null +++ b/python/oop/Animals.py @@ -0,0 +1,20 @@ +class Animals: + + def __init__(self,name:str,age:str,food:list[str]): + self.name = name + self.age = age + self.food = food + self.__health_level = 100 # 這是私有屬性 + + # methods + def eat(self): + print(f"{self.name}","is",f"{self.food}") + + def environment(self): + print("依據不同物種來評估適合的環境") + + def get_sick(self): + self.__health_level -= 20 + print(f"{self.name} 的健康值下降了。") + + \ No newline at end of file diff --git a/python/oop/Fruit.py b/python/oop/Fruit.py index 84cbb2e..d926c26 100644 --- a/python/oop/Fruit.py +++ b/python/oop/Fruit.py @@ -2,23 +2,24 @@ class Fruit: - # def __init__(self,name:str,price:int,quantity:int): - # ''' - # 等同於PHP __construct() - # ''' - # self.name = name - # self.price = price - # self.quantity = quantity + def __init__(self,name:str,price:int,quantity:int): + ''' + 等同於PHP __construct() + ''' + self.name = name + self.price = price + self.quantity = quantity - # def calculate(self): - # ''' - # 計算總價 + # methods + def calculate(self): + ''' + 計算總價 - # 優惠: - # 滿3送1 - # ''' - # return self.price * (self.quantity - math.floor(self.quantity / 3)) + 優惠: + 滿3送1 + ''' + return self.price * (self.quantity - math.floor(self.quantity / 3)) def make_seedling(self): print("Make seedling.") diff --git a/python/oop/Lion.py b/python/oop/Lion.py new file mode 100644 index 0000000..68ed66f --- /dev/null +++ b/python/oop/Lion.py @@ -0,0 +1,11 @@ +from oop.Animals import Animals + +class Lion(Animals): + def __init__(self, name, age, food): + super().__init__(name, age, food) + + def eat(self): + return super().eat() + + def environment(self): + print("溫暖") \ No newline at end of file diff --git a/python/oop/Melon.py b/python/oop/Melon.py index 8e34c0e..191b418 100644 --- a/python/oop/Melon.py +++ b/python/oop/Melon.py @@ -2,6 +2,11 @@ class Melon(Fruit): + def __init__(self, name, price, quantity,seed): + super().__init__(name, price, quantity) + self.seed = seed + + # over write def make_seedling(self): print("make melon seed") diff --git a/python/oop/Tortoises.py b/python/oop/Tortoises.py new file mode 100644 index 0000000..bdce47e --- /dev/null +++ b/python/oop/Tortoises.py @@ -0,0 +1,35 @@ +from oop.Animals import Animals + +class Tortoises(Animals): + + def __init__(self, name, age, food,speciceType:str): + super().__init__(name, age, food) + self.speciceType = speciceType + + def eat(self): + return super().eat() + + + # 覆寫父類別的方法 + def environment(self): + ''' + 環境 + ''' + env = {"沙漠型陸龜":"需要較高的溫度熱點", + "地中海型陸龜":"有冬眠習性,但會因為所在地溫度而不冬眠", + "雨林型陸龜":"需要較高的濕度", + "海島型陸龜":"需要較高的濕度" + } + + for key, value in env.items(): + try: + if key in self.speciceType: + print(f"{key}: {value}") + except NameError: + print("eror") + + def hibernation(self): + ''' + 冬眠 + ''' + print("if it's really cold") \ No newline at end of file diff --git a/python/oop/__pycache__/Animals.cpython-312.pyc b/python/oop/__pycache__/Animals.cpython-312.pyc new file mode 100644 index 0000000..4935a6c Binary files /dev/null and b/python/oop/__pycache__/Animals.cpython-312.pyc differ diff --git a/python/oop/__pycache__/Fruit.cpython-312.pyc b/python/oop/__pycache__/Fruit.cpython-312.pyc new file mode 100644 index 0000000..1631e9a Binary files /dev/null and b/python/oop/__pycache__/Fruit.cpython-312.pyc differ diff --git a/python/oop/__pycache__/Lion.cpython-312.pyc b/python/oop/__pycache__/Lion.cpython-312.pyc new file mode 100644 index 0000000..2fa756c Binary files /dev/null and b/python/oop/__pycache__/Lion.cpython-312.pyc differ diff --git a/python/oop/__pycache__/Melon.cpython-312.pyc b/python/oop/__pycache__/Melon.cpython-312.pyc new file mode 100644 index 0000000..ba729cb Binary files /dev/null and b/python/oop/__pycache__/Melon.cpython-312.pyc differ diff --git a/python/oop/__pycache__/Tortoises.cpython-312.pyc b/python/oop/__pycache__/Tortoises.cpython-312.pyc new file mode 100644 index 0000000..e97dcef Binary files /dev/null and b/python/oop/__pycache__/Tortoises.cpython-312.pyc differ