Skip to content

Commit 8b4d776

Browse files
committed
add 234, 500, 832, 1323, 1678, 1684, 2315, 2725, 3248; update 1, 231, 709, 2011
1 parent 42d9ad3 commit 8b4d776

14 files changed

+247
-18
lines changed

0001-two-sum.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
"""
22
1. Two Sum
3-
First solved: February 27, 2024
4-
Last solved: November 22, 2024
3+
4+
Submitted: January 16, 2025
55
66
Runtime: 0 ms (beats 100.00%)
7-
Memory: 17.98 MB (beats 14.36%)
7+
Memory: 19.00 MB (beats 19.63%)
88
"""
99

1010
class Solution:
1111
def twoSum(self, nums: List[int], target: int) -> List[int]:
1212
d = {}
13-
for i in range(len(nums)):
14-
want = target - nums[i]
15-
if want in d:
16-
return [i, d[want]]
17-
else:
18-
d[nums[i]] = i
13+
return next(x for x in (
14+
(i, d[target - nums[i]]) if ((target - nums[i]) in d) else d.update({ nums[i]: i })
15+
for i in range(len(nums))
16+
) if x is not None
17+
)

0231-power-of-two.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/*
22
231. Power of Two
33
4-
Solved: December 9, 2024
4+
Submitted: January 17, 2025
55
66
Runtime: 0 ms (beats 100.00%)
7-
Memory: 7.84 MB (beats 10.33%)
7+
Memory: 7.83 MB (beats 49.22%)
88
*/
99

1010
class Solution {
1111
public:
1212
bool isPowerOfTwo(int n) {
13-
if (n <= 0) return false;
14-
return !(n & (n - 1));
13+
return (n > 0) && !(n & (n - 1));
1514
}
1615
};

0234-palindrome-linked-list.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
234. Palindrome Linked List
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 6 ms (beats 35.32%)
7+
Memory: 132.08 MB (beats 14.40%)
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* struct ListNode {
13+
* int val;
14+
* ListNode *next;
15+
* ListNode() : val(0), next(nullptr) {}
16+
* ListNode(int x) : val(x), next(nullptr) {}
17+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
bool isPalindrome(ListNode* head) {
23+
vector<int> v;
24+
for (; head != nullptr; head = head->next) {
25+
v.push_back(head->val);
26+
}
27+
auto forward = v.cbegin();
28+
auto backward = v.crbegin();
29+
while (forward != v.cend()) {
30+
if (*forward++ != *backward++) return false;
31+
}
32+
return true;
33+
}
34+
};

0500-keyboard-row.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
500. Keyboard Row
3+
4+
Submitted: January 16, 2025
5+
"""
6+
7+
class Solution:
8+
def findWords(self, words: List[str]) -> List[str]:
9+
return [
10+
word for word in words if any(
11+
all(char in row for char in word.lower())
12+
for row in (
13+
'qwertyuiop',
14+
'asdfghjkl',
15+
'zxcvbnm'
16+
)
17+
)
18+
]

0709-to-lower-case.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""
22
709. To Lower Case
33
4-
Submitted: November 22, 2024
4+
Submitted: January 17, 2025
55
66
Runtime: 0 ms (100.00%)
7-
Memory: 16.40 MB (beats 80.46%)
7+
Memory: 17.36 MB (beats 33.60%)
88
"""
99

1010
class Solution:
11-
def __init__(self):
12-
# this is a literal joke
13-
self.toLowerCase = str.lower
11+
def toLowerCase(self, s: str) -> str:
12+
return s.lower()

0832-flipping-an-image.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
832. Flipping an Image
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.73 MB (beats 32.61%)
8+
"""
9+
10+
class Solution:
11+
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
12+
return [[int(not k) for k in i] for i in (j[::-1] for j in image)]

1323-maximum-69-number.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
1323. Maximum 69 Number
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.97 MB (beats 15.18%)
8+
"""
9+
10+
class Solution:
11+
def maximum69Number(self, num: int) -> int:
12+
return max(
13+
max(
14+
int(''.join(
15+
('6' if str(num)[i] == '9' else '9') if i == j else str(num)[j] for j in range(len(str(num)))
16+
)) for i in range(len(str(num)))
17+
),
18+
num
19+
)

1678-goal-parser-interpretation.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
1678. Goal Parser Interpretation
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 50 ms (beats 33.76%)
7+
Memory: 17.61 MB (beats 35.26%)
8+
"""
9+
10+
class Solution:
11+
def interpret(self, command: str) -> str:
12+
return command.replace('()', 'o').replace('(al)', 'al')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
1684. Count the Number of Consistent Strings
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 258 ms (beats 7.24%)
7+
Memory: 19.71 MB (beats 10.46%)
8+
"""
9+
10+
class Solution:
11+
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
12+
return sum(all(c in allowed for c in s) for s in words)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
2011. Final Value of Variable After Performing Operations
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.88 MB (beats 21.09)
8+
"""
9+
10+
class Solution:
11+
def finalValueAfterOperations(self, operations: List[str]) -> int:
12+
return sum((-1 if op[1] == '-' else 1) for op in operations)

2315-count-asterisks.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
2315. Count Asterisks
3+
4+
Submitted: January 17, 2025
5+
6+
RUntime: 0 ms (Beats 100.00%)
7+
Memory: 8.36 MB (beats 68.54%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int countAsterisks(string s) {
13+
bool count = true;
14+
int res = 0;
15+
for (const char c : s) {
16+
if (c == '|') count = !count;
17+
if (count && c == '*') ++res;
18+
}
19+
return res;
20+
}
21+
};

2725-interval-cancellation.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
2725. Interval Cancellation
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 43 ms (beats 99.89%)
7+
Memory: 49.62 MB (beats 51.41%)
8+
*/
9+
10+
/**
11+
* @param {Function} fn
12+
* @param {Array} args
13+
* @param {number} t
14+
* @return {Function}
15+
*/
16+
var cancellable = function(fn, args, t) {
17+
fn(...args);
18+
let id = setInterval(function() {
19+
fn(...args);
20+
}, t);
21+
return function() {
22+
clearInterval(id);
23+
}
24+
};
25+
26+
/**
27+
* const result = [];
28+
*
29+
* const fn = (x) => x * 2;
30+
* const args = [4], t = 35, cancelTimeMs = 190;
31+
*
32+
* const start = performance.now();
33+
*
34+
* const log = (...argsArr) => {
35+
* const diff = Math.floor(performance.now() - start);
36+
* result.push({"time": diff, "returned": fn(...argsArr)});
37+
* }
38+
*
39+
* const cancel = cancellable(log, args, t);
40+
*
41+
* setTimeout(cancel, cancelTimeMs);
42+
*
43+
* setTimeout(() => {
44+
* console.log(result); // [
45+
* // {"time":0,"returned":8},
46+
* // {"time":35,"returned":8},
47+
* // {"time":70,"returned":8},
48+
* // {"time":105,"returned":8},
49+
* // {"time":140,"returned":8},
50+
* // {"time":175,"returned":8}
51+
* // ]
52+
* }, cancelTimeMs + t + 15)
53+
*/

3248-snake-in-matrix.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
3248. Snake in Matrix
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 34.90 MB (beats 43.05%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int finalPositionOfSnake(int n, vector<string>& commands) {
13+
int pos = 0;
14+
for (const string& cmd : commands) {
15+
pos += cmd == "UP" ? -n :
16+
cmd == "RIGHT" ? 1 :
17+
cmd == "DOWN" ? n :
18+
cmd == "LEFT" ? -1 : 0;
19+
}
20+
return pos;
21+
}
22+
};

3248-snake-in-matrix.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Snake in Matrix
3+
4+
Submitted: January 17, 2025
5+
6+
Runtime: 3 ms (beats 48.96%)
7+
Memory: 18.03 MB (beats 14.17%)
8+
"""
9+
10+
class Solution:
11+
def finalPositionOfSnake(self, n: int, commands: List[str]) -> int:
12+
return sum(
13+
-n if cmd == "UP" else
14+
1 if cmd == "RIGHT" else
15+
n if cmd == "DOWN" else -1
16+
for cmd in commands
17+
)

0 commit comments

Comments
 (0)