This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Given a matrix of heights, returns coordinates of cells that can flow to both the Pacific and Atlantic oceans. | |
| * Pacific touches the left and top edges; Atlantic touches the right and bottom edges. | |
| * @param {number[][]} heights - 2D grid of elevation values | |
| * @return {number[][]} - List of coordinates [i, j] that can reach both oceans | |
| */ | |
| var pacificAtlantic = function(heights) { | |
| let row = heights.length; | |
| let col = heights[0].length; | |
| let arr = []; // Stores final result: cells that can reach both oceans |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} height | |
| * @return {number} | |
| */ | |
| var maxArea = function(height) { | |
| // Initialize two pointers: one at the start, one at the end of the array | |
| let left = 0; | |
| let right = height.length - 1; | |
| // Variable to keep track of the maximum area found so far |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} numBottles | |
| * @param {number} numExchange | |
| * @return {number} | |
| */ | |
| var maxBottlesDrunk = function(numBottles, numExchange) { | |
| // Track the total number of bottles drunk | |
| let totalDrunk = 0; | |
| // Track how many empty bottles we have after drinking |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var triangularSum = function(nums) { | |
| // Continue the process until only one element remains | |
| while (nums.length > 1) { | |
| let newNums = []; | |
| // Generate the next row by summing adjacent elements modulo 10 | |
| for (let i = 0; i < nums.length - 1; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} values | |
| * @return {number} | |
| */ | |
| var minScoreTriangulation = function(values) { | |
| const n = values.length; | |
| // Create a 2D DP table initialized with 0s | |
| // dp[i][j] will store the minimum triangulation score for the subpolygon from vertex i to vertex j. | |
| const dp = Array.from ({ length: n }, () => Array(n).fill(0)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Finds the largest perimeter of a triangle that can be formed from the given array of side lengths. | |
| * A triangle is valid if the sum of any two sides is greater than the third side. | |
| * | |
| * @param {number[]} nums - Array of positive integers representing side lengths. | |
| * @return {number} - The largest perimeter of a valid triangle, or 0 if none can be formed. | |
| */ | |
| var largestPerimeter = function(nums) { | |
| // Sort the array in descending order to try the largest sides first | |
| nums.sort((a, b) => b - a); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[][]} points | |
| * @return {number} | |
| */ | |
| var largestTriangleArea = function(points) { | |
| // Helper function to calculate the area of a triangle given three points | |
| function triangleArea(p1, p2, p3) { | |
| // Shoelace formula: | |
| // Area = 0.5 * |x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)| | |
| let [x1, y1] = p1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var triangleNumber = function(nums) { | |
| // Step 1: Sort the array to simplify triangle inequality checks | |
| nums.sort((a, b) => a - b); | |
| let count = 0; | |
| let n = nums.length; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[][]} triangle | |
| * @return {number} | |
| */ | |
| var minimumTotal = function(triangle) { | |
| // Start from the second-to-last row and move upward | |
| for (let row = triangle.length - 2; row >= 0; row--) { | |
| for (let col = 0; col < triangle[row].length; col++) { | |
| // For each element, choose the minimum of the two adjacent numbers in the row below | |
| // and add it to the current element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} numerator | |
| * @param {number} denominator | |
| * @return {string} | |
| */ | |
| var fractionToDecimal = function(numerator, denominator) { | |
| if (numerator === 0) return "0"; | |
| let result = ""; |