Monday, April 21, 2025

509. Fibonacci Number ---E

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.

Given n, calculate F(n).

 

Example 1:

Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

 

Constraints:

  • 0 <= n <= 30

A:

class Solution {
public:
int fib(int n) {
if (n <=1)
return n;
int fn1= 1, fn2=0;
int fn=0;
for(int i = 1; i<n;i++){
fn = fn1 + fn2;
fn2= fn1;
fn1=fn;
}
return fn;
}
};


Sunday, April 20, 2025

491. Non-decreasing Subsequences ---M !!!!

 Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

 

Example 1:

Input: nums = [4,6,7,7]
Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

Example 2:

Input: nums = [4,4,3,2,1]
Output: [[4,4]]

 

Constraints:

  • 1 <= nums.length <= 15
  • -100 <= nums[i] <= 100

A :

一开始,想每次插入,记录之前的最后一个值的找到的个数。 然而还是不够,需要记录更多。


第二个思路, 用set来记录已经有过了的。但是需要把 vector<int> 转成string 来做hash。 就是有点儿麻烦,转来转去的。

 in C++, you can use set<vector<int>> ans;it works correctly as long as you include the necessary headers and use std::vector elements that are comparable.

unordered_set<vector<int>> ans; will NOT work by default in C++ ❌ — because std::unordered_set requires a hash function for its key type, and std::vector<int> does not have a built-in std::hash.



因此如果是python,就简单太多了

class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
ans = set()
def sol(path, remaining):
if len(path) > 1 and path == sorted(path):
ans.add(tuple(path))
for i in range(len(remaining)):
sol(path + [remaining[i]], remaining[i+1:])
sol([], nums)
return sorted(ans)

第三个思路: 核心困难点就在于重复的值。 首先找到所有不重复的。再插入把重复的值val,比如有n次,就替换为 1,2,3.。。n次   但是这个也会有问题。 算了,,看答案吧。 TNND

class Solution {
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
set<vector<int>> S;
vector<int> pre;
helper(nums, 0, pre, S);
return vector(S.begin(), S.end());
}
private:
void helper(vector<int>& nums, int start, vector<int>& pre, set<vector<int>>& S ){
int n = nums.size();
if(start >= n){
if(pre.size() >= 2){
S.insert(pre);
}
return;
}
// do not use
helper(nums, start+1, pre, S);
// consider to use this value
if(pre.size() == 0 || pre.back()<= nums[start]){
pre.push_back(nums[start]);
helper(nums, start+1, pre, S);
pre.pop_back();
}
}
};


Learned:

    unordered_set<vector<int>> S;

这样是不行的,因为 vector<int> 没有一个确定的hash函数。

thus we have to either define our own Hashable , 但是可以用set<vector<int>>


Thursday, April 17, 2025

1079. Letter Tile Possibilities ------M

 You have n  tiles, where each tile has one letter tiles[i] printed on it.

Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.

 

Example 1:

Input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".

Example 2:

Input: tiles = "AAABBC"
Output: 188

Example 3:

Input: tiles = "V"
Output: 1

 

Constraints:

  • 1 <= tiles.length <= 7
  • tiles consists of uppercase English letters.


A:

就还是递归。

class Solution {
public:
int numTilePossibilities(string tiles) {
int n = tiles.length();
vector<int> C(26, 0);
for (auto ch : tiles) {
C[ch - 'A'] += 1;
}
int res = 0;
for (int c = 1; c <= n; c++) {
res += helper(C, 0, c);
}
return res;
}

private:
int helper(vector<int>& C, int start, int len) {
if (len < 0)
return 0;
if (start >= C.size())
return len == 0 ? 1 : 0;
int res = 0;
for (int used = 0; used <= C[start]; used++) {
auto nextRes = helper(C, start + 1, len - used);
res += getC_n_k(len, used) * nextRes;
}
return res;
}
int getC_n_k(int n, int k) {
// k >=0 n >=k
int res = 1;
if (k <= 0)
return 1;
for (int i = 0; i < k; i++) {
res *= (n - i);
}
for (int i = 0; i < k; i++) {
res /= (k - i);
}
return res;
}
};

果然是刷题要看状态。 昨天晚上凌搞错了。一时间也没搞明白怎么改。
就算了去睡觉了。也根本没再思考。 结果醒来,直接看到了问题。 一到一分钟,就改好了。

核心问题在于结束的状态。需要返回0 而不是1


401. Binary Watch -----M !

A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.

  • For example, the below binary watch reads "4:51".

Given an integer turnedOn which represents the number of LEDs that are currently on (ignoring the PM), return all possible times the watch could represent. You may return the answer in any order.

The hour must not contain a leading zero.

  • For example, "01:00" is not valid. It should be "1:00".

The minute must consist of two digits and may contain a leading zero.

  • For example, "10:2" is not valid. It should be "10:02".

 

Example 1:

Input: turnedOn = 1
Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]

Example 2:

Input: turnedOn = 9
Output: []

 

Constraints:

  • 0 <= turnedOn <= 10


A: 

就是实现题。   考的是细心。 值得再做

主要是实现各种permutation。


class Solution {
public:
vector<string> readBinaryWatch(int turnedOn) {
vector<string> res;
vector<int> M{32, 16, 8, 4, 2, 1};
for (int lenH = 0; lenH <= turnedOn; lenH++) {
if (lenH >= 4)
continue;
auto lenM = turnedOn - lenH;
if (lenM >= 6)
continue;
auto resH = getPermH(lenH);
auto resM = getPermM(M, 0, 0, lenM);
for (auto h : resH) {
for (auto m : resM) {
res.push_back(h + ":" + m);
}
}
}
return res;
}

private:
vector<string> getPermH(int len) {
vector<int> H{8, 4, 2, 1};
if (len == 0)
return {"0"};
if (len == 1) {
return {"1", "2", "4", "8"};
}
if (len == 2) {
return {"3", "5", "6", "10", "9"};
}
if (len == 3) {
return {"7", "11"};
}
return {};
}
vector<string> getPermM(vector<int>& M, int start, int preSum, int len) {
if(len<0 || start >= M.size())
return {};
if (len == 0) {
return {"00"};
}
vector<string> res;
if (len == 1) {
for (int i = start; i < M.size(); i++) {
if (M[i] + preSum < 60){
auto str = to_string(M[i] + preSum);
if(str.length() == 1)
str = "0"+str;
res.push_back(str);
}
}
return res;
}else{
res = getPermM(M, start + 1, preSum, len); // do not use this value
auto res2 = getPermM(M, start + 1, preSum + M[start], len - 1);
res.insert(res.end(), res2.begin(), res2.end());
}
return res;
}
};