We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1edfdb0 commit 2f3fbf5Copy full SHA for 2f3fbf5
0070-climbing-stairs.c
@@ -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
@@ -0,0 +1,22 @@
+Memory: 7.97 MB (beats 43.48%)
+class Solution {
+public:
+ int cache[45] = { 0 };
+ int climbStairs(int n) {
20
21
22
+};
0070-climbing-stairs.py
@@ -0,0 +1,16 @@
+"""
+Runtime: 1 ms (beats 3.84%)
+Memory: 17.59 MB (beats 37.71%)
+class Solution:
+ @functools.lru_cache(maxsize=45)
+ def climbStairs(self, n: int) -> int:
+ if n == 0 or n == 1:
+ return 1
+ else:
+ return self.climbStairs(n - 1) + self.climbStairs(n - 2)
0509-fibonacci-number.c
@@ -0,0 +1,18 @@
+509. Fibonacci Number
+Submitted: January 9, 2025
+Runtime: 3 ms (beats 60.33%)
+Memory: 7.74 MB (beats 42.31%)
+int cache[30] = { 0 };
+int fib(int n) {
+ if (n == 0) return 0;
+ if (n == 1 || n == 2) return 1;
+ int res = fib(n - 2) + fib(n - 1);
0509-fibonacci-number.cpp
@@ -1,15 +1,21 @@
/*
509. Fibonacci Number
-Submitted: October 24, 2024
+Submitted: January 10, 2024
-Runtime: 11 ms (beats 43.59%)
-Memory: 7.19 MB (beats 98.56%)
+Runtime: 3 ms (beats 65.66%)
+Memory: 7.61 MB (beats 68.47%)
*/
class Solution {
public:
+ int cache[30] = { 0 };
int fib(int n) {
- return n < 2 ? n : (fib(n - 1) + fib(n - 2));
}
-};
0509-fibonacci-number.py
@@ -3,11 +3,12 @@
Submitted: October 24, 2024
-Runtime: 977 ms (beats 5.12%)
-Memory: 13.87 MB (beats 100.00%)
+Runtime: 38 ms (beats 57.81%)
+Memory: 17.66 MB (beats 24.28%)
"""
class Solution:
+ @functools.cache
def fib(self, n: int) -> int:
if n == 0 or n == 1:
return n
1414-find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k.c
@@ -0,0 +1,41 @@
+1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K
+Runtime: 1 ms (beats 11.11%)
+Memory: 8.16 MB (beats 77.78%)
+#define SIZE 45
+int cache[SIZE + 1];
+int _helper(int);
+int cached_fib(int);
+int findMinFibonacciNumbers(int k) {
+ for (int i = 0; i < SIZE + 1; ++i) {
+ cached_fib(i);
+ return _helper(k);
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
38
39
+ if (cache[n-1] != 0) return cache[n-1];
40
+ return (cache[n-1] = (cached_fib(n - 1) + cached_fib(n - 2)));
41
2185-counting-words-with-a-given-prefix.c
+2185. Counting Words with a Given Prefix
+LeetCode Daily Question for January 9, 2025
+Memory: 8.86
+int prefixCount(char** words, int wordsSize, char* pref) {
+ int res = 0;
+ int pref_len = strlen(pref); // get length of prefix
+ for (int i = 0; i < wordsSize; ++i) {
+ // compare prefix with first pref_len characters of each word
+ res += (strncmp(words[i], pref, pref_len) == 0);
2185-counting-words-with-a-given-prefix.py
@@ -0,0 +1,12 @@
+Memory: 17.86 MB (beats 20.89%)
+ def prefixCount(self, words: List[str], pref: str) -> int:
+ return sum(s.startswith(pref) for s in words)
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments