Skip to content

Commit fc93ffc

Browse files
committed
add 77, 191, 704, 1351, 2724; update 1, 146, 191, 2469
1 parent 0b06dd4 commit fc93ffc

10 files changed

+168
-14
lines changed

0001-two-sum.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class Solution:
1111
def twoSum(self, nums: List[int], target: int) -> List[int]:
1212
d = {}
1313
return next(x for x in (
14-
(i, d[target - nums[i]]) if ((target - nums[i]) in d) else d.update({ nums[i]: i })
14+
(i, d[target - nums[i]])
15+
if ((target - nums[i]) in d)
16+
else d.__setitem__(nums[i], i)
1517
for i in range(len(nums))
1618
) if x is not None
1719
)

0077-combinations.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
77. Combinations
3+
4+
Submitted: January 29, 2025
5+
6+
Runtime: 23 ms (beats 97.78%)
7+
Memory: 56.92 MB (beats 98.34%)
8+
"""
9+
10+
from itertools import combinations
11+
12+
class Solution:
13+
def combine(self, n: int, k: int) -> List[List[int]]:
14+
return list(combinations(range(1, n + 1), k))

0146-lru-cache.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
146. LRU Cache
3+
4+
Submitted: January 24, 2025
5+
6+
Runtime: 116 ms (beats 82.52%)
7+
Memory: 78.04 MB (beats 88.47%)
8+
"""
9+
10+
from collections import OrderedDict
11+
12+
class LRUCache(OrderedDict):
13+
14+
def __init__(self, capacity: int):
15+
self.capacity = capacity
16+
17+
def get(self, key: int) -> int:
18+
if key in self:
19+
self.move_to_end(key, last=True)
20+
return self[key]
21+
else:
22+
return -1
23+
24+
def put(self, key: int, value: int) -> None:
25+
if key in self: self.move_to_end(key, last=True)
26+
self[key] = value
27+
while len(self) > self.capacity:
28+
self.popitem(last=False)
29+
30+
# Your LRUCache object will be instantiated and called as such:
31+
# obj = LRUCache(capacity)
32+
# param_1 = obj.get(key)
33+
# obj.put(key,value)

0191-number-of-1-bits.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
/*
22
191. Number of 1 Bits
33
4-
First submitted: October 13, 2024
5-
This solution: October 14, 2024
4+
Submitted: January 25, 2025
65
76
Runtime: 0 ms (beats 100.00%)
8-
Memory: 7.90 MB (beats 76.22%)
7+
Memory: 8.26 MB (beats 47.54%)
98
*/
109

1110
class Solution {
1211
public:
13-
int hammingWeight(int n) {
14-
int res = 0;
15-
while (n != 0) {
16-
res += n & 1;
17-
n >>= 1;
18-
}
19-
return res;
12+
inline int hammingWeight(int n) {
13+
return __builtin_popcount(n);
2014
}
2115
};

0704-binary-search.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
704. Binary Search
3+
4+
Submitted: January 31, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 9.40 MB (beats 30.03%)
8+
*/
9+
10+
int search(int* nums, int numsSize, int target) {
11+
int min = 0, max = numsSize - 1;
12+
int mid;
13+
while (min <= max) {
14+
mid = (max + min) / 2;
15+
if (nums[mid] == target) return mid;
16+
else if (nums[mid] < target) min = mid + 1;
17+
else if (nums[mid] > target) max = mid - 1;
18+
}
19+
return -1;
20+
}

0704-binary-search.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
704. Binary Search
3+
4+
Submitted: January 31, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 31.24 MB (beats 79.81%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int search(vector<int>& nums, int target) {
13+
int min = 0, max = nums.size() - 1;
14+
int mid;
15+
auto arr = nums.data();
16+
while (min <= max) {
17+
mid = (max + min) / 2;
18+
if (arr[mid] == target) return mid;
19+
else if (arr[mid] < target) min = mid + 1;
20+
else if (arr[mid] > target) max = mid - 1;
21+
}
22+
return -1;
23+
}
24+
};

0704-binary-search.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
704. Binary Search
3+
4+
Submitted: January 31, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 18.59 MB (beats 82.38%)
8+
"""
9+
10+
class Solution:
11+
def search(self, nums: List[int], target: int) -> int:
12+
min = 0
13+
max = len(nums) - 1
14+
while min <= max:
15+
mid = (max + min) // 2
16+
if nums[mid] == target: return mid
17+
elif nums[mid] < target: min = mid + 1
18+
elif nums[mid] > target: max = mid - 1
19+
return -1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
1351. Count Negative Numbers in a Sorted Matrix
3+
4+
Submitted: February 6, 2025
5+
6+
Runtime: 4 ms (beats 12.26%)
7+
Memory: 19.05 MB (beats 13.05%)
8+
"""
9+
10+
class Solution:
11+
def countNegatives(self, grid: List[List[int]]) -> int:
12+
return sum(sum(cell < 0 for cell in row) for row in grid)

2469-convert-the-temperature.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
44
Submitted: October 3, 2024
55
6-
Runtime: 38 ms (beats 100.00%)
7-
Memory: 16.68 MB (beats 37.35%)
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.94 MB (beats 16.66%)
88
"""
99

1010
class Solution:
1111
def convertTemperature(self, celsius: float) -> List[float]:
12-
return [celsius + 273.15, celsius * 1.80 + 32.00]
12+
return celsius + 273.15, celsius * 1.80 + 32.00

2724-sort-by.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
2724. Sort By
3+
4+
Submitted: February 6, 2025
5+
6+
Runtime: 506 MB (beats 5.62%)
7+
Memory: 70.77 MB (beats 5.12%)
8+
*/
9+
10+
/**
11+
* @param {Array} arr
12+
* @param {Function} fn
13+
* @return {Array}
14+
*/
15+
function sortBy(arr, fn) {
16+
if (arr.length <= 1) return arr;
17+
var left = arr.slice(0, arr.length / 2);
18+
var right = arr.slice(arr.length / 2);
19+
return merge(sortBy(left, fn), sortBy(right, fn), fn);
20+
};
21+
22+
function merge(left, right, fn) {
23+
let res = [];
24+
while (left.length && right.length) {
25+
if (fn(left[0]) > fn(right[0])) {
26+
res.push(right[0]);
27+
right = right.slice(1);
28+
} else {
29+
res.push(left[0]);
30+
left = left.slice(1);
31+
}
32+
}
33+
if (left.length) res = res.concat(left);
34+
else res = res.concat(right);
35+
return res;
36+
}

0 commit comments

Comments
 (0)