@Tanych said in Why my code catch the TypeError: unsupported operand type(s) for +: 'int' and 'NoneType':
print a,n,b,m,k
findkth doesn't return any value for some cases, where it recursively calls itself.
@Tanych said in Why my code catch the TypeError: unsupported operand type(s) for +: 'int' and 'NoneType':
print a,n,b,m,k
findkth doesn't return any value for some cases, where it recursively calls itself.
class Solution {
public:
int minMoves(vector<int>& nums) {
if (nums.size() <= 1)
return 0;
sort(nums.begin(), nums.end(), greater<int>());
int sum = 0;
int factor = 1;
for (int i = 0; i < nums.size() - 1; ++i, ++factor){
sum += factor * (nums[i] - nums[i + 1]);
}
return sum;
}
};
pop() is not O(1) since it has to traverse the list looking for the new minimum.
TrieNode* node = children[s[i]-'a'];
if (node == NULL) {
node = new TrieNode();
}
node->insert(s, i+1);
You never update children[s[i]-'a']
with the newly allocated node object.
Both containsKey and containsValue can't be O(1), unless internally HashMap stores two mapping tables.
This is O(n^2) due to the nested for loop.