Leetcode practice  0.0
Public Member Functions | List of all members
Dynamic_Array_Exercise Class Reference

#include <leetcode_practice.h>

Public Member Functions

int pivot (vector< int > &nums)
 Find the pivot index - Easy. More...
 
int dominantIndex (vector< int > &nums)
 Find the index of largest number At Least Twice of Others - Easy. More...
 
vector< int > plusOne (vector< int > &nums)
 Plus one the last position - Easy. More...
 
vector< int > findDiagonalOrder (vector< vector< int >> &matrix)
 Diagonal Order - Medium. More...
 
vector< int > spiralMatrix (vector< vector< int >> &matrix)
 Print Spiral Matrix - Medium. More...
 
vector< vector< int > > pachiraTringle (int numRows)
 Print Pascal's triangle - Easy. More...
 
int numEquivDominoPairs (vector< vector< int >> &dominoes)
 Number of Equivalent dominoes Pairs - Easy. More...
 
int numEquivDominoPairs_opt (vector< vector< int >> &dominoes)
 Optimized Number of Equivalent dominoes Pairs - Easy. More...
 

Member Function Documentation

◆ dominantIndex()

int Dynamic_Array_Exercise::dominantIndex ( vector< int > &  nums)

Find the index of largest number At Least Twice of Others - Easy.

Parameters
Thetest case's vector
Returns
The index of largest number

Ideal:

  1. Find the index of max value
  2. Determine whether the largest number is at least twice of others and avoid to scan on the current index
117 {
118  int maxIndex = 0;
119 
120  // Find the index of max value
121  for (int i = 0; i < nums.size(); ++ i)
122  if (nums[i] < nums[maxIndex])
123  maxIndex = i;
124 
125  // Fail situation, otherwise the index of largest number is at least twice of others
126  for (int j = 0; j < nums.size(); ++ j) {
127  if (nums[maxIndex] < 2 * nums[j] && maxIndex != j)
128  return -1;
129  }
130 
131  return maxIndex;
132 }

Referenced by leetcode_practice().

◆ findDiagonalOrder()

vector< int > Dynamic_Array_Exercise::findDiagonalOrder ( vector< vector< int >> &  matrix)

Diagonal Order - Medium.

Parameters
Thetest case's matrix
Returns
The diagonal order's matrix

Ideal:

  1. Using the direction map to indicate the route's direction
  2. Avoid out-of-range
  3. Storing the route

Detailed description starts here. [0, 0] -> [-1, 1] -> [0, 1] -> [1, 0] -> [2, -1] -> [2, 0] -> ...

180 {
181  vector<int> result;
182 
183  if(matrix.size() == 0 || matrix[0].size() == 0)
184  return result;
185 
186  int m = matrix.size(); // rows
187  int n = matrix[0].size(); // cols
188 
189  // Direction map
190  vector<vector<int>> dirs = {{-1, 1}, // up direction
191  {1, -1}}; // dowm direction
192 
193  int d = 0; int row = 0; int col = 0;
194 
195  for (int i = 0; i < n*m; i ++){
196  // Put the value.
197  result.push_back(matrix[row][col]);
198 
199  // Each step
200  row += dirs[d][0];
201  col += dirs[d][1];
202 
203  // Avoid out-of-range
204  if (row >= m) {
205  row = m - 1;
206  col += 2;
207  d = 1 - d; // 0 or 1
208  }
209 
210  // Avoid out-of-range
211  if (col >= n) {
212  col = n - 1;
213  row += 2;
214  d = 1 - d;
215  }
216 
217  // avoid out-of-range
218  if (row < 0) {
219  row = 0;
220  d = 1 - d;
221  }
222 
223  // avoid out-of-range
224  if (col < 0) {
225  col = 0;
226  d = 1 - d;
227  }
228  }
229  return result;
230 }

Referenced by leetcode_practice().

◆ numEquivDominoPairs()

int Dynamic_Array_Exercise::numEquivDominoPairs ( vector< vector< int >> &  dominoes)

Number of Equivalent dominoes Pairs - Easy.

Parameters
The2-D dominoes vector
Returns
The number of pairs (i, j)

Complexity: O(N^2)

Ideal:

  1. Search the pairs one-by-one

Test case: Input: dominoes = [[1,2],[2,1],[3,4],[5,6]] Output: 1

350 {
351  int i, j;
352  int result = 0;
353 
354  for(j = 0; j < dominoes.size() - 1 ; ++ j) {
355  for (i = j + 1; i < dominoes.size() ; ++ i) {
356  result += (dominoes[j][0] == dominoes[i][0] && dominoes[j][1] == dominoes[i][1]) ||
357  (dominoes[j][0] == dominoes[i][1] && dominoes[j][1] == dominoes[i][0]);
358  }
359  }
360 
361  return result;
362 }

◆ numEquivDominoPairs_opt()

int Dynamic_Array_Exercise::numEquivDominoPairs_opt ( vector< vector< int >> &  dominoes)

Optimized Number of Equivalent dominoes Pairs - Easy.

Parameters
The2-D dominoes vector
Returns
The number of pairs (i, j)

Complexity: O(N)

Ideal:

  1. Using hash map and sorting the pairs
  2. Count the number of equivalent pairs
  3. Calculate the total of the number of equivalent pairs

Test case: Input: dominoes = [[1,2],[2,1],[3,4],[5,6]] Output: 1

381 {
382  int result = 0;
383 
384  // hash map (key, value)
385  map< pair < int, int >, int > dict;
386 
387  for(int i=0; i<dominoes.size(); i++){
388 
389  int a = dominoes[i][0], b = dominoes[i][1];
390 
391  // Sorting
392  two_int key = {min(a, b), max(a, b)};
393 
394  // Count the same pair
395  dict[key] += 1;
396  }
397 
398  // Search hash map and calculate the arithmetic series
399  for(auto it = dict.begin(); it != dict.end(); it++){
400  int value = it->second;
401  result += value * (value-1) / 2;
402  }
403  return result;
404 }
pair< int, int > two_int
Definition: leetcode_practice.cpp:364

Referenced by leetcode_practice().

◆ pachiraTringle()

vector< vector< int > > Dynamic_Array_Exercise::pachiraTringle ( int  numRows)

Print Pascal's triangle - Easy.

Parameters
Therow of Pascal's triangle
Returns
The last row of Pascal's triangle

Ideal:

  1. Create first and second layer of Pascal's triangle
  2. Declare new vector and storing the sum of the two numbers on the previous row

{[1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1], ...}

300 {
301  vector<vector<int>> result;
302 
303  if(numRows == 0)
304  return result;
305 
306  vector<int> temp;
307 
308  // first layer
309  temp.push_back(1);
310  result.push_back(temp);
311 
312  if (numRows == 1)
313  return result;
314 
315  // second layer
316  temp.push_back(1);
317  result.push_back(temp);
318 
319  if (numRows == 2)
320  return result;
321 
322  // Others
323  for (int i = 2; i < numRows; i ++) {
324  // default value of the last Pascal's triangle is 1
325  vector<int> c(i+1, 1);
326 
327  // sum of the two numbers on the previous row (total i - 1 times)
328  for (int j = 1; j < i; j ++) {
329  c[j] = result[i-1][j-1] + result[i-1][j];
330  }
331  result.push_back(c);
332  }
333  return result;
334 }

Referenced by leetcode_practice().

◆ pivot()

int Dynamic_Array_Exercise::pivot ( vector< int > &  nums)

Find the pivot index - Easy.

Parameters
Thetest case's vector
Returns
The pivot index

Ideal:

  1. Find sum of all elements
  2. Calculate left sum (sum - pre_sum - current elements) and
  3. Determine whether the right sum is the same as the left sum
90 {
91  long sum = 0;
92  int left_sum = 0;
93 
94  // Find sum of all elements
95  for (int n: nums)
96  sum += n;
97 
98  // Compare left sum with the right sum
99  for(int i = 0; i < nums.size(); i ++) {
100  if(left_sum == sum - left_sum - nums[i])
101  return i;
102  left_sum += nums[i];
103  }
104  return -1;
105 }

Referenced by leetcode_practice().

◆ plusOne()

vector< int > Dynamic_Array_Exercise::plusOne ( vector< int > &  nums)

Plus one the last position - Easy.

Parameters
Thetest case's vector
Returns
The result of plus one's matrix

Ideal:

  1. Plus one on the last value and distinguish whether it need to carry

Test case: Input: [1,2,3] Output: [1,2,4]

146 {
147  int i;
148 
149  for(i = nums.size() - 1; i >= 0 ; i--) {
150  // Plus one when last value is not equal to 9, otherwise carrying
151  if (nums[i] != 9) {
152  nums[i] ++;
153  return nums;
154  } else // Carry
155  nums[i] = 0;
156  }
157 
158  // Overflow
159  if (i < 0) {
160  // insert element with value 1 at 1th position in vector
161  nums.insert(nums.begin(), 1);
162  }
163 
164  return nums;
165 }

Referenced by leetcode_practice().

◆ spiralMatrix()

vector< int > Dynamic_Array_Exercise::spiralMatrix ( vector< vector< int >> &  matrix)

Print Spiral Matrix - Medium.

Parameters
Thetest case's matrix
Returns
The spiral matrix

Ideal:

  1. Route's direction
  • first row (left to right), last column (up to down),
  • last row (right to left), first column (down to up)
242 {
243  vector<int> result;
244 
245  // Protecting
246  if(matrix.size() == 0 || matrix[0].size() == 0)
247  return result;
248 
249  int i = 0;
250  int col = 0; // left to right
251  int row = 0; // up to down
252  int m = matrix.size(); // down to up (rows)
253  int n = matrix[0].size(); // right to left (columns)
254 
255  // be careful with heap-buffer-overflow issue
256  while (row < m && col < n ) {
257  // first row (left to right)
258  for (i = col ; i < n; ++ i) {
259  result.push_back(matrix[row][i]);
260  }
261 
262  // last column (up to down)
263  for (i = row + 1 ; i < m ; ++ i)
264  result.push_back(matrix[i][n - 1]);
265 
266  // last row (right to left) and avoid repetition
267  if ((m - 1) != row) {
268  for (i = n - 2 ; i >= row ; -- i) {
269  result.push_back(matrix[m - 1][i]);
270  }
271  }
272 
273  // first column (down to up) and avoid repetition
274  if ((n - 1) != col) {
275  for (i = m - 2 ; i > col; -- i) {
276  result.push_back(matrix[i][col]);
277  }
278  }
279 
280  // indicate the route's direction
281  ++ row;
282  ++ col;
283  -- m;
284  -- n;
285  }
286  return result;
287 }

Referenced by leetcode_practice().


The documentation for this class was generated from the following files: