Skip to content

Commit 17f1fdd

Browse files
committed
新增解題練習(Pythoon & JS),python oop練習
1 parent 68a6aac commit 17f1fdd

32 files changed

Lines changed: 217 additions & 132 deletions

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
2. 題目來源:
55
- LeetCode
66
- CodeWars
7+
- HackerRank
78

89
雖是JavaScript,但實際上使用Node.js,因此不需要打開瀏覽器便可執行JS的環境
9-
CMD打上node app.js
10+
CMD打上node {檔案名稱.js},EG.node index.js <br>
11+
而Python,則是打上 python3 {檔案名稱.py} EG.python3 index.py

javascript/LeetCode/Array/3683.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 3683. Earliest Time to Finish One Task
3+
*
4+
* task = [start time,finsh time]
5+
* 回傳task最早完成的時間
6+
* @param {number[][]} tasks
7+
* @return {number}
8+
*/
9+
var earliestTime = function(tasks) {
10+
let ans = Infinity;
11+
for(let i = 0;i < tasks.length;i++) {
12+
ans = Math.min(ans,tasks[i][0] + tasks[i][1]);
13+
}
14+
return ans;
15+
};
16+
let tasks = [[1,6],[2,3]];
17+
// 5
18+
// 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.
19+
console.log(earliestTime(tasks));

javascript/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,4 +1028,3 @@ var closetPair = function(arr1,arr2,x) {
10281028
// [1,30];
10291029
// console.log(closetPair(arr1,arr2,x));
10301030

1031-
//

python/index.py

Lines changed: 10 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from math import sqrt
1111
from operator import le
1212
from typing import List
13+
import math
1314

1415

1516
# from numpy import diff
@@ -22,38 +23,25 @@
2223

2324
# oop
2425
# from file name(module) import the class.
26+
from oop.Animals import Animals
27+
from oop.Tortoises import Tortoises
28+
29+
greek = Tortoises("福氣","7個月大","veggie","地中海型陸龜")
30+
greek.eat()
31+
greek.environment()
32+
2533
# from oop.Fruit import Fruit
2634
# from oop.Melon import Melon
2735
# fruits = Fruit("Apple",3,5)
28-
# print(fruits.calculate())
36+
# print("價格:",f"{fruits.calculate()}")
2937

30-
# fruits = Fruit()
3138
# fruits.make_watering()
3239

33-
# v = Melon()
40+
# v = Melon("西瓜",65,10,30)
3441
# v.make_seedling()
3542
# v.palnt()
3643

3744

38-
39-
def diagonalDifference(arr):
40-
'''
41-
Diagonal Difference
42-
Complete the 'diagonalDifference' function below.
43-
44-
The function is expected to return an INTEGER.
45-
The function accepts 2D_INTEGER_ARRAY arr as parameter.
46-
'''
47-
left = 0
48-
right = 0
49-
for i in range(len(arr)):
50-
left += arr[i][i]
51-
right += arr[i][len(arr) - 1 - i]
52-
return abs(left - right)
53-
54-
55-
56-
5745
'''
5846
1415. The k-th Lexicographical String of All Happy Strings of Length n
5947
@@ -221,53 +209,6 @@ def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
221209
# print(a.getFinalState(nums,k,multiplier))
222210

223211

224-
225-
'''
226-
3407. Substring Matching Pattern
227-
228-
You are given a string s and a pattern string p, where p contains exactly one '*' character.
229-
The '*' in p can be replaced with any sequence of zero or more characters.
230-
Return true if p can be made a substring of s, and false otherwise.
231-
232-
Hints:
233-
1. Divide the pattern in two strings and search in the string.
234-
235-
Example 1:
236-
Input: s = "leetcode", p = "ee*e"
237-
Output: true
238-
Explanation:
239-
By replacing the '*' with "tcod", the substring "eetcode" matches the pattern.
240-
241-
Example 2:
242-
Input: s = "car", p = "c*v"
243-
Output: false
244-
Explanation:
245-
There is no substring matching the pattern.
246-
247-
Example 3:
248-
Input: s = "luck", p = "u*"
249-
Output: true
250-
Explanation:
251-
The substrings "u", "uc", and "uck" match the pattern.
252-
253-
Constraints:
254-
1 <= s.length <= 50
255-
1 <= p.length <= 50
256-
s contains only lowercase English letters.
257-
p contains only lowercase English letters and exactly one '*'
258-
259-
參數為一個字串s和字串p,p內有一個"*"符號,而該符號可被替換成任一或多個字母
260-
若p的*號在替換成字母後可變成s的子字串,則回傳true
261-
否則false
262-
'''
263-
def hasMatch(s: str, p: str) -> bool:
264-
# 將字串拆成兩部分再搜尋
265-
return False
266-
# s = "leetcode"
267-
# p = "ee*e"
268-
# True
269-
# print(hasMatch(s,p))
270-
271212
'''
272213
2523. Closest Prime Numbers in Range
273214
@@ -296,8 +237,6 @@ def hasMatch(s: str, p: str) -> bool:
296237
297238
Constraints:
298239
1 <= left <= right <= 106
299-
300-
301240
'''
302241
def closestPrimes(left: int, right: int) -> List[int]:
303242
'''
@@ -353,48 +292,3 @@ def isPrime(element:int):
353292
# print(closestPrimes(left,right))
354293

355294

356-
'''
357-
2873. Maximum Value of an Ordered Triplet I
358-
359-
You are given a 0-indexed integer array nums.
360-
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.
361-
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
362-
363-
Hints:
364-
1.Use three nested loops to find all the triplets.
365-
366-
Example 1:
367-
Input: nums = [12,6,1,2,7]
368-
Output: 77
369-
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
370-
It can be shown that there are no ordered triplets of indices with a value greater than 77.
371-
372-
Example 2:
373-
Input: nums = [1,10,3,4,19]
374-
Output: 133
375-
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
376-
It can be shown that there are no ordered triplets of indices with a value greater than 133.
377-
Example 3:
378-
379-
Input: nums = [1,2,3]
380-
Output: 0
381-
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.
382-
383-
384-
Constraints:
385-
3 <= nums.length <= 100
386-
1 <= nums[i] <= 106
387-
388-
'''
389-
def maximumTripletValue(nums: List[int]) -> int:
390-
# i < j < k
391-
# (nums[i] - nums[j]) * nums[k]
392-
ans = 0
393-
394-
return ans
395-
396-
397-
nums = [12,6,1,2,7]
398-
# 77
399-
# (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
400-
print(maximumTripletValue(nums))

0 commit comments

Comments
 (0)