HackerRank: Staircase

Thank you for visiting this post! Be sure to also take a look at my other work on LinkedIn, Github, and my website.

Picture this scenario for me. You move into this AMAZING house. It’s great, it’s beautiful, it’s…wait is that a staircase?

Oh, no. You’ve never walked on stairs before….why? You never got around to learning it — shhhh just go with it.

Anywho, let’s learn how to conquer stairs via code!

Problem

Please click the link below to view the Problem Statement

Solution

I setup variables and counters for both the spaces and the string element they want us to use. Then I setup a basic for loop: the spaceCount would decrease and the hashTagCount would increase every time it looped through n.

Pretty simple, right? Is there a faster way we can do this though?

SO glad you asked!

My colleague from my study group, Genius Ben we call him, came up with this SUPER amazing solution.

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

-MDN Web Docs

Check it out:

Using padStart(), he sets the padding to the amount of n , which is 6, right from the start. He then sets his conditional to loop until “i” is less than or equal to n(6), and every time it repeats, it list’s a ‘#’ in place of the space.

WHAT!? I didn’t even know that padStart() was a thing. That’s the amazing thing about being a developer. You NEVER stop learning!

Conclusion

Thank you so much for stopping by! Starting next Monday, I will begin learning Java. Stay tuned for more Algorithm Content. Java Style!

Please checkout my online portfolio and feel free to connect with me on Linkedin!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Nginx, uWSGI and Docker to deploy your Flask application.

Do You Wanna Build a Startup ? *Frozen song

New Orchestrator Features of JD Edwards EnterpriseOne 9.2.3.3

Understanding Files

Should I use delete or truncate to remove all rows in BigQuery ???

Day 1 Swift 基礎 宣告、型別、列陣整理&運用練習

Pinknode 1.0 is now LIVE — a sleek UI for managing your infra

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Stephanie Segura

Stephanie Segura

More from Medium

5 Steps to Grind LeetCode Effectively with Spaced Repetition and Deliberate Exercise — Wee Hong KOH

[LeetCode] (Easy) 70. Climbing Stairs

Open Source Round 1 at Encora Apprenticeship — Week 1

ARTS Week 28