🗓 Daily LeetCode Progress – Day 16

Problems Solved:

#118 Pascal’s Triangle
#66 Plus One

This continues my daily series (Day 16: Constructive Row Generation + Carry Handling). Today I explored how to build Pascal’s Triangle row by row using previous results and how to handl…


This content originally appeared on DEV Community and was authored by Ertugrul

Problems Solved:

  • #118 Pascal's Triangle
  • #66 Plus One

This continues my daily series (Day 16: Constructive Row Generation + Carry Handling). Today I explored how to build Pascal's Triangle row by row using previous results and how to handle digit addition with proper carry management.

đź’ˇ What I Learned

  • For Pascal's Triangle, dynamic row generation is the key. Each new row starts and ends with 1, while the middle values come from the sum of two numbers directly above. This can be done in O(numRows²) time without extra complexity.
  • For Plus One, directly converting digits into an integer can cause overflow in C++, so the robust way is to simulate addition with a carry. This ensures correctness for arbitrarily large digit arrays.
  • Both problems demonstrate how iterative construction with local state (row or carry) can elegantly solve array-based challenges.

🧩 #118 — Pascal's Triangle

Python (Row Construction)

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        result = [[1]]

        for i in range(1, numRows):
            prev = result[-1]
            row = [1]
            for j in range(1, len(prev)):
                row.append(prev[j-1] + prev[j])
            row.append(1)
            result.append(row)

        return result

C++ (Row Construction)

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> result;
        result.push_back({1});

        for (int i = 1; i < numRows; i++) {
            vector<int> prev = result.back();
            vector<int> row;
            row.push_back(1);

            for (int j = 1; j < prev.size(); j++) {
                row.push_back(prev[j-1] + prev[j]);
            }

            row.push_back(1);
            result.push_back(row);
        }

        return result;
    }
};

Why it works: each row is fully determined by the row before, so we can build the triangle iteratively.

Time: O(numRows²)
Space: O(numRows²)

🧩 #66 — Plus One

Python (String Conversion)

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        new_int = int(''.join(map(str, digits)))
        new_int += 1
        liste = [int(x) for x in str(new_int)]
        return liste

C++ (Carry Simulation)

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int n = digits.size();
        int carry = 1; // start with +1

        for (int i = n - 1; i >= 0; i--) {
            int sum = digits[i] + carry;
            digits[i] = sum % 10;
            carry = sum / 10;
        }

        if (carry > 0) {
            digits.insert(digits.begin(), carry);
        }

        return digits;
    }
};

Why it works: simulating addition with carry prevents integer overflow and works even for very large arrays.

Time: O(n)
Space: O(1) (in-place modification)

📸 Achievements

  • Pascal's Triangle (Python & C++)

pascal py

pascal cpp

  • Plus One (Python & C++)

plusone py

plusone cpp

📦 Complexity Recap

  • Pascal's Triangle: quadratic in number of rows.
  • Plus One: linear in digit length, constant extra space.

📣 Join the Journey

I’m solving and documenting problems daily in both Python and C++, covering arrays, linked lists, dynamic programming, and more. Follow along if you’re interested in systematic problem solving.

Links


This content originally appeared on DEV Community and was authored by Ertugrul


Print Share Comment Cite Upload Translate Updates
APA

Ertugrul | Sciencx (2025-09-01T10:33:00+00:00) 🗓 Daily LeetCode Progress – Day 16. Retrieved from https://www.scien.cx/2025/09/01/%f0%9f%97%93-daily-leetcode-progress-day-16/

MLA
" » đź—“ Daily LeetCode Progress – Day 16." Ertugrul | Sciencx - Monday September 1, 2025, https://www.scien.cx/2025/09/01/%f0%9f%97%93-daily-leetcode-progress-day-16/
HARVARD
Ertugrul | Sciencx Monday September 1, 2025 » đź—“ Daily LeetCode Progress – Day 16., viewed ,<https://www.scien.cx/2025/09/01/%f0%9f%97%93-daily-leetcode-progress-day-16/>
VANCOUVER
Ertugrul | Sciencx - » đź—“ Daily LeetCode Progress – Day 16. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/01/%f0%9f%97%93-daily-leetcode-progress-day-16/
CHICAGO
" » đź—“ Daily LeetCode Progress – Day 16." Ertugrul | Sciencx - Accessed . https://www.scien.cx/2025/09/01/%f0%9f%97%93-daily-leetcode-progress-day-16/
IEEE
" » đź—“ Daily LeetCode Progress – Day 16." Ertugrul | Sciencx [Online]. Available: https://www.scien.cx/2025/09/01/%f0%9f%97%93-daily-leetcode-progress-day-16/. [Accessed: ]
rf:citation
» đź—“ Daily LeetCode Progress – Day 16 | Ertugrul | Sciencx | https://www.scien.cx/2025/09/01/%f0%9f%97%93-daily-leetcode-progress-day-16/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.