1313. Decompress Run-Length Encoded List
Description:
We are given a list nums
of integers representing a list compressed with run-length encoding.
Consider each adjacent pair of elements [a, b] = [nums[2*i], nums[2*i+1]]
(with i >= 0
). For each such pair, there are a
elements with value b
in the decompressed list.
Return the decompressed list.
Constraints:
2 <= nums.length <= 100
nums.length % 2 == 0
1 <= nums[i] <= 100
Example 1:
Input: nums = [1,2,3,4]
Output: [2,4,4,4]
Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
At the end the concatenation [2] + [4,4,4,4] is [2,4,4,4].
Solutions:
class Solution {
public:
vector<int> decompressRLElist(vector<int>& nums) {
vector<int> decom;
for(int i=0; i < nums.size(); i+=2)
for(int j=0; j < nums[i]; j++)
{
decom.push_back(nums[i+1]);
}
return decom;
}
};
Performance:
1295. Find Numbers with Even Number of Digits
Description:
Given an array nums
of integers, return how many of them contain an even number of digits.
Constraints:
1 <= nums.length <= 500
1 <= nums[i] <= 10^5
Example 1:
Input: nums = [555,901,482,1771]
Output: 1
Explanation:
Only 1771 contains an even number of digits.
Solutions:
class Solution {
public:
int findNumbers(vector<int>& nums) {
int sum = 0;
for(int i = 0; i < nums.size(); i++)
{
int count = 0;
int j = nums[i];
while(j > 0)
{
count += 1;
j = j/10;
}
if(count%2 == 0)
sum += 1;
}
return sum;
}
};
Performance:
1266. Minimum Time Visiting All Points
Description:
On a plane there are n
points with integer coordinates points[i] = [xi, yi]
. Your task is to find the minimum time in seconds to visit all points.
You can move according to the next rules:
- In one second always you can either move vertically, horizontally by one unit or diagonally (it means to move one unit vertically and one unit horizontally in one second).
- You have to visit the points in the same order as they appear in the array.
Constraints:
points.length == n
1 <= n <= 100
points[i].length == 2
-1000 <= points[i][0], points[i][1] <= 1000
Example 1:
Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
Time from [1,1] to [3,4] = 3 seconds
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds
Solutions:
class Solution {
public:
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
int sum = 0;
int deta_x = 0; int deta_y = 0; int min_diff = 0; // int deta_x, int deta_y, min_diff = 0;
for(int i = 0; i < points.size()-1; i++) //Error: i < points.size()
{
deta_x = abs(points[i+1][0]-points[i][0]);
deta_y = abs(points[i+1][1]-points[i][1]);
min_diff = min(deta_x, deta_y);
sum += min_diff;
deta_x - min_diff > 0 ? sum += deta_x - min_diff : sum += deta_y - min_diff;
}
return sum;
}
};
Runtime Error: (caused by out of bound of array)
Line 933: Char 34: runtime error: reference binding to misaligned address 0xbebebebebebebec2 for type ‘value_type’, which requires 4 byte alignment (stl_vector.h)
0xbebebebebebebec2: note: pointer points here
Performance:
118. Pascal's Triangle
Description:
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Solutions:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
vector<int> temp; //initialize vector<vector<int>> result
temp.push_back(1);
for(int i = 0; i < numRows; i++)
result.push_back(temp);
for(int i = 0; i < numRows; i++)
{
for(int j = 1; j <= i; j++)
{
if(j == i)
{
result[i].push_back(1);
break;
}
result[i].push_back(result[i-1][j-1] + result[i-1][j]);
}
}
return result;
}
};
Runtime Error:(forget to initialize vector<vector<int>> result which leads to null pointer :result[i] )
Line 922: Char 34: runtime error: reference binding to null pointer of type 'struct value_type' (stl_vector.h)
Performance: