diff --git a/LeetCodeNet.Tests/G0101_0200/S0129_sum_root_to_leaf_numbers/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0129_sum_root_to_leaf_numbers/SolutionTest.cs new file mode 100644 index 0000000..8634091 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0129_sum_root_to_leaf_numbers/SolutionTest.cs @@ -0,0 +1,20 @@ +namespace LeetCodeNet.G0101_0200.S0129_sum_root_to_leaf_numbers { + + using Xunit; + using System.Collections.Generic; + using LeetCodeNet.Com_github_leetcode; + + public class SolutionTest { + [Fact] + public void SumNumbers() { + var treeNode = TreeNode.Create(new List {1, 2, 3}); + Assert.Equal(25, new Solution().SumNumbers(treeNode)); + } + + [Fact] + public void SumNumbers2() { + var treeNode = TreeNode.Create(new List {4, 9, 0, 5, 1}); + Assert.Equal(1026, new Solution().SumNumbers(treeNode)); + } + } +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0130_surrounded_regions/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0130_surrounded_regions/SolutionTest.cs new file mode 100644 index 0000000..731c5ca --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0130_surrounded_regions/SolutionTest.cs @@ -0,0 +1,32 @@ +namespace LeetCodeNet.G0101_0200.S0130_surrounded_regions { + + using Xunit; + + public class SolutionTest { + [Fact] + public void Solve() { + char[][] board = new char[][] { + new char[] {'X', 'X', 'X', 'X'}, + new char[] {'X', 'O', 'O', 'X'}, + new char[] {'X', 'X', 'O', 'X'}, + new char[] {'X', 'O', 'X', 'X'} + }; + new Solution().Solve(board); + char[][] expected = new char[][] { + new char[] {'X', 'X', 'X', 'X'}, + new char[] {'X', 'X', 'X', 'X'}, + new char[] {'X', 'X', 'X', 'X'}, + new char[] {'X', 'O', 'X', 'X'} + }; + Assert.Equal(expected, board); + } + + [Fact] + public void Solve2() { + char[][] board = new char[][] {new char[] {'X'}}; + new Solution().Solve(board); + char[][] expected = new char[][] {new char[] {'X'}}; + Assert.Equal(expected, board); + } + } +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0133_clone_graph/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0133_clone_graph/SolutionTest.cs new file mode 100644 index 0000000..a6cbe38 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0133_clone_graph/SolutionTest.cs @@ -0,0 +1,32 @@ +namespace LeetCodeNet.G0101_0200.S0133_clone_graph { + + using Xunit; + using System.Collections.Generic; + + public class SolutionTest { + [Fact] + public void CloneGraph() { + var node1 = new Node(1); + var node2 = new Node(2); + var node3 = new Node(3); + var node4 = new Node(4); + var node1and2and4 = new Node(1, new List {node2, node4}); + var node2and1and3 = new Node(2, new List {node1, node3}); + var node3and2and4 = new Node(3, new List {node2, node4}); + var node4and1and3 = new Node(4, new List {node1, node3}); + var node = new Node(5, new List {node1and2and4, node2and1and3, node3and2and4, node4and1and3}); + Assert.Equal("[[2,4],[1,3],[2,4],[1,3]]", new Solution().CloneGraph(node).ToString()); + } + + [Fact] + public void CloneGraph2() { + var node1 = new Node(1); + Assert.Equal("[]", new Solution().CloneGraph(node1).ToString()); + } + + [Fact] + public void CloneGraph3() { + Assert.Null(new Solution().CloneGraph(null)); + } + } +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0134_gas_station/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0134_gas_station/SolutionTest.cs new file mode 100644 index 0000000..0a78b51 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0134_gas_station/SolutionTest.cs @@ -0,0 +1,16 @@ +namespace LeetCodeNet.G0101_0200.S0134_gas_station { + + using Xunit; + + public class SolutionTest { + [Fact] + public void CanCompleteCircuit() { + Assert.Equal(3, new Solution().CanCompleteCircuit(new int[] {1, 2, 3, 4, 5}, new int[] {3, 4, 5, 1, 2})); + } + + [Fact] + public void CanCompleteCircuit2() { + Assert.Equal(-1, new Solution().CanCompleteCircuit(new int[] {2, 3, 4}, new int[] {3, 4, 3})); + } + } +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0135_candy/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0135_candy/SolutionTest.cs new file mode 100644 index 0000000..259c9a7 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0135_candy/SolutionTest.cs @@ -0,0 +1,16 @@ +namespace LeetCodeNet.G0101_0200.S0135_candy { + + using Xunit; + + public class SolutionTest { + [Fact] + public void Candy() { + Assert.Equal(5, new Solution().Candy(new int[] {1, 0, 2})); + } + + [Fact] + public void Candy2() { + Assert.Equal(4, new Solution().Candy(new int[] {1, 2, 2})); + } + } +} diff --git a/LeetCodeNet/G0101_0200/S0129_sum_root_to_leaf_numbers/Solution.cs b/LeetCodeNet/G0101_0200/S0129_sum_root_to_leaf_numbers/Solution.cs new file mode 100644 index 0000000..3a58fa4 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0129_sum_root_to_leaf_numbers/Solution.cs @@ -0,0 +1,42 @@ +namespace LeetCodeNet.G0101_0200.S0129_sum_root_to_leaf_numbers { + +// #Medium #Depth_First_Search #Tree #Binary_Tree #Top_Interview_150_Binary_Tree_General +// #2025_07_12_Time_0_ms_(100.00%)_Space_41.52_MB_(70.15%) + +using LeetCodeNet.Com_github_leetcode; + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution { + private int sum = 0; + + public int SumNumbers(TreeNode root) { + RecurseSum(root, 0); + return sum; + } + + private void RecurseSum(TreeNode node, int curNum) { + if (node.left == null && node.right == null) { + sum += 10 * curNum + node.val.Value; + } else { + if (node.left != null) { + RecurseSum(node.left, 10 * curNum + node.val.Value); + } + if (node.right != null) { + RecurseSum(node.right, 10 * curNum + node.val.Value); + } + } + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0129_sum_root_to_leaf_numbers/readme.md b/LeetCodeNet/G0101_0200/S0129_sum_root_to_leaf_numbers/readme.md new file mode 100644 index 0000000..66a3401 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0129_sum_root_to_leaf_numbers/readme.md @@ -0,0 +1,39 @@ +129\. Sum Root to Leaf Numbers + +Medium + +You are given the `root` of a binary tree containing digits from `0` to `9` only. + +Each root-to-leaf path in the tree represents a number. + +* For example, the root-to-leaf path `1 -> 2 -> 3` represents the number `123`. + +Return _the total sum of all root-to-leaf numbers_. Test cases are generated so that the answer will fit in a **32-bit** integer. + +A **leaf** node is a node with no children. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg) + +**Input:** root = [1,2,3] + +**Output:** 25 + +**Explanation:** The root-to-leaf path `1->2` represents the number `12`. The root-to-leaf path `1->3` represents the number `13`. Therefore, sum = 12 + 13 = `25`. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg) + +**Input:** root = [4,9,0,5,1] + +**Output:** 1026 + +**Explanation:** The root-to-leaf path `4->9->5` represents the number 495. The root-to-leaf path `4->9->1` represents the number 491. The root-to-leaf path `4->0` represents the number 40. Therefore, sum = 495 + 491 + 40 = `1026`. + +**Constraints:** + +* The number of nodes in the tree is in the range `[1, 1000]`. +* `0 <= Node.val <= 9` +* The depth of the tree will not exceed `10`. \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0130_surrounded_regions/Solution.cs b/LeetCodeNet/G0101_0200/S0130_surrounded_regions/Solution.cs new file mode 100644 index 0000000..1d24208 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0130_surrounded_regions/Solution.cs @@ -0,0 +1,51 @@ +namespace LeetCodeNet.G0101_0200.S0130_surrounded_regions { + +// #Medium #Top_Interview_Questions #Array #Depth_First_Search #Breadth_First_Search #Matrix +// #Union_Find #Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search +// #Top_Interview_150_Graph_General #2025_07_12_Time_1_ms_(100.00%)_Space_64.38_MB_(92.86%) + +public class Solution { + public void Solve(char[][] board) { + if (board.Length == 0) { + return; + } + for (int i = 0; i < board[0].Length; i++) { + if (board[0][i] == 'O') { + Dfs(board, 0, i); + } + if (board[board.Length - 1][i] == 'O') { + Dfs(board, board.Length - 1, i); + } + } + for (int i = 0; i < board.Length; i++) { + if (board[i][0] == 'O') { + Dfs(board, i, 0); + } + if (board[i][board[0].Length - 1] == 'O') { + Dfs(board, i, board[0].Length - 1); + } + } + for (int i = 0; i < board.Length; i++) { + for (int j = 0; j < board[0].Length; j++) { + if (board[i][j] == 'O') { + board[i][j] = 'X'; + } + if (board[i][j] == '#') { + board[i][j] = 'O'; + } + } + } + } + + private void Dfs(char[][] board, int row, int column) { + if (row < 0 || row >= board.Length || column < 0 || column >= board[0].Length || board[row][column] != 'O') { + return; + } + board[row][column] = '#'; + Dfs(board, row + 1, column); + Dfs(board, row - 1, column); + Dfs(board, row, column + 1); + Dfs(board, row, column - 1); + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0130_surrounded_regions/readme.md b/LeetCodeNet/G0101_0200/S0130_surrounded_regions/readme.md new file mode 100644 index 0000000..6b12aaa --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0130_surrounded_regions/readme.md @@ -0,0 +1,30 @@ +130\. Surrounded Regions + +Medium + +Given an `m x n` matrix `board` containing `'X'` and `'O'`, _capture all regions that are 4-directionally surrounded by_ `'X'`. + +A region is **captured** by flipping all `'O'`s into `'X'`s in that surrounded region. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg) + +**Input:** board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]] + +**Output:** [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]] + +**Explanation:** Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically. + +**Example 2:** + +**Input:** board = [["X"]] + +**Output:** [["X"]] + +**Constraints:** + +* `m == board.length` +* `n == board[i].length` +* `1 <= m, n <= 200` +* `board[i][j]` is `'X'` or `'O'`. \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0133_clone_graph/Node.cs b/LeetCodeNet/G0101_0200/S0133_clone_graph/Node.cs new file mode 100644 index 0000000..1051b68 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0133_clone_graph/Node.cs @@ -0,0 +1,47 @@ +namespace LeetCodeNet.G0101_0200.S0133_clone_graph { +public class Node { + public int val; + public IList neighbors; + + public Node() { + val = 0; + neighbors = new List(); + } + + public Node(int _val) { + val = _val; + neighbors = new List(); + } + + public Node(int _val, List _neighbors) { + val = _val; + neighbors = _neighbors; + } + + public override string ToString() { + var result = new System.Text.StringBuilder(); + result.Append("["); + bool first = true; + foreach (var node in neighbors) { + if (!first) result.Append(","); + if (node.neighbors == null || node.neighbors.Count == 0) { + result.Append(node.val); + } else { + var inner = new System.Text.StringBuilder(); + inner.Append("["); + bool innerFirst = true; + foreach (var nodeItem in node.neighbors) { + if (!innerFirst) inner.Append(","); + inner.Append(nodeItem.val); + innerFirst = false; + } + inner.Append("]"); + result.Append(inner.ToString()); + } + first = false; + } + result.Append("]"); + return result.ToString(); + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0133_clone_graph/Solution.cs b/LeetCodeNet/G0101_0200/S0133_clone_graph/Solution.cs new file mode 100644 index 0000000..0ea6ab5 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0133_clone_graph/Solution.cs @@ -0,0 +1,55 @@ +namespace LeetCodeNet.G0101_0200.S0133_clone_graph { + +// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Udemy_Graph +// #Top_Interview_150_Graph_General #2025_07_12_Time_117_ms_(96.34%)_Space_47.31_MB_(97.95%) + +using System.Collections.Generic; + +/* +// Definition for a Node. +public class Node { + public int val; + public IList neighbors; + + public Node() { + val = 0; + neighbors = new List(); + } + + public Node(int _val) { + val = _val; + neighbors = new List(); + } + + public Node(int _val, List _neighbors) { + val = _val; + neighbors = _neighbors; + } +} +*/ + +public class Solution { + public Node CloneGraph(Node node) { + return CloneGraph(node, new Dictionary()); + } + + private Node CloneGraph(Node node, Dictionary processedNodes) { + if (node == null) { + return null; + } else if (processedNodes.ContainsKey(node)) { + return processedNodes[node]; + } + Node newNode = new Node(); + processedNodes[node] = newNode; + newNode.val = node.val; + newNode.neighbors = new List(); + foreach (Node neighbor in node.neighbors) { + Node clonedNeighbor = CloneGraph(neighbor, processedNodes); + if (clonedNeighbor != null) { + newNode.neighbors.Add(clonedNeighbor); + } + } + return newNode; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0133_clone_graph/readme.md b/LeetCodeNet/G0101_0200/S0133_clone_graph/readme.md new file mode 100644 index 0000000..97ec332 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0133_clone_graph/readme.md @@ -0,0 +1,69 @@ +133\. Clone Graph + +Medium + +Given a reference of a node in a **[connected](https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph)** undirected graph. + +Return a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) (clone) of the graph. + +Each node in the graph contains a value (`int`) and a list (`List[Node]`) of its neighbors. + +class Node { public int val; public List neighbors; } + +**Test case format:** + +For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with `val == 1`, the second node with `val == 2`, and so on. The graph is represented in the test case using an adjacency list. + +**An adjacency list** is a collection of unordered **lists** used to represent a finite graph. Each list describes the set of neighbors of a node in the graph. + +The given node will always be the first node with `val = 1`. You must return the **copy of the given node** as a reference to the cloned graph. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png) + +**Input:** adjList = [[2,4],[1,3],[2,4],[1,3]] + +**Output:** [[2,4],[1,3],[2,4],[1,3]] + +**Explanation:** + + There are 4 nodes in the graph. + 1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). + 2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). + 3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). + 4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/01/07/graph.png) + +**Input:** adjList = [[]] + +**Output:** [[]] + +**Explanation:** Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors. + +**Example 3:** + +**Input:** adjList = [] + +**Output:** [] + +**Explanation:** This an empty graph, it does not have any nodes. + +**Example 4:** + +![](https://assets.leetcode.com/uploads/2020/01/07/graph-1.png) + +**Input:** adjList = [[2],[1]] + +**Output:** [[2],[1]] + +**Constraints:** + +* The number of nodes in the graph is in the range `[0, 100]`. +* `1 <= Node.val <= 100` +* `Node.val` is unique for each node. +* There are no repeated edges and no self-loops in the graph. +* The Graph is connected and all nodes can be visited starting from the given node. \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0134_gas_station/Solution.cs b/LeetCodeNet/G0101_0200/S0134_gas_station/Solution.cs new file mode 100644 index 0000000..d662663 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0134_gas_station/Solution.cs @@ -0,0 +1,23 @@ +namespace LeetCodeNet.G0101_0200.S0134_gas_station { + +// #Medium #Top_Interview_Questions #Array #Greedy #Top_Interview_150_Array/String +// #2025_07_12_Time_0_ms_(100.00%)_Space_70.56_MB_(58.80%) + +public class Solution { + public int CanCompleteCircuit(int[] gas, int[] cost) { + int index = 0; + int total = 0; + int current = 0; + for (int i = 0; i < gas.Length; i++) { + int balance = gas[i] - cost[i]; + total += balance; + current += balance; + if (current < 0) { + index = i + 1; + current = 0; + } + } + return total >= 0 ? index : -1; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0134_gas_station/readme.md b/LeetCodeNet/G0101_0200/S0134_gas_station/readme.md new file mode 100644 index 0000000..18c57df --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0134_gas_station/readme.md @@ -0,0 +1,47 @@ +134\. Gas Station + +Medium + +There are `n` gas stations along a circular route, where the amount of gas at the ith station is `gas[i]`. + +You have a car with an unlimited gas tank and it costs `cost[i]` of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations. + +Given two integer arrays `gas` and `cost`, return _the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return_ `-1`. If there exists a solution, it is **guaranteed** to be **unique** + +**Example 1:** + +**Input:** gas = [1,2,3,4,5], cost = [3,4,5,1,2] + +**Output:** 3 + +**Explanation:** + + Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 + Travel to station 4. Your tank = 4 - 1 + 5 = 8 + Travel to station 0. Your tank = 8 - 2 + 1 = 7 + Travel to station 1. Your tank = 7 - 3 + 2 = 6 + Travel to station 2. Your tank = 6 - 4 + 3 = 5 + Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3. + Therefore, return 3 as the starting index. + +**Example 2:** + +**Input:** gas = [2,3,4], cost = [3,4,3] + +**Output:** -1 + +**Explanation:** + + You can't start at station 0 or 1, as there is not enough gas to travel to the next station. + Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 + Travel to station 0. Your tank = 4 - 3 + 2 = 3 + Travel to station 1. Your tank = 3 - 3 + 3 = 3 + You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3. + Therefore, you can't travel around the circuit once no matter where you start. + +**Constraints:** + +* `gas.length == n` +* `cost.length == n` +* 1 <= n <= 105 +* 0 <= gas[i], cost[i] <= 104 \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0135_candy/Solution.cs b/LeetCodeNet/G0101_0200/S0135_candy/Solution.cs new file mode 100644 index 0000000..0f2f614 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0135_candy/Solution.cs @@ -0,0 +1,94 @@ +namespace LeetCodeNet.G0101_0200.S0135_candy { + +// #Hard #Array #Greedy #Top_Interview_150_Array/String +// #2025_07_12_Time_1_ms_(99.02%)_Space_47.02_MB_(99.09%) + +using System; + +public class Solution { + private bool isFirstIteration; + + public enum RatingState { + increase, + decrease, + stable, + none + } + + private RatingState GetNewState(int prevRating, int currentRating) { + if (currentRating > prevRating) { + return RatingState.increase; + } + if (currentRating < prevRating) { + return RatingState.decrease; + } + return RatingState.stable; + } + + private void CountSum(RatingState currentState, ref int n, ref int totalCandies, ref int startNum) { + int res = 0; + switch (currentState) { + case (RatingState.increase): + if (!isFirstIteration) { + startNum++; + } + res = (2 * startNum + n - 1) * (n) / 2; + totalCandies += res; + startNum = startNum + n - 1; + break; + case (RatingState.decrease): + if (!isFirstIteration){ + startNum--; + } + var start = startNum - n + 1; + var end = startNum; + res = (end + start) * n / 2; + totalCandies += res; + if (start <= 0) { + start--; + int toRemove = 0; + if (isFirstIteration) + toRemove = -1; + totalCandies += (n + 1 + toRemove) * (-start); + } + else if (start > 1) { + totalCandies -= (start - 1) * (n); + } + startNum = 1; + break; + case (RatingState.stable): + totalCandies += n; + startNum = 1; + break; + case (RatingState.none): + return; + } + isFirstIteration = false; + n = 1; + } + + public int Candy(int[] ratings) { + if (ratings.Length == 1) { + return 1; + } + RatingState currentState = RatingState.none; + int prevRating = ratings[0]; + int totalCandies = 0; + int n = 2; + int startNum = 1; + isFirstIteration = true; + for (int i = 1; i < ratings.Length; i++) { + var newState = GetNewState(prevRating, ratings[i]); + prevRating = ratings[i]; + if (newState != currentState) { + CountSum(currentState, ref n, ref totalCandies, ref startNum); + currentState = newState; + } else { + n++; + } + } + CountSum(currentState, ref n, ref totalCandies, ref startNum); + return totalCandies; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0135_candy/readme.md b/LeetCodeNet/G0101_0200/S0135_candy/readme.md new file mode 100644 index 0000000..ad9d72a --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0135_candy/readme.md @@ -0,0 +1,34 @@ +135\. Candy + +Hard + +There are `n` children standing in a line. Each child is assigned a rating value given in the integer array `ratings`. + +You are giving candies to these children subjected to the following requirements: + +* Each child must have at least one candy. +* Children with a higher rating get more candies than their neighbors. + +Return _the minimum number of candies you need to have to distribute the candies to the children_. + +**Example 1:** + +**Input:** ratings = [1,0,2] + +**Output:** 5 + +**Explanation:** You can allocate to the first, second and third child with 2, 1, 2 candies respectively. + +**Example 2:** + +**Input:** ratings = [1,2,2] + +**Output:** 4 + +**Explanation:** You can allocate to the first, second and third child with 1, 2, 1 candies respectively. The third child gets 1 candy because it satisfies the above two conditions. + +**Constraints:** + +* `n == ratings.length` +* 1 <= n <= 2 * 104 +* 0 <= ratings[i] <= 2 * 104 \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy