[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

sumHeights2  challenge

Posted on 2016-08-30
7
Medium Priority
?
166 Views
Last Modified: 2016-09-08
Hi,


i am working one below challenge
http://codingbat.com/prob/p157900


i was not clear on below description




(A variation on the sumHeights problem.) We have an array of heights, representing the altitude along a walking trail. Given start/end indexes into the array, return the sum of the changes for a walk beginning at the start index and ending at the end index, however increases in height count double. For example, with the heights {5, 3, 6, 7, 2} and start=2, end=4 yields a sum of 1*2 + 5 = 7. The start end end index will both be valid indexes into the array with start <= end.

how below results are 7,2, 15 each? please advise

sumHeights2([5, 3, 6, 7, 2], 2, 4) → 7
sumHeights2([5, 3, 6, 7, 2], 0, 1) → 2
sumHeights2([5, 3, 6, 7, 2], 0, 4) → 15
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 41777525
indeed, if you have the sumHeights done, this one becomes very similar.
the only difference is that if the value goes "up", you have to double it, hence the simple function "absolute" will not do the job, you need to take the difference and check it's value before going on with the eventual " *2  " before adding the result to the total
0
 
LVL 7

Author Comment

by:gudii9
ID: 41780970
sumHeights  is not clear yet
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41781143
ok, clarified on that other question
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 7

Author Comment

by:gudii9
ID: 41788975
public int sumHeights2(int[] heights, int start, int end) {
 int diff=0;
  int sum=0;
  int absDiff=0;
  for(int i=start; i<end; i++){
    
    diff=heights[i+1]-heights[i];
    if(diff<0){
    absDiff=Math.abs(diff);
    }else{
      absDiff=2*diff;
    }
    sum=sum+absDiff;
  }
  return sum;
}

Open in new window

above passes all tests . Any improvements or alternate approaches?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41789071
looks correct to me.
as mentioned in the other question, you should consider commenting the code
0
 
LVL 32

Expert Comment

by:awking00
ID: 41790239
Alternative method without absolute -
int diff=0;
int sum = 0;
for(int j=start; j<end; j++) {
      diff = (heights[j + 1] > heights[j]) ? 2*(heights[j + 1] - heights[j]) : -(heights[j + 1] - heights[j]);
      sum = sum + diff;
}
return sum;
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790490
: -(heights[j + 1] - heights[j])

Open in new window

i like this trick of multiplying with minus
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question