Giter VIP home page Giter VIP logo

code_challenge_june_7th's Introduction

Sal is an instructor. They want to give some chocolate to the students in their class to reward them for a job well done on a recent test. The students arrange themselves in a line (perhaps alphabetically). Each student has an integer score from 0 to 100 on the test. Sal wants to give at least 1 chocolate to each student. Students get jealous of their immediate neighbors, so of two adjacent students whichever student has a higher score on the test must get one extra piece of chocolate than the student with the lower score. Sal wants to save money, so they wants to minimize the total number of chocolates they must buy.

Restrictions: each student must get at least 1 chocolate you cannot rearrange the students you should allow for up to 100,000,000 students

Examples: test grades: 100, 90, 80 chocolates: 3, 2, 1 total chocolates needed: 6

test grades: 80, 90, 100 chocolates: 1, 2, 3 total chocolates needed: 6

test grades: 100, 80, 90 chocolates: 2, 1, 2 total chocolates needed: 5

test grades: 55, 91, 23, 27, 25, 41, 42, 32, 20, 82, 35, 45, 69, 33, 58 chocolates: 1, 2, 1, 2, 1, 2, 3, 2, 1, 2, 1, 2, 3, 1, 2 total chocolates needed: 26

test grades: 98, 25, 69, 84, 89, 49, 86, 97, 68, 86, chocolates: 2, 1, 2, 3, 4, 1, 2, 3, 1, 2, total chocolates needed: 21

note: because this algorithm has two 'for' loops in it, we're technically looking at our data twice, so for 100M students, this would represent 200M lookups, but when we think of big-O notation we only count one iteration at a code "depth" level. If these were nested for loops (a for loop within a for loop) then we would say this is an n-squared solution, O(n^2) We're also using a lot of extra memory, but this is the tradeoff we make as programmers: do we use memory or do we use CPU, or is there a balance of each. This algorithm uses 3 additional data structures to hold our left/right sequences, plus a final distribution, so we would say our "space complexity" is 4x

code_challenge_june_7th's People

Contributors

sdmalek44 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.