Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5e58fb2

Browse filesBrowse files
authored
Merge pull request AlgoStudyGroup#183 from hellomrsun/master
add c# solution
2 parents 96169c5 + 8f50484 commit 5e58fb2
Copy full SHA for 5e58fb2

File tree

2 files changed

+86
-0
lines changed
Filter options

2 files changed

+86
-0
lines changed
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Solution {
2+
public void DeleteNode(ListNode node) {
3+
node.val = node.next.val;
4+
node.next = node.next.next;
5+
}
6+
}
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Solution: Binary search
2+
// Runtime: 208 ms, faster than 95.23% of C# online submissions for Random Pick with Weight.
3+
// Memory Usage: 47.2 MB, less than 5.10% of C# online submissions for Random Pick with Weight.
4+
public class Solution
5+
{
6+
private int sum = 0; //sum of all integers of array w
7+
private int[] arr; //new Array, its index means index, its value means weight
8+
private Random random = new Random();
9+
public Solution(int[] w)
10+
{
11+
arr = new int[w.Length];
12+
for (var i = 0; i < w.Length; i++)
13+
{
14+
sum += w[i];
15+
arr[i] = sum;
16+
}
17+
}
18+
19+
//Binary search
20+
public int PickIndex()
21+
{
22+
//Generate a random value between 0 and sum
23+
var randomValue = random.Next(0, sum) + 1;
24+
int left = 0, right = arr.Length - 1;
25+
while (left <= right)
26+
{
27+
int mid = left + (right - left) / 2;
28+
if (arr[mid] == randomValue)
29+
return mid;
30+
else if (arr[mid] > randomValue)
31+
right = mid - 1;
32+
else
33+
left = mid + 1;
34+
}
35+
36+
return left;
37+
}
38+
}
39+
40+
41+
// Solution: Linear search
42+
// Runtime: 400 ms, faster than 37.10% of C# online submissions for Random Pick with Weight.
43+
// Memory Usage: 47 MB, less than 5.10% of C# online submissions for Random Pick with Weight.
44+
public class Solution
45+
{
46+
private int sum = 0; //sum of all integers of array w
47+
private int[] arr; //new Array, its index means index, its value means weight
48+
private Random random = new Random();
49+
public Solution(int[] w)
50+
{
51+
arr = new int[w.Length];
52+
for (var i = 0; i < w.Length; i++)
53+
{
54+
sum += w[i];
55+
arr[i] = sum;
56+
}
57+
}
58+
59+
//Linear search
60+
public int PickIndex()
61+
{
62+
//Generate a random value between 0 and sum
63+
var randomeValue = random.Next(0, sum);
64+
65+
for (int i = 0; i < arr.Length; i++)
66+
{
67+
if (randomeValue < arr[i])
68+
{
69+
return i;
70+
}
71+
}
72+
return 0;
73+
}
74+
}
75+
76+
/**
77+
* Your Solution object will be instantiated and called as such:
78+
* Solution obj = new Solution(w);
79+
* int param_1 = obj.PickIndex();
80+
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.