patternjavascriptMinor
Count the number of biggest numbers on the board
Viewed 0 times
thenumbernumbersbiggestcountboard
Problem
I coded two answers to this challenge. The first one was a standard attempt, but after it timed out, I figured out the "trick" to solving the problem much more simply. However, I'd still appreciate comments and critiques on my first attempt, because there will be other problems that will need a long approach and I'd like to improve how I'd code the answer.
Challenge
Julia is playing a game on an infinite 2-dimensional grid with the bottom left cell referenced as (1, 1). All the cells contain a value of zero initially. The game consists of n steps. In each step, Julia is given two integers a and b. The value of each of the cells in the coordinate (u, v) satisfying 1 ≤ u ≤ a and 1 ≤ v ≤ b, is increased by 1. After n such steps, if x is the largest number in any cell on the board, how many instances of x are there on the board?
Complete the function countX that has one parameter, a string array, steps, denoting the values of a and b for each of steps of the game. The function should return the total number of occurrences of greatest integer x in the grid after n steps.
Sample Input and Output *
Input:
Output:
First Attempt
Second Attempt
```
function countX(stepArr){
let sArr = stepArr[0].split(" ");
let smallColA = parseInt(sArr[0]);
let smallColB = parseInt(sArr[1]);
if(sArr.length > 1){
Challenge
Julia is playing a game on an infinite 2-dimensional grid with the bottom left cell referenced as (1, 1). All the cells contain a value of zero initially. The game consists of n steps. In each step, Julia is given two integers a and b. The value of each of the cells in the coordinate (u, v) satisfying 1 ≤ u ≤ a and 1 ≤ v ≤ b, is increased by 1. After n such steps, if x is the largest number in any cell on the board, how many instances of x are there on the board?
Complete the function countX that has one parameter, a string array, steps, denoting the values of a and b for each of steps of the game. The function should return the total number of occurrences of greatest integer x in the grid after n steps.
Sample Input and Output *
Input:
[ '18 29', '32 17', '34 9', '38 15', '36 22', '7 14', '5 100' ]Output:
2First Attempt
function countX(stepArr){
let board = []; //2 dimensional array
let bigNum = 0;
let bigNumCount = 0;
const stepArrCount = stepArr.length;
for(let k = 0; k bigNum){
bigNum = board[i][j];
bigNumCount = 1;
}
else if(board[i][j] === bigNum){
bigNumCount++;
}
else{
stopCompare = true;
}
}
}
}
}
return bigNumCount;
}Second Attempt
```
function countX(stepArr){
let sArr = stepArr[0].split(" ");
let smallColA = parseInt(sArr[0]);
let smallColB = parseInt(sArr[1]);
if(sArr.length > 1){
Solution
The second solution
If the input array is empty, the program will crash with an exception.
It's recommended to specify the radix to
The
the loop condition naturally takes care of that.
Even better, no need to treat the first element specially,
you can initialize
Not only it will be simpler,
but in it will work even when the input array is empty.
The first (naive) solution
You could write this simpler:
... using a technique you already used in your other recent question:
If the input array is empty, the program will crash with an exception.
It's recommended to specify the radix to
parseInt calls:let smallColA = parseInt(sArr[0], 10);The
if(sArr.length > 1){ condition is unnecessary,the loop condition naturally takes care of that.
Even better, no need to treat the first element specially,
you can initialize
smallColA and smallColB to Infinity.Not only it will be simpler,
but in it will work even when the input array is empty.
The first (naive) solution
You could write this simpler:
if(board[i][j] !== undefined){
board[i][j]++;
}
else{
board[i][j] = 1;
}... using a technique you already used in your other recent question:
board[i][j] = (board[i][j] || 0) + 1;Code Snippets
let smallColA = parseInt(sArr[0], 10);if(board[i][j] !== undefined){
board[i][j]++;
}
else{
board[i][j] = 1;
}board[i][j] = (board[i][j] || 0) + 1;Context
StackExchange Code Review Q#148920, answer score: 2
Revisions (0)
No revisions yet.