Skip to content

Commit 60000fa

Browse files
authored
Merge branch 'main' into gfg0100
2 parents f56aa3a + e79c454 commit 60000fa

File tree

5 files changed

+383
-0
lines changed

5 files changed

+383
-0
lines changed

deployment-app.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
# Unique key of the Deployment instance
5+
name: my-node-app
6+
spec:
7+
# 2 Pods should exist at all times.
8+
replicas: 2
9+
selector:
10+
matchLabels:
11+
app: node-app
12+
template:
13+
metadata:
14+
labels:
15+
# Apply this label to pods and default
16+
# the Deployment label selector to this value
17+
app: node-app
18+
spec:
19+
containers:
20+
- name: node-app
21+
# Run this image
22+
image: #put your image name which you push in dockerhub
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
id: shuffle-an-array
3+
title: Shuffle-an-Array
4+
sidebar_label: 0384-Shuffle an Array
5+
tags:
6+
- Leet code
7+
description: "Solution to leetocde 384"
8+
---
9+
10+
### Problem Description
11+
12+
Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.
13+
14+
Implement the Solution class:
15+
16+
- `Solution(int[] nums)` Initializes the object with the integer array nums.
17+
- `int[] reset()` Resets the array to its original configuration and returns it.
18+
- `int[] shuffle()` Returns a random shuffling of the array.
19+
20+
Example 1:
21+
22+
```
23+
Input
24+
["Solution", "shuffle", "reset", "shuffle"]
25+
[[[1, 2, 3]], [], [], []]
26+
Output
27+
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
28+
29+
Explanation
30+
Solution solution = new Solution([1, 2, 3]);
31+
solution.shuffle(); // Shuffle the array [1,2,3] and return its result.
32+
// Any permutation of [1,2,3] must be equally likely to be returned.
33+
// Example: return [3, 1, 2]
34+
solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3]
35+
solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]
36+
```
37+
38+
### Constraints:
39+
40+
- `1 <= nums.length <= 50`
41+
- `-10^6 <= nums[i] <= 10^6`
42+
43+
### Motive
44+
45+
Our motive in this question is to find a random permutation of an array on demand. Now this random permutation should be trully random with a uniform probability distribution.
46+
In other words, for any call to shuffle(), all permutations of the array should have equal probability of being returned.
47+
48+
Let's try doing it by hand. We take the elements of the array in a bag and shuffle them, then for each position in the shuffled array, we draw one element from the bag, and put that element in that position, and so on, until the bag is empty.
49+
We can simulate this exact process in our code by maintaining a copy of the original array and generating a random index of the array to put at each index.
50+
51+
But the problem arises in the exclusion of the selected elements from the bag. We could do a normal array delete `(or shift)` and that would cost us `O(n)` in the worst case, making our time complexity $O(n^2)$, but we can do better.
52+
53+
54+
### Code Implementation
55+
56+
**Python:**
57+
58+
```python
59+
class Solution(object):
60+
def __init__(self, nums):
61+
self.reset = lambda: nums
62+
self.shuffle = lambda: random.sample(nums, len(nums))
63+
```
64+
65+
**C++:**
66+
67+
```c++
68+
class Solution {
69+
vector<int> original;
70+
int n;
71+
public:
72+
73+
Solution(vector<int>& nums) {
74+
original = nums;
75+
n = original.size();
76+
}
77+
78+
vector<int> reset() {
79+
return original;
80+
}
81+
82+
vector<int> shuffle() {
83+
//make a copy of the original
84+
vector<int> shuffled = original;
85+
86+
int leftSize = n;
87+
for(int i = n-1; i>=0; i--) {
88+
//draw from the bag
89+
int j = rand()%leftSize;
90+
91+
//put this element at current position
92+
//and put the original element in the bag
93+
swap(shuffled[i], shuffled[j]);
94+
leftSize--;
95+
}
96+
return shuffled;
97+
}
98+
99+
};
100+
```
101+
102+
**Java:**
103+
104+
```java
105+
import java.util.Random;
106+
107+
public class Solution {
108+
private int[] nums;
109+
private Random random;
110+
111+
public Solution(int[] nums) {
112+
this.nums = nums;
113+
random = new Random();
114+
}
115+
116+
/** Resets the array to its original configuration and return it. */
117+
public int[] reset() {
118+
return nums;
119+
}
120+
121+
/** Returns a random shuffling of the array. */
122+
public int[] shuffle() {
123+
if(nums == null) return null;
124+
int[] a = nums.clone();
125+
for(int j = 1; j < a.length; j++) {
126+
int i = random.nextInt(j + 1);
127+
swap(a, i, j);
128+
}
129+
return a;
130+
}
131+
132+
private void swap(int[] a, int i, int j) {
133+
int t = a[i];
134+
a[i] = a[j];
135+
a[j] = t;
136+
}
137+
}
138+
```
139+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
id: lexicographical-numbers
3+
title: Lexicographical-Numbers
4+
sidebar_label: 0389-Lexicographical-Numbers
5+
tags:
6+
- Leet code
7+
description: "Solution to leetocde 386"
8+
---
9+
10+
### Problem Description
11+
12+
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order.
13+
14+
You must write an algorithm that runs in $O(n)$ time and uses $O(1)$ extra space.
15+
16+
Example 1:
17+
18+
```
19+
Input: n = 13
20+
Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
21+
22+
```
23+
24+
Example 2:
25+
26+
```
27+
Input: n = 2
28+
Output: [1,2]
29+
30+
```
31+
32+
### Constraints:
33+
34+
- `1 <= n <= 5 * 10^4`
35+
36+
### Algorithm
37+
38+
The basic idea is to find the next number to add.
39+
Take 45 for example: if the current number is 45, the next one will be `450 (450 == 45 * 10)(if 450 <= n)`, or `46 (46 == 45 + 1) (if 46 <= n)` or `5 (5 == 45 / 10 + 1)`(5 is less than 45 so it is for sure less than n).
40+
We should also consider `n = 600`, and the current number = 499, the next number is `5` because there are all "9"s after "4" in `"499"` so we should divide `499 by 10` until the last digit is not `"9"`.
41+
It is like a tree, and we are easy to get a sibling, a left most child and the parent of any node.
42+
43+
### Code Implementation
44+
45+
**Python:**
46+
47+
```python
48+
def lexicalOrder(self, n):
49+
top = 1
50+
while top * 10 <= n:
51+
top *= 10
52+
def mycmp(a, b, top=top):
53+
while a < top: a *= 10
54+
while b < top: b *= 10
55+
return -1 if a < b else b < a
56+
return sorted(xrange(1, n+1), mycmp)
57+
58+
```
59+
60+
**C++:**
61+
62+
```c++
63+
class Solution {
64+
public:
65+
vector<int> lexicalOrder(int n) {
66+
vector<int> res(n);
67+
int cur = 1;
68+
for (int i = 0; i < n; i++) {
69+
res[i] = cur;
70+
if (cur * 10 <= n) {
71+
cur *= 10;
72+
} else {
73+
if (cur >= n)
74+
cur /= 10;
75+
cur += 1;
76+
while (cur % 10 == 0)
77+
cur /= 10;
78+
}
79+
}
80+
return res;
81+
}
82+
};
83+
```
84+
85+
**Java:**
86+
87+
```java
88+
public List<Integer> lexicalOrder(int n) {
89+
List<Integer> list = new ArrayList<>(n);
90+
int curr = 1;
91+
for (int i = 1; i <= n; i++) {
92+
list.add(curr);
93+
if (curr * 10 <= n) {
94+
curr *= 10;
95+
} else if (curr % 10 != 9 && curr + 1 <= n) {
96+
curr++;
97+
} else {
98+
while ((curr / 10) % 10 == 9) {
99+
curr /= 10;
100+
}
101+
curr = curr / 10 + 1;
102+
}
103+
}
104+
return list;
105+
}
106+
```
107+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
id: kids-with-the-greatest-number-of-candies
3+
title: Kids With the Greatest Number of Candies
4+
level: easy
5+
sidebar_label: Kids With the Greatest Number of Candies
6+
tags:
7+
- Array
8+
- Greedy
9+
- Java
10+
description: "This document provides solutions for the Kids With the Greatest Number of Candies problem."
11+
---
12+
13+
## Problem Statement
14+
15+
There are `n` kids with candies. You are given an integer array `candies`, where each `candies[i]` represents the number of candies the ith kid has, and an integer `extraCandies`, denoting the number of extra candies that you have.
16+
17+
Return a boolean array `result` of length `n`, where `result[i]` is `true` if, after giving the ith kid all the `extraCandies`, they will have the greatest number of candies among all the kids, or `false` otherwise.
18+
19+
**Example 1:**
20+
21+
Input: `candies = [2,3,5,1,3]`, `extraCandies = 3`
22+
23+
Output: `[true,true,true,false,true]`
24+
25+
Explanation: If you give all `extraCandies` to:
26+
- Kid 1, they will have `2 + 3 = 5` candies, which is the greatest among the kids.
27+
- Kid 2, they will have `3 + 3 = 6` candies, which is the greatest among the kids.
28+
- Kid 3, they will have `5 + 3 = 8` candies, which is the greatest among the kids.
29+
- Kid 4, they will have `1 + 3 = 4` candies, which is not the greatest among the kids.
30+
- Kid 5, they will have `3 + 3 = 6` candies, which is the greatest among the kids.
31+
32+
**Example 2:**
33+
34+
Input: `candies = [4,2,1,1,2]`, `extraCandies = 1`
35+
36+
Output: `[true,false,false,false,false]`
37+
38+
Explanation: There is only `1` extra candy.
39+
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
40+
41+
**Example 3:**
42+
43+
Input: `candies = [12,1,12]`, `extraCandies = 10`
44+
45+
Output: `[true,false,true]`
46+
47+
**Constraints:**
48+
49+
- `n == candies.length`
50+
- `2 <= n <= 100`
51+
- `1 <= candies[i] <= 100`
52+
- `1 <= extraCandies <= 50`
53+
54+
## Solutions
55+
56+
### Approach
57+
58+
To determine if each kid can have the greatest number of candies after receiving `extraCandies`, follow these steps:
59+
60+
1. **Find the Maximum Candies:**
61+
- Traverse through the `candies` array to find the maximum number of candies any kid currently has.
62+
63+
2. **Check Each Kid's Condition:**
64+
- Iterate through the `candies` array again.
65+
- For each kid, check if adding `extraCandies` to their current candies makes their total candies equal to or greater than the maximum number found.
66+
- Store `true` if the condition is met, otherwise store `false`.
67+
68+
### Java
69+
70+
```java
71+
72+
class Solution {
73+
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
74+
int maxCandies = 0;
75+
for (int candy : candies) {
76+
maxCandies = Math.max(maxCandies, candy);
77+
}
78+
79+
List<Boolean> result = new ArrayList<>();
80+
for (int candy : candies) {
81+
result.add(candy + extraCandies >= maxCandies);
82+
}
83+
84+
return result;
85+
}
86+
87+
}
88+
```
89+
90+
### Python
91+
```Python
92+
class Solution:
93+
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
94+
maxCandies = max(candies)
95+
result = []
96+
97+
for candy in candies:
98+
result.append(candy + extraCandies >= maxCandies)
99+
100+
return result
101+
```

service-app.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: service-my-node-app
5+
spec:
6+
ports:
7+
- name: http
8+
port: 8080
9+
targetPort: 3000
10+
selector:
11+
# Loadbalance traffic across Pods matching
12+
# this label selector
13+
app: node-app
14+
type: LoadBalancer

0 commit comments

Comments
 (0)