From 46a0ca189deca24c332329ddb5fd0b4becdfda4f Mon Sep 17 00:00:00 2001 From: dab07 Date: Thu, 20 Oct 2022 16:11:16 +0530 Subject: [PATCH] add LeetCode solution --- src/java/climbing_stairs.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/java/climbing_stairs.java diff --git a/src/java/climbing_stairs.java b/src/java/climbing_stairs.java new file mode 100644 index 0000000..0744aaa --- /dev/null +++ b/src/java/climbing_stairs.java @@ -0,0 +1,33 @@ +/* +Question: You are climbing a staircase. It takes n steps to reach the top. +Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? + +Example 1: + Input: n = 2 + Output: 2 + Explanation: There are two ways to climb to the top. + 1. 1 step + 1 step + 2. 2 steps + */ + +//Solution +class Solution { + public int climbStairs(int n) { + if (n == 1) { + return 1; + } + int a = 1; + int b = 1; + int count = 0; + while (n > 1) { + count = a + b; + int temp = b; + b = count; + a = temp; + n--; + } + return count; + } +} + +//Submission link: https://leetcode.com/submissions/detail/703917952/