TODO:
heapq.nlargest(), itertools.islice()mpremote cp secrets.py :secrets.pympremote mount .class Solution:
def calculateScore(self, a: int, b: int) -> int:
def count_set_bits(n: int) -> int:
n_str = bin(n)[2:]
return sum(int(c) for c in n_str)
s1 = count_set_bits(a)
s2 = count_set_bits(b)
if s2 <= s1:
return s1 * s2
return s2 // s1class Solution:
def calculateScore(self, a: int, b: int) -> int:
s1 = a.bit_count()
s2 = b.bit_count()
if s2 <= s1:
return s1 * s2
return s2 // s1class Solution:
def maximizeSum(self, nums: list[int], k: int) -> int:
improvements = []
for num in nums:
max_num = int("".join(sorted(str(num), reverse=True)))
improvements.append(max_num - num)
improvements.sort(reverse=True)
return sum(nums) + sum(improvements[:k])import heapq
class Solution:
def maximizeSum(self, nums: list[int], k: int) -> int:
improvements = []
for num in nums:
max_num = int("".join(sorted(str(num), reverse=True)))
improvements.append(max_num - num)
best_improvements = heapq.nlargest(k, improvements)
return sum(nums) + sum(best_improvements)bin()mpremote run button.py,
mpremote cp button.py :main.py,
mpremote rm :main.pyclass Solution:
def findColoredRoot(self, edges: list[list[int]], colors: str) -> int:
next_color = {"R": "B", "B": "W", "W": "R"}
in_degree = [0] * len(colors)
out_degree = [0] * len(colors)
for u, v in edges:
if next_color[colors[u]] == colors[v]:
parent, child = u, v
elif next_color[colors[v]] == colors[u]:
parent, child = v, u
else: # same color on both ends
return -1
in_degree[child] += 1
out_degree[parent] += 1
if in_degree[child] > 1 or out_degree[parent] > 2:
return -1
return in_degree.index(0)I’ve been mulling over what ot get myself for my birthday. I was thinking of getting Garmin Fenix 9 Pro Solar, Fitbit Air, or Fitbit Edge, but I decided to get some new keyboards instead.
str.lower(). I don’t use it
everyday.std::isalnum() & std::tolower()pattern_to_wordsterminal, nonterminal,
rule(production), start symbol% will be evaluated before +std::isalnum(), std::tolower()#include <cctype>
#include <cstddef>
#include <string>
class Solution {
public:
bool isPalindrome(std::string s) {
std::size_t left = 0;
std::size_t right = s.size() - 1;
while (left < right) {
if (!std::isalnum(s[left])) {
++left;
continue;
}
if (!std::isalnum(s[right])) {
--right;
continue;
}
if (std::tolower(s[left]) != std::tolower(s[right])) {
return false;
}
++left;
--right;
}
return true;
}
};I’m cold… sleepy…
class Solution {
public:
bool isPalindrome(int x) {
int original = x;
long long reversed = 0;
while (0 < x) {
reversed = reversed * 10 + (x % 10);
x /= 10;
}
return reversed == original;
}
};class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) { return false; }
int num_digits = 0;
int x_copy = x;
while (0 < x_copy) {
x_copy /= 10;
++num_digits;
}
int reversed_last = 0;
for (int i = 0; i < num_digits / 2; ++i) {
reversed_last = reversed_last * 10 + (x % 10);
x /= 10;
}
if (num_digits % 2 != 0) {
x /= 10;
}
return reversed_last == x;
}
};class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) { return false; }
// Without this, leading zeros will be discarded incorrectly
// in the following operations
if (x != 0 && x % 10 == 0) { return false; }
int reversed_last = 0;
while (reversed_last < x) {
reversed_last = reversed_last * 10 + (x % 10);
x /= 10;
}
return reversed_last == x || reversed_last / 10 == x;
}
};#include <cstddef>, std::size_t,
.size() vs .length()#include <cstddef>
#include <string>
class Solution {
public:
bool isPalindrome(int x) {
std::string s = std::to_string(x);
std::size_t left = 0;
std::size_t right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) { return false; }
++left;
--right;
}
return true;
}
};#include <algorithm>
#include <vector>
class Solution {
public:
int missingNumber(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] != i) { return i; }
}
return static_cast<int>(nums.size());
}
};#include <vector>
#include <numeric>
class Solution {
public:
int missingNumber(std::vector<int>& nums) {
int total = std::accumulate(nums.begin(), nums.end(), 0);
int n = static_cast<int>(nums.size());
int expected = (n * (n + 1)) / 2;
return expected - total;
}
};I went on a date with my wife at Valley Fair Mall
string.ascii_lowercase,
pattern matching#include <algorithm>, std::max(),
backwards, static_cast<int>()#include <string>
class Solution {
public:
int lengthOfLastWord(std::string s) {
int i = static_cast<int>(s.size() - 1);
while (0 <= i && s[i] == ' ') { --i; }
int length = 0;
while (0 <= i && s[i] != ' ') {
--i;
++length;
}
return length;
}
};My wife passed all 4 exams of CPA! Congratulations! Now I’ll be a stay-at-home-husband…
I felt a little envious when someone else in my study group got a job at a Big Tech company, but on second thought, health is what matters most, and I have a lovely wife.
Today was the most relaxing day I’ve had in a while.
I ate too much in the last 3 days. will try to go back to the normal state
class Solution {
public:
bool isPowerOfFour(int n) {
if (n <= 0) { return false; }
if (n == 1) { return true; }
if (n % 4 != 0) { return false; }
return isPowerOfFour(n / 4);
}
};math.ceil()class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) { return false; }
while (1 < n) {
if (n % 2 == 1) { return false; }
n = n / 2; // n /= 2;
}
return true;
}
};class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= 0) { return false; }
return (n & (n - 1)) == 0;
}
};class Solution {
public:
bool isPowerOfFour(int n) {
const int MASK = 0x55555555;
if (n <= 0) { return false; }
if ((n & MASK) != n) { return false; }
return (n & (n - 1)) == 0;
}
};