From 76cd9cedd718387f3a0ae824dcbec6ee1f9e59a2 Mon Sep 17 00:00:00 2001 From: ThanhNIT <93962044+ThanhNIT@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:30:51 +0700 Subject: [PATCH] Added tasks 102-121 --- .../Com_github_leetcode/TreeUtils.cs | 33 ++++++++++++ .../SolutionTest.cs | 25 +++++++++ .../SolutionTest.cs | 20 +++++++ .../SolutionTest.cs | 25 +++++++++ .../SolutionTest.cs | 22 ++++++++ .../SolutionTest.cs | 17 ++++++ .../Solution.cs | 53 +++++++++++++++++++ .../readme.md | 30 +++++++++++ .../Solution.cs | 37 +++++++++++++ .../readme.md | 38 +++++++++++++ .../Solution.cs | 50 +++++++++++++++++ .../readme.md | 29 ++++++++++ .../Solution.cs | 51 ++++++++++++++++++ .../readme.md | 35 ++++++++++++ .../Solution.cs | 23 ++++++++ .../readme.md | 30 +++++++++++ 16 files changed, 518 insertions(+) create mode 100644 LeetCodeNet.Tests/Com_github_leetcode/TreeUtils.cs create mode 100644 LeetCodeNet.Tests/G0101_0200/S0102_binary_tree_level_order_traversal/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0101_0200/S0104_maximum_depth_of_binary_tree/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0101_0200/S0114_flatten_binary_tree_to_linked_list/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0101_0200/S0121_best_time_to_buy_and_sell_stock/SolutionTest.cs create mode 100644 LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/Solution.cs create mode 100644 LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/readme.md create mode 100644 LeetCodeNet/G0101_0200/S0104_maximum_depth_of_binary_tree/Solution.cs create mode 100644 LeetCodeNet/G0101_0200/S0104_maximum_depth_of_binary_tree/readme.md create mode 100644 LeetCodeNet/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.cs create mode 100644 LeetCodeNet/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md create mode 100644 LeetCodeNet/G0101_0200/S0114_flatten_binary_tree_to_linked_list/Solution.cs create mode 100644 LeetCodeNet/G0101_0200/S0114_flatten_binary_tree_to_linked_list/readme.md create mode 100644 LeetCodeNet/G0101_0200/S0121_best_time_to_buy_and_sell_stock/Solution.cs create mode 100644 LeetCodeNet/G0101_0200/S0121_best_time_to_buy_and_sell_stock/readme.md diff --git a/LeetCodeNet.Tests/Com_github_leetcode/TreeUtils.cs b/LeetCodeNet.Tests/Com_github_leetcode/TreeUtils.cs new file mode 100644 index 0000000..e51dbba --- /dev/null +++ b/LeetCodeNet.Tests/Com_github_leetcode/TreeUtils.cs @@ -0,0 +1,33 @@ +namespace LeetCodeNet.Com_github_leetcode { + +public class TreeUtils { + /* + * This method is to construct a normal binary tree. The input reads like + * this for [5, 3, 6, 2, 4, null, null, 1], i.e. preorder: + 5 + / \ + 3 6 + / \ / \ + 2 4 # # + / + 1 + */ + public static TreeNode ConstructBinaryTree(List treeValues) { + TreeNode root = new TreeNode(treeValues[0]); + Queue queue = new Queue(); + queue.Enqueue(root); + for (int i = 1; i < treeValues.Count; i++) { + TreeNode curr = queue.Dequeue(); + if (treeValues[i] != null) { + curr.left = new TreeNode(treeValues[i]); + queue.Enqueue(curr.left); + } + if (++i < treeValues.Count && treeValues[i] != null) { + curr.right = new TreeNode(treeValues[i]); + queue.Enqueue(curr.right); + } + } + return root; + } +} +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0102_binary_tree_level_order_traversal/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0102_binary_tree_level_order_traversal/SolutionTest.cs new file mode 100644 index 0000000..10b26ea --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0102_binary_tree_level_order_traversal/SolutionTest.cs @@ -0,0 +1,25 @@ +namespace LeetCodeNet.G0101_0200.S0102_binary_tree_level_order_traversal { + +using System; +using Xunit; +using Com_github_leetcode; + +public class SolutionTest { + [Fact] + public void LevelOrder1() { + TreeNode root = TreeUtils.ConstructBinaryTree(new List { 3, 9, 20, null, null, 15, 7 }); + Assert.Equal(new List> { new List { 3 }, new List { 9, 20 }, new List { 15, 7 } }, new Solution().LevelOrder(root)); + } + + [Fact] + public void LevelOrder2() { + TreeNode root = TreeUtils.ConstructBinaryTree(new List { 1 }); + Assert.Equal(new List> { new List { 1 } }, new Solution().LevelOrder(root)); + } + + [Fact] + public void LevelOrder3() { + Assert.Equal(new List> { }, new Solution().LevelOrder(null)); + } +} +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0104_maximum_depth_of_binary_tree/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0104_maximum_depth_of_binary_tree/SolutionTest.cs new file mode 100644 index 0000000..666aceb --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0104_maximum_depth_of_binary_tree/SolutionTest.cs @@ -0,0 +1,20 @@ +namespace LeetCodeNet.G0101_0200.S0104_maximum_depth_of_binary_tree { + +using System; +using Xunit; +using Com_github_leetcode; + +public class SolutionTest { + [Fact] + public void MaxDepth() { + TreeNode root = TreeUtils.ConstructBinaryTree(new List { 3, 9, 20, null, null, 15, 7 }); + Assert.Equal(3, new Solution().MaxDepth(root)); + } + + [Fact] + public void MaxDepth2() { + TreeNode root = TreeUtils.ConstructBinaryTree(new List { 1, null, 2 }); + Assert.Equal(2, new Solution().MaxDepth(root)); + } +} +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/SolutionTest.cs new file mode 100644 index 0000000..81bee77 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/SolutionTest.cs @@ -0,0 +1,25 @@ +namespace LeetCodeNet.G0101_0200.S0105_construct_binary_tree_from_preorder_and_inorder_traversal { + +using System; +using Xunit; +using LeetCodeNet.Com_github_leetcode; + +public class SolutionTest { + [Fact] + public void BuildTree() { + int[] preorder = { 3, 9, 20, 15, 7 }; + int[] inorder = { 9, 3, 15, 20, 7 }; + TreeNode actual = new Solution().BuildTree(preorder, inorder); + Assert.Equal("3,9,20,15,7", actual.ToString()); + } + + [Fact] + public void BuildTree2() { + int[] preorder = { -1 }; + int[] inorder = { -1 }; + TreeNode actual = new Solution().BuildTree(preorder, inorder); + Assert.Equal("-1", actual.ToString()); + } + +} +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0114_flatten_binary_tree_to_linked_list/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0114_flatten_binary_tree_to_linked_list/SolutionTest.cs new file mode 100644 index 0000000..743ec07 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0114_flatten_binary_tree_to_linked_list/SolutionTest.cs @@ -0,0 +1,22 @@ +namespace LeetCodeNet.G0101_0200.S0114_flatten_binary_tree_to_linked_list { + +using System; +using Xunit; +using LeetCodeNet.Com_github_leetcode; + +public class SolutionTest { + [Fact] + public void Flatten() { + TreeNode root = TreeUtils.ConstructBinaryTree(new List { 1, 2, 5, 3, 4, null, 6 }); + new Solution().Flatten(root); + Assert.Equal("1,null,2,null,3,null,4,null,5,null,6", root.ToString()); + } + + [Fact] + public void Flatten2() { + TreeNode root = TreeUtils.ConstructBinaryTree(new List { 0 }); + new Solution().Flatten(root); + Assert.Equal("0", root.ToString()); + } +} +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0121_best_time_to_buy_and_sell_stock/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0121_best_time_to_buy_and_sell_stock/SolutionTest.cs new file mode 100644 index 0000000..fc55f0a --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0121_best_time_to_buy_and_sell_stock/SolutionTest.cs @@ -0,0 +1,17 @@ +namespace LeetCodeNet.G0101_0200.S0121_best_time_to_buy_and_sell_stock { + +using System; +using Xunit; + +public class SolutionTest { + [Fact] + public void MaxProfit() { + Assert.Equal(5, new Solution().MaxProfit(new int[] { 7, 1, 5, 3, 6, 4 })); + } + + [Fact] + public void MaxProfit2() { + Assert.Equal(0, new Solution().MaxProfit(new int[] { 7, 6, 4, 3, 1 })); + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/Solution.cs b/LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/Solution.cs new file mode 100644 index 0000000..fceef36 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/Solution.cs @@ -0,0 +1,53 @@ +namespace LeetCodeNet.G0101_0200.S0102_binary_tree_level_order_traversal { + +// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Breadth_First_Search #Tree +// #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_1_Day_6_Tree #Udemy_Tree_Stack_Queue +// #Big_O_Time_O(N)_Space_O(N) #2024_01_09_Time_97_ms_(98.14%)_Space_47.4_MB_(12.94%) + +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 { + public IList> LevelOrder(TreeNode root) { + IList> result = new List>(); + if (root == null) { + return result; + } + Queue queue = new Queue(); + queue.Enqueue(root); + queue.Enqueue(null); + List level = new List(); + while (queue.Count > 0) { + root = queue.Dequeue(); + while (queue.Count > 0 && root != null) { + level.Add((int)root.val); + if (root.left != null) { + queue.Enqueue(root.left); + } + if (root.right != null) { + queue.Enqueue(root.right); + } + root = queue.Dequeue(); + } + result.Add(level); + level = new List(); + if (queue.Count > 0) { + queue.Enqueue(null); + } + } + return result; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/readme.md b/LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/readme.md new file mode 100644 index 0000000..92fe4a4 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/readme.md @@ -0,0 +1,30 @@ +102\. Binary Tree Level Order Traversal + +Medium + +Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level). + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) + +**Input:** root = [3,9,20,null,null,15,7] + +**Output:** [[3],[9,20],[15,7]] + +**Example 2:** + +**Input:** root = [1] + +**Output:** [[1]] + +**Example 3:** + +**Input:** root = [] + +**Output:** [] + +**Constraints:** + +* The number of nodes in the tree is in the range `[0, 2000]`. +* `-1000 <= Node.val <= 1000` \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0104_maximum_depth_of_binary_tree/Solution.cs b/LeetCodeNet/G0101_0200/S0104_maximum_depth_of_binary_tree/Solution.cs new file mode 100644 index 0000000..4face7f --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0104_maximum_depth_of_binary_tree/Solution.cs @@ -0,0 +1,37 @@ +namespace LeetCodeNet.G0101_0200.S0104_maximum_depth_of_binary_tree { + +// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search +// #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree +// #Programming_Skills_I_Day_10_Linked_List_and_Tree #Udemy_Tree_Stack_Queue +// #Big_O_Time_O(N)_Space_O(H) #2024_01_09_Time_65_ms_(93.31%)_Space_42.3_MB_(9.74%) + +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 { + public int MaxDepth(TreeNode root) { + return FindDepth(root, 0); + } + + private int FindDepth(TreeNode node, int currentDepth) { + if (node == null) { + return 0; + } + currentDepth++; + return 1 + + Math.Max(FindDepth(node.left, currentDepth), FindDepth(node.right, currentDepth)); + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0104_maximum_depth_of_binary_tree/readme.md b/LeetCodeNet/G0101_0200/S0104_maximum_depth_of_binary_tree/readme.md new file mode 100644 index 0000000..052ac01 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0104_maximum_depth_of_binary_tree/readme.md @@ -0,0 +1,38 @@ +104\. Maximum Depth of Binary Tree + +Easy + +Given the `root` of a binary tree, return _its maximum depth_. + +A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg) + +**Input:** root = [3,9,20,null,null,15,7] + +**Output:** 3 + +**Example 2:** + +**Input:** root = [1,null,2] + +**Output:** 2 + +**Example 3:** + +**Input:** root = [] + +**Output:** 0 + +**Example 4:** + +**Input:** root = [0] + +**Output:** 1 + +**Constraints:** + +* The number of nodes in the tree is in the range [0, 104]. +* `-100 <= Node.val <= 100` \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.cs b/LeetCodeNet/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.cs new file mode 100644 index 0000000..53bca8e --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.cs @@ -0,0 +1,50 @@ +namespace LeetCodeNet.G0101_0200.S0105_construct_binary_tree_from_preorder_and_inorder_traversal { + +// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Tree #Binary_Tree +// #Divide_and_Conquer #Data_Structure_II_Day_15_Tree #Big_O_Time_O(N)_Space_O(N) +// #2024_01_09_Time_53_ms_(99.83%)_Space_42.5_MB_(46.06%) + +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 j; + private Dictionary map = new Dictionary(); + + public int Get(int key) { + return map[key]; + } + + private TreeNode Answer(int[] preorder, int[] inorder, int start, int end) { + if (start > end || j > preorder.Length) { + return null; + } + int value = preorder[j++]; + int index = Get(value); + TreeNode node = new TreeNode(value); + node.left = Answer(preorder, inorder, start, index - 1); + node.right = Answer(preorder, inorder, index + 1, end); + return node; + } + + public TreeNode BuildTree(int[] preorder, int[] inorder) { + j = 0; + for (int i = 0; i < preorder.Length; i++) { + map.Add(inorder[i], i); + } + return Answer(preorder, inorder, 0, preorder.Length - 1); + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md b/LeetCodeNet/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md new file mode 100644 index 0000000..fa4a8b4 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md @@ -0,0 +1,29 @@ +105\. Construct Binary Tree from Preorder and Inorder Traversal + +Medium + +Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg) + +**Input:** preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] + +**Output:** [3,9,20,null,null,15,7] + +**Example 2:** + +**Input:** preorder = [-1], inorder = [-1] + +**Output:** [-1] + +**Constraints:** + +* `1 <= preorder.length <= 3000` +* `inorder.length == preorder.length` +* `-3000 <= preorder[i], inorder[i] <= 3000` +* `preorder` and `inorder` consist of **unique** values. +* Each value of `inorder` also appears in `preorder`. +* `preorder` is **guaranteed** to be the preorder traversal of the tree. +* `inorder` is **guaranteed** to be the inorder traversal of the tree. \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0114_flatten_binary_tree_to_linked_list/Solution.cs b/LeetCodeNet/G0101_0200/S0114_flatten_binary_tree_to_linked_list/Solution.cs new file mode 100644 index 0000000..8167fe9 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0114_flatten_binary_tree_to_linked_list/Solution.cs @@ -0,0 +1,51 @@ +namespace LeetCodeNet.G0101_0200.S0114_flatten_binary_tree_to_linked_list { + +// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Stack #Linked_List +// #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(N) +// #2024_01_09_Time_52_ms_(98.71%)_Space_41.3_MB_(6.68%) + +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 { + public void Flatten(TreeNode root) { + if (root != null) { + FindTail(root); + } + } + + private TreeNode FindTail(TreeNode root) { + TreeNode left = root.left; + TreeNode right = root.right; + TreeNode tail; + // find the tail of left subtree, tail means the most left leaf + if (left != null) { + tail = FindTail(left); + // stitch the right subtree below the tail + root.left = null; + root.right = left; + tail.right = right; + } else { + tail = root; + } + // find tail of the right subtree + if (tail.right == null) { + return tail; + } else { + return FindTail(tail.right); + } + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0114_flatten_binary_tree_to_linked_list/readme.md b/LeetCodeNet/G0101_0200/S0114_flatten_binary_tree_to_linked_list/readme.md new file mode 100644 index 0000000..ea101ac --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0114_flatten_binary_tree_to_linked_list/readme.md @@ -0,0 +1,35 @@ +114\. Flatten Binary Tree to Linked List + +Medium + +Given the `root` of a binary tree, flatten the tree into a "linked list": + +* The "linked list" should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`. +* The "linked list" should be in the same order as a [**pre-order** **traversal**](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR) of the binary tree. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg) + +**Input:** root = [1,2,5,3,4,null,6] + +**Output:** [1,null,2,null,3,null,4,null,5,null,6] + +**Example 2:** + +**Input:** root = [] + +**Output:** [] + +**Example 3:** + +**Input:** root = [0] + +**Output:** [0] + +**Constraints:** + +* The number of nodes in the tree is in the range `[0, 2000]`. +* `-100 <= Node.val <= 100` + +**Follow up:** Can you flatten the tree in-place (with `O(1)` extra space)? \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0121_best_time_to_buy_and_sell_stock/Solution.cs b/LeetCodeNet/G0101_0200/S0121_best_time_to_buy_and_sell_stock/Solution.cs new file mode 100644 index 0000000..a19a486 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0121_best_time_to_buy_and_sell_stock/Solution.cs @@ -0,0 +1,23 @@ +namespace LeetCodeNet.G0101_0200.S0121_best_time_to_buy_and_sell_stock { + +// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming +// #Data_Structure_I_Day_3_Array #Dynamic_Programming_I_Day_7 #Level_1_Day_5_Greedy #Udemy_Arrays +// #Big_O_Time_O(N)_Space_O(1) #2024_01_09_Time_328_ms_(35.43%)_Space_58.2_MB_(5.29%) + +public class Solution { + public int MaxProfit(int[] prices) { + int min = int.MaxValue; + int profit = 0; + int max = 0; + for (int i = 0; i < prices.Length; i++) { + if (prices[i] < min) { + min = prices[i]; + } + profit = prices[i] - min; + max = Math.Max(profit, max); + + } + return max; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0121_best_time_to_buy_and_sell_stock/readme.md b/LeetCodeNet/G0101_0200/S0121_best_time_to_buy_and_sell_stock/readme.md new file mode 100644 index 0000000..6e22ccf --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0121_best_time_to_buy_and_sell_stock/readme.md @@ -0,0 +1,30 @@ +121\. Best Time to Buy and Sell Stock + +Easy + +You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day. + +You want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock. + +Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`. + +**Example 1:** + +**Input:** prices = [7,1,5,3,6,4] + +**Output:** 5 + +**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. + +**Example 2:** + +**Input:** prices = [7,6,4,3,1] + +**Output:** 0 + +**Explanation:** In this case, no transactions are done and the max profit = 0. + +**Constraints:** + +* 1 <= prices.length <= 105 +* 0 <= prices[i] <= 104 \ No newline at end of file