Skip to content

Commit 79c5385

Browse files
committed
GRAPH, LinkList, Tree, Array
1 parent be8abaa commit 79c5385

16 files changed

+845
-1
lines changed

100.IsSameTree.cs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 100. Same Tree
2+
3+
// Given the roots of two binary trees p and q, write a function to check if they are the same or not.
4+
5+
// Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
6+
7+
8+
9+
// Example 1:
10+
11+
12+
// Input: p = [1,2,3], q = [1,2,3]
13+
// Output: true
14+
// Example 2:
15+
16+
17+
// Input: p = [1,2], q = [1,null,2]
18+
// Output: false
19+
20+
/**
21+
* Definition for a binary tree node.
22+
* public class TreeNode {
23+
* public int val;
24+
* public TreeNode left;
25+
* public TreeNode right;
26+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
27+
* this.val = val;
28+
* this.left = left;
29+
* this.right = right;
30+
* }
31+
* }
32+
*/
33+
public class Solution {
34+
public bool IsSameTree(TreeNode p, TreeNode q) {
35+
if(p==null && q==null)
36+
return true;
37+
if(p==null || q==null)
38+
return false;
39+
if(p.val != q.val)
40+
return false;
41+
return IsSameTree(p.left,q.left) && IsSameTree(p.right,q.right);
42+
}
43+
}

121.MaxProfit.cs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 121. Best Time to Buy and Sell Stock
2+
3+
// You are given an array prices where prices[i] is the price of a given stock on the ith day.
4+
5+
// 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.
6+
7+
// Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
8+
9+
10+
11+
// Example 1:
12+
13+
// Input: prices = [7,1,5,3,6,4]
14+
// Output: 5
15+
// Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
16+
// Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
17+
// Example 2:
18+
19+
// Input: prices = [7,6,4,3,1]
20+
// Output: 0
21+
// Explanation: In this case, no transactions are done and the max profit = 0.
22+
23+
public class Solution {
24+
public int MaxProfit(int[] prices) {
25+
int[] max = new int[prices.Length];
26+
max[prices.Length-1] = prices[prices.Length-1];
27+
for(int i=prices.Length-2;i>=0;i--){
28+
max[i] = Math.Max(max[i+1],prices[i]);
29+
}
30+
int profit = 0;
31+
for(int i=0;i<prices.Length-1;i++){
32+
if(prices[i]<max[i])
33+
profit = Math.Max(profit,max[i]-prices[i]);
34+
}
35+
return profit;
36+
}
37+
}

122.MaxProfit.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 122. Best Time to Buy and Sell Stock II
2+
3+
// You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
4+
5+
// On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
6+
7+
// Find and return the maximum profit you can achieve.
8+
9+
10+
11+
// Example 1:
12+
13+
// Input: prices = [7,1,5,3,6,4]
14+
// Output: 7
15+
// Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
16+
// Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
17+
// Total profit is 4 + 3 = 7.
18+
// Example 2:
19+
20+
// Input: prices = [1,2,3,4,5]
21+
// Output: 4
22+
// Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
23+
// Total profit is 4.
24+
// Example 3:
25+
26+
// Input: prices = [7,6,4,3,1]
27+
// Output: 0
28+
// Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
29+
30+
public class Solution {
31+
public int MaxProfit(int[] prices) {
32+
int profit = 0;
33+
for(int i=0;i<prices.Length-1;i++){
34+
if(prices[i]<prices[i+1])
35+
profit += prices[i+1] - prices[i];
36+
}
37+
return profit;
38+
}
39+
}

1319.MakeConnected.cs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// 1319. Number of Operations to Make Network Connected
2+
3+
// There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
4+
5+
// You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected.
6+
7+
// Return the minimum number of times you need to do this in order to make all the computers connected. If it is not possible, return -1.
8+
9+
10+
11+
// Example 1:
12+
13+
14+
// Input: n = 4, connections = [[0,1],[0,2],[1,2]]
15+
// Output: 1
16+
// Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.
17+
// Example 2:
18+
19+
20+
// Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
21+
// Output: 2
22+
// Example 3:
23+
24+
// Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
25+
// Output: -1
26+
// Explanation: There are not enough cables.
27+
28+
29+
public class Solution {
30+
public bool[] Visited = new bool[100000];
31+
public int MakeConnected(int n, int[][] connections)
32+
{
33+
if (connections.Length - (n - 1) < 0)
34+
return - 1;
35+
List<int>[] edges = new List<int>[n];
36+
for (int i = 0; i < n; i++)
37+
{
38+
edges[i] = new List<int>();
39+
}
40+
for (int i = 0; i < connections.Length; i++)
41+
{
42+
edges[connections[i][0]].Add(connections[i][1]);
43+
edges[connections[i][1]].Add(connections[i][0]);
44+
}
45+
int noOfConected = 0;
46+
for (int i = 0; i < n; i++)
47+
{
48+
if (!Visited[i])
49+
{
50+
connectedComponent(edges, i);
51+
noOfConected++;
52+
}
53+
}
54+
return noOfConected - 1;
55+
}
56+
57+
public void connectedComponent(List<int>[] edges, int src)
58+
{
59+
if (Visited[src])
60+
return;
61+
Visited[src] = true;
62+
foreach (var edge in edges[src])
63+
{
64+
if (!Visited[edge])
65+
connectedComponent(edges, edge);
66+
}
67+
}
68+
}

1971.ValidPath.cs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 1971. Find if Path Exists in Graph
2+
3+
// There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
4+
5+
// You want to determine if there is a valid path that exists from vertex source to vertex destination.
6+
7+
// Given edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.
8+
9+
10+
11+
// Example 1:
12+
13+
14+
// Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
15+
// Output: true
16+
// Explanation: There are two paths from vertex 0 to vertex 2:
17+
// - 0 → 1 → 2
18+
// - 0 → 2
19+
// Example 2:
20+
21+
22+
// Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
23+
// Output: false
24+
// Explanation: There is no path from vertex 0 to vertex 5.
25+
26+
public class Solution {
27+
public Dictionary<int, bool> visited = new Dictionary<int, bool>();
28+
public bool ValidPath(int n, int[][] edges, int source, int destination)
29+
{
30+
List<int>[] Path = new List<int>[n];
31+
for (int i = 0; i < n; i++)
32+
{
33+
Path[i] = new List<int>();
34+
visited.Add(i, false);
35+
}
36+
foreach (var item in edges)
37+
{
38+
Path[item[0]].Add(item[1]);
39+
Path[item[1]].Add(item[0]);
40+
}
41+
42+
return HasPath(Path, source, destination);
43+
}
44+
public bool HasPath(List<int>[] Path,
45+
int source, int destination)
46+
{
47+
if (source == destination)
48+
return true;
49+
if (visited[source])
50+
return false;
51+
visited[source] = true;
52+
foreach (var item in Path[source])
53+
{
54+
if (HasPath(Path, item, destination))
55+
return true;
56+
}
57+
return false;
58+
}
59+
}

200.NumIslands.cs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// 200. Number of Islands
2+
3+
// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
4+
5+
// An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
6+
7+
8+
9+
// Example 1:
10+
11+
// Input: grid = [
12+
// ["1","1","1","1","0"],
13+
// ["1","1","0","1","0"],
14+
// ["1","1","0","0","0"],
15+
// ["0","0","0","0","0"]
16+
// ]
17+
// Output: 1
18+
// Example 2:
19+
20+
// Input: grid = [
21+
// ["1","1","0","0","0"],
22+
// ["1","1","0","0","0"],
23+
// ["0","0","1","0","0"],
24+
// ["0","0","0","1","1"]
25+
// ]
26+
// Output: 3
27+
28+
public class Solution {
29+
bool[,] visited = new bool[300,300];
30+
public int NumIslands(char[][] grid)
31+
{
32+
33+
int count=0;
34+
for(int i = 0; i < grid.Length; i++)
35+
{
36+
for(int j = 0; j < grid[i].Length; j++)
37+
{
38+
if (grid[i][j] == '1' && !visited[i,j])
39+
{
40+
FindConectedPath(grid, i, j);
41+
count++;
42+
}
43+
}
44+
}
45+
return count;
46+
}
47+
public void FindConectedPath(char[][] grid, int i,int j)
48+
{
49+
if (i<0 ||j<0 || i>=grid.Length|| j >= grid[0].Length ||
50+
grid[i][j] == '0' || visited[i,j])
51+
{
52+
return;
53+
}
54+
55+
visited[i,j] = true;
56+
FindConectedPath(grid, i + 1, j);
57+
FindConectedPath(grid, i -1, j);
58+
FindConectedPath(grid, i , j-1);
59+
FindConectedPath(grid, i, j+1);
60+
}
61+
}

239.MaxSlidingWindow.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Return the max sliding window.
66

7-
//Deque with List
7+
//Deque implementation via List
88
public class Solution {
99
public int[] MaxSlidingWindow(int[] nums, int k) {
1010
int[] res = new int[nums.Length - k + 1];

3.LengthOfLongestSubstring.cs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// 3. Longest Substring Without Repeating Characters
2+
// Medium
3+
4+
// 28833
5+
6+
// 1229
7+
8+
// Add to List
9+
10+
// Share
11+
// Given a string s, find the length of the longest substring without repeating characters.
12+
13+
14+
15+
// Example 1:
16+
17+
// Input: s = "abcabcbb"
18+
// Output: 3
19+
// Explanation: The answer is "abc", with the length of 3.
20+
// Example 2:
21+
22+
// Input: s = "bbbbb"
23+
// Output: 1
24+
// Explanation: The answer is "b", with the length of 1.
25+
// Example 3:
26+
27+
// Input: s = "pwwkew"
28+
// Output: 3
29+
// Explanation: The answer is "wke", with the length of 3.
30+
// Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
31+
32+
public class Solution {
33+
public int LengthOfLongestSubstring(string s) {
34+
if (string.IsNullOrEmpty(s) )
35+
return 0;
36+
int[] freq = new int[95];
37+
int left,right;
38+
left = 0;
39+
right =0;
40+
int max = 0;
41+
while(right<s.Length){
42+
if(freq[s[right] - ' '] == 0){
43+
freq[s[right] - ' '] ++;
44+
}
45+
else{
46+
while(left < right && freq[s[right] - ' '] !=0){
47+
freq[s[left] - ' '] --;
48+
left++;
49+
}
50+
freq[s[right] - ' '] ++;
51+
}
52+
right++;
53+
max = Math.Max(max, (right-left));
54+
}
55+
return max;
56+
}
57+
}

0 commit comments

Comments
 (0)