I used a simple list comprehension to construct the result array in a clean and efficient way.
class Solution:
def buildArray(self, nums: List[int]) -> List[int]:
I used a simple list comprehension to construct the result array in a clean and efficient way.
class Solution:
def buildArray(self, nums: List[int]) -> List[int]:
I used dynamic programming to build a solution bottom-up. I defined dp[i] as the number of ways to tile a 2×i board. The recurrence relation came from understanding that I can add a vertical domino (dp[i-1]), a pair of horizontal dominoes or trominoes (dp[i-2]), and more complex tromino arrangements that require tracking prior gaps using a separate variable (dp[i-3] and beyond). I initialized the base cases for n = 1, 2, 3, and iteratively built up the solution to n. To avoid overflow, I applied modulo 10^9 + 7 at every step.
class Solution:
def numTilings(self, n: int) -> int:
To solve this, I realized that each domino can be uniquely represented by its sorted tuple form — for example, [2,1] becomes (1,2). This ensures that both [1,2] and [2,1] are considered equal. I then used a dictionary to count how many times each normalized domino appears. For each count greater than 1, I computed the number of unique pairs using the combination formula count * (count - 1) // 2, and summed these up to get the final answer.
class Solution:
When approaching this problem, I realized that only the first domino's top and bottom values could potentially be the unified value across the entire row. So I focused on checking if all dominoes can be rotated such that either tops or bottoms becomes full of just tops[0] or bottoms[0]. For each candidate, I counted how many rotations are needed to achieve this — both by rotating tops to match the candidate or bottoms to match the candidate. If either configuration works, I returned the minimum number of rotations. If not, I returned -1.
class Solution:
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
import os | |
from flask import Flask, render_template, request, jsonify | |
from google import genai | |
from google.genai import types | |
app = Flask(__name__) | |
PROJECT_ID = "build-with-vertex-ai" |
When solving this, I imagined the dominoes as experiencing "forces" from left and right. Each domino receives a force from the closest 'R' to its left or 'L' to its right. The closer the source, the stronger the force. I assigned a large positive value when a domino was pushed to the right ('R') and a large negative value when pushed to the left ('L'). Then I scanned the string twice:
Finally, I added the forces at each position:
To solve this, I just iterated through the nums array and for each number, I counted the number of digits using either len(str(num)) or logarithmic math. If the number of digits was even, I incremented a counter.
class Solution:
def findNumbers(self, nums: List[int]) -> int:
I iterated through the array with a right pointer and maintained a left pointer to denote the start of the current window. As I moved the right pointer, I tracked how many times the current max value appeared in the window.
Each time the count of the max value reached at least k, I knew that all subarrays ending at this right and starting from any index ≤ left would be valid. So I added (left + 1) to the result.
If the count of the max fell below k, I shrank the window from the left until the condition was satisfied again.
Used editiorial today for the Hard questions.