Skip to content

Instantly share code, notes, and snippets.

@syntaxhacker
Last active August 2, 2019 05:26
Show Gist options
  • Save syntaxhacker/34e31027f873c209381660f82b19e7c2 to your computer and use it in GitHub Desktop.
Save syntaxhacker/34e31027f873c209381660f82b19e7c2 to your computer and use it in GitHub Desktop.
Cp
//getNumberOfWaysToClimbStairs
An O(n) time and O(1) space solution:
private static int getNumberOfWaysToClimbStairs(int n) {
int a = 1, b = 2, c = 4, d = 0;
if (n == 0 || n == 1 || n == 2)
return n;
if (n == 3)
return c;
for (int i = 0; i < n - 3; i++) {
d = c + b + a;
a = b;
b = c;
c = d;
}
return d;
}
// lower case and upper case string fast input
inline void ins(string &s)
{
string i="";
int temp=getchar_unlocked();
while((temp<'a'&& temp>'z') || (temp<'A' && temp>'Z'))
temp=getchar_unlocked();
while((temp>='a'&&temp<='z') || (temp>='A'&&temp<='Z'))
{
i+=(char)temp;
temp=getchar_unlocked();
}
s = i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment