Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2f3fbf5

Browse files
add 70, 1414, 2185, update 509
1 parent 1edfdb0 commit 2f3fbf5

9 files changed

+160
-7
lines changed

‎0070-climbing-stairs.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
70. Climbing Stairs
3+
4+
Submitted: January 9th, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 7.75 MB (beats 54.99%)
8+
*/
9+
10+
int cache[45] = { 0 };
11+
int climbStairs(int n) {
12+
if (n == 0 || n == 1) {
13+
return 1;
14+
}
15+
if (cache[n - 1] != 0) return cache[n - 1];
16+
int res = climbStairs(n - 2) + climbStairs(n - 1);
17+
if (cache[n - 1] == 0) cache[n - 1] = res;
18+
return res;
19+
}

‎0070-climbing-stairs.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
70. Climbing Stairs
3+
4+
Submitted: January 9th, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 7.97 MB (beats 43.48%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int cache[45] = { 0 };
13+
int climbStairs(int n) {
14+
if (n == 0 || n == 1) {
15+
return 1;
16+
}
17+
if (cache[n - 1] != 0) return cache[n - 1];
18+
int res = climbStairs(n - 2) + climbStairs(n - 1);
19+
if (cache[n - 1] == 0) cache[n - 1] = res;
20+
return res;
21+
}
22+
};

‎0070-climbing-stairs.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
70. Climbing Stairs
3+
4+
Submitted: January 9th, 2025
5+
6+
Runtime: 1 ms (beats 3.84%)
7+
Memory: 17.59 MB (beats 37.71%)
8+
"""
9+
10+
class Solution:
11+
@functools.lru_cache(maxsize=45)
12+
def climbStairs(self, n: int) -> int:
13+
if n == 0 or n == 1:
14+
return 1
15+
else:
16+
return self.climbStairs(n - 1) + self.climbStairs(n - 2)

‎0509-fibonacci-number.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
509. Fibonacci Number
3+
4+
Submitted: January 9, 2025
5+
6+
Runtime: 3 ms (beats 60.33%)
7+
Memory: 7.74 MB (beats 42.31%)
8+
*/
9+
10+
int cache[30] = { 0 };
11+
int fib(int n) {
12+
if (n == 0) return 0;
13+
if (n == 1 || n == 2) return 1;
14+
if (cache[n - 1] != 0) return cache[n - 1];
15+
int res = fib(n - 2) + fib(n - 1);
16+
if (cache[n - 1] == 0) cache[n - 1] = res;
17+
return res;
18+
}

‎0509-fibonacci-number.cpp‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
/*
22
509. Fibonacci Number
33
4-
Submitted: October 24, 2024
4+
Submitted: January 10, 2024
55
6-
Runtime: 11 ms (beats 43.59%)
7-
Memory: 7.19 MB (beats 98.56%)
6+
Runtime: 3 ms (beats 65.66%)
7+
Memory: 7.61 MB (beats 68.47%)
88
*/
99

1010
class Solution {
1111
public:
12+
int cache[30] = { 0 };
1213
int fib(int n) {
13-
return n < 2 ? n : (fib(n - 1) + fib(n - 2));
14+
if (n == 0) return 0;
15+
if (n == 1 || n == 2) return 1;
16+
if (cache[n - 1] != 0) return cache[n - 1];
17+
int res = fib(n - 2) + fib(n - 1);
18+
if (cache[n - 1] == 0) cache[n - 1] = res;
19+
return res;
1420
}
15-
};
21+
};

‎0509-fibonacci-number.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
44
Submitted: October 24, 2024
55
6-
Runtime: 977 ms (beats 5.12%)
7-
Memory: 13.87 MB (beats 100.00%)
6+
Runtime: 38 ms (beats 57.81%)
7+
Memory: 17.66 MB (beats 24.28%)
88
"""
99

1010
class Solution:
11+
@functools.cache
1112
def fib(self, n: int) -> int:
1213
if n == 0 or n == 1:
1314
return n
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K
3+
4+
Submitted: January 9, 2025
5+
6+
Runtime: 1 ms (beats 11.11%)
7+
Memory: 8.16 MB (beats 77.78%)
8+
*/
9+
10+
#define SIZE 45
11+
12+
int cache[SIZE + 1];
13+
14+
int _helper(int);
15+
int cached_fib(int);
16+
17+
int findMinFibonacciNumbers(int k) {
18+
for (int i = 0; i < SIZE + 1; ++i) {
19+
cached_fib(i);
20+
}
21+
return _helper(k);
22+
}
23+
24+
int _helper(int k) {
25+
if (k == 0) return 0;
26+
if (k == 1) return 1;
27+
for (int i = 0; i < SIZE; ++i) {
28+
if (cache[SIZE - i - 1] <= k) {
29+
return _helper(k - cache[SIZE - i - 1]) + 1;
30+
break;
31+
}
32+
}
33+
return -1; // should never get here
34+
}
35+
36+
int cached_fib(int n) {
37+
if (n == 0) return 0;
38+
if (n == 1 || n == 2) return 1;
39+
if (cache[n-1] != 0) return cache[n-1];
40+
return (cache[n-1] = (cached_fib(n - 1) + cached_fib(n - 2)));
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
2185. Counting Words with a Given Prefix
3+
4+
LeetCode Daily Question for January 9, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 8.86
8+
*/
9+
10+
int prefixCount(char** words, int wordsSize, char* pref) {
11+
int res = 0;
12+
int pref_len = strlen(pref); // get length of prefix
13+
for (int i = 0; i < wordsSize; ++i) {
14+
// compare prefix with first pref_len characters of each word
15+
res += (strncmp(words[i], pref, pref_len) == 0);
16+
}
17+
return res;
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
2185. Counting Words with a Given Prefix
3+
4+
LeetCode Daily Question for January 9, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.86 MB (beats 20.89%)
8+
"""
9+
10+
class Solution:
11+
def prefixCount(self, words: List[str], pref: str) -> int:
12+
return sum(s.startswith(pref) for s in words)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /