Skip to content

Commit 9a1323c

Browse files
committed
new soln
1 parent e3e77a3 commit 9a1323c

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

148.SortList.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 148. Sort List
2+
// Given the head of a linked list, return the list after sorting it in ascending order.
3+
// Example 1:
4+
// Input: head = [4,2,1,3]
5+
// Output: [1,2,3,4]
6+
// Example 2:
7+
// Input: head = [-1,5,3,4,0]
8+
// Output: [-1,0,3,4,5]
9+
// Example 3:
10+
// Input: head = []
11+
// Output: []
12+
13+
/**
14+
* Definition for singly-linked list.
15+
* public class ListNode {
16+
* public int val;
17+
* public ListNode next;
18+
* public ListNode(int val=0, ListNode next=null) {
19+
* this.val = val;
20+
* this.next = next;
21+
* }
22+
* }
23+
*/
24+
public class Solution {
25+
public ListNode SortList(ListNode head) {
26+
List<int> list = new List<int>();
27+
ListNode temp = head;
28+
while(temp!=null){
29+
list.Add(temp.val);
30+
temp = temp.next;
31+
}
32+
list.Sort();
33+
temp = head;
34+
foreach(var item in list){
35+
temp.val = item;
36+
temp = temp.next;
37+
}
38+
return head;
39+
}
40+
}

2225.FindWinners.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// 2225. Find Players With Zero or One Losses
2+
// You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
3+
// Return a list answer of size 2 where:
4+
// answer[0] is a list of all players that have not lost any matches.
5+
// answer[1] is a list of all players that have lost exactly one match.
6+
// The values in the two lists should be returned in increasing order.
7+
// Note:
8+
// You should only consider the players that have played at least one match.
9+
// The testcases will be generated such that no two matches will have the same outcome.
10+
// Example 1:
11+
// Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
12+
// Output: [[1,2,10],[4,5,7,8]]
13+
// Explanation:
14+
// Players 1, 2, and 10 have not lost any matches.
15+
// Players 4, 5, 7, and 8 each have lost one match.
16+
// Players 3, 6, and 9 each have lost two matches.
17+
// Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
18+
// Example 2:
19+
// Input: matches = [[2,3],[1,3],[5,4],[6,4]]
20+
// Output: [[1,2,5,6],[]]
21+
// Explanation:
22+
// Players 1, 2, 5, and 6 have not lost any matches.
23+
// Players 3 and 4 each have lost two matches.
24+
// Thus, answer[0] = [1,2,5,6] and answer[1] = [].
25+
26+
public class Solution {
27+
public IList<IList<int>> FindWinners(int[][] matches) {
28+
Dictionary<int,int> stats = new Dictionary<int,int>();
29+
foreach(var item in matches){
30+
if(!stats.ContainsKey(item[0])){
31+
stats[item[0]] = 0;
32+
}
33+
if(stats.ContainsKey(item[1])){
34+
stats[item[1]]++;
35+
}
36+
else{
37+
stats[item[1]] = 1;
38+
}
39+
}
40+
IList<IList<int>> result = new List<IList<int>>();
41+
List<int> l1 = new List<int>();
42+
List<int> l2 = new List<int>();
43+
foreach(var item in stats){
44+
if(item.Value == 0)
45+
l1.Add(item.Key);
46+
if(item.Value == 1)
47+
l2.Add(item.Key);
48+
}
49+
l1.Sort();
50+
l2.Sort();
51+
result.Add(l1);
52+
result.Add(l2);
53+
return result;
54+
}
55+
}

692.TopKFrequent.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// 692. Top K Frequent Words
2+
// Given an array of strings words and an integer k, return the k most frequent strings.
3+
// Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
4+
// Example 1:
5+
// Input: words = ["i","love","leetcode","i","love","coding"], k = 2
6+
// Output: ["i","love"]
7+
// Explanation: "i" and "love" are the two most frequent words.
8+
// Note that "i" comes before "love" due to a lower alphabetical order.
9+
// Example 2:
10+
// Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
11+
// Output: ["the","is","sunny","day"]
12+
// Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
13+
14+
public class Solution {
15+
public IList<string> TopKFrequent(string[] words, int k) {
16+
Dictionary<string,int> freq = new Dictionary<string,int>();
17+
foreach(var item in words){
18+
if(freq.ContainsKey(item))
19+
freq[item]++;
20+
else
21+
freq[item] = 1;
22+
}
23+
List<string> res = freq.OrderByDescending(x => x.Value)
24+
.ThenBy(x => x.Key)
25+
.Select(x => x.Key).Take(k).ToList();
26+
return res;
27+
}
28+
}

994.OrangesRotting.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// 994. Rotting Oranges
2+
// You are given an m x n grid where each cell can have one of three values:
3+
// 0 representing an empty cell,
4+
// 1 representing a fresh orange, or
5+
// 2 representing a rotten orange.
6+
// Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.
7+
// Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
8+
// Example 1:
9+
// Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
10+
// Output: 4
11+
// Example 2:
12+
// Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
13+
// Output: -1
14+
// Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
15+
// Example 3:
16+
// Input: grid = [[0,2]]
17+
// Output: 0
18+
// Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
19+
20+
public class Solution {
21+
public int OrangesRotting(int[][] grid) {
22+
int time = 0;
23+
int countFresh = 0;
24+
int[] X = new int[]{1,-1,0,0};
25+
int[] Y = new int[]{0,0,1,-1};
26+
Queue<(int,int)> queue = new Queue<(int,int)>();
27+
for(int i = 0; i < grid.Length; i++){
28+
for(int j = 0; j < grid[0].Length; j++){
29+
if(grid[i][j] == 2)
30+
queue.Enqueue((i,j));
31+
else if(grid[i][j] == 1)
32+
countFresh++;
33+
}
34+
}
35+
if(countFresh == 0) return 0;
36+
while(queue.Count > 0){
37+
++time;
38+
int size = queue.Count();
39+
while(size > 0){
40+
(int x, int y) = queue.Dequeue();
41+
for(int i = 0; i < 4; i++){
42+
if(!IsValid(grid,X[i]+x,Y[i]+y))
43+
continue;
44+
grid[X[i]+x][Y[i]+y] = 2;
45+
queue.Enqueue((X[i]+x,Y[i]+y));
46+
countFresh--;
47+
}
48+
size--;
49+
}
50+
}
51+
52+
return countFresh == 0 ? time-1 : -1;
53+
}
54+
public bool IsValid(int[][]grid, int row, int col){
55+
if(row < 0 || col < 0 || row >= grid.Length || col >= grid[0].Length
56+
|| grid[row][col] == 0 || grid[row][col] == 2)
57+
return false;
58+
return true;
59+
}
60+
}

0 commit comments

Comments
 (0)