Người quan sát cô đơn
Senior Member
tức là cả bài dùng độc có 3 biến global thôi ấy hảdùng 3 biến thôi anh tài![]()
tức là cả bài dùng độc có 3 biến global thôi ấy hảdùng 3 biến thôi anh tài![]()
chuẩntức là cả bài dùng độc có 3 biến global thôi ấy hả![]()
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
def is_critical_point(p1, p2, p3):
return p1.val < p2.val > p3.val or p1.val > p2.val < p3.val
def find_first_critial_point_index(head):
if not head or not head.next:
return -1, None
p1, p2, p3 = head, head.next, head.next.next
counter = 0
while p3:
if is_critical_point(p1, p2, p3):
return counter + 1, p2
counter += 1
p1, p2, p3 = p2, p3, p3.next
return -1, None
i_1, head = find_first_critial_point_index(head)
off = i_1
min_d = max_d = -1
while head:
j, head = find_first_critial_point_index(head)
if head:
off += j
max_d = off - i_1
min_d = off - i_1 if min_d == -1 else min(min_d, j)
return [min_d, max_d]
class Solution {
public:
vector<int> nodesBetweenCriticalPoints(ListNode* head) {
int f[100001];
int ansMin = INT_MAX;
int i = 0, j = 0;
int prev = head->val;
head = head->next;
while(head->next != nullptr){
if ((head->val > prev && head->val > head->next->val) ||
(head->val < prev && head->val < head->next->val)){
f[i] = j;
i++;
}
prev = head->val;
j++;
head=head->next;
}
for(int j = 1; j < i; j++){
ansMin = min(ansMin, f[j] - f[j -1]);
}
if(i > 1){
return {ansMin, f[i-1] - f[0]};
}else return {-1, -1};
}
};
Bác làm mảng gì, techstack là gì mà thất nghiệp lâu thế
em fresher C# backendmới 2 tháng mà thím, tôi đợt lâu nhất tới 1 năm đây
Thím chắc làm C# backend hỉ

public class Solution {
public int[] NodesBetweenCriticalPoints(ListNode head) {
ListNode temp = head.next;
int prev_val = head.val;
int min = Int32.MaxValue;
int prev_crit_point = 0;
int pos = 1;
int first_crit_point = 0;
int last_crit_point = 0;
while(temp.next != null)
{
if((temp.val > prev_val && temp.val > temp.next.val) || (temp.val < prev_val && temp.val < temp.next.val))
{
if(first_crit_point == 0)
{
first_crit_point = pos;
last_crit_point = pos;
prev_crit_point = pos;
}
else
{
prev_crit_point = last_crit_point;
last_crit_point = pos;
}
}
if(prev_crit_point != last_crit_point)
{
if(last_crit_point - prev_crit_point < min)
min = last_crit_point - prev_crit_point;
}
pos++;
prev_val = temp.val;
temp = temp.next;
}
if(prev_crit_point != last_crit_point)
return new int[] {min, last_crit_point - first_crit_point};
else
return new int[] {-1, -1};
}
}
Bác này giỏi thật, thời điểm này kiếm được job không phải là dễđược thêm 1 chỗ offer nữa rồi mấy bác ơi, mừng quá trời mừng
em fresher C# backend
C#:public class Solution { public int[] NodesBetweenCriticalPoints(ListNode head) { ListNode temp = head.next; int prev_val = head.val; int min = Int32.MaxValue; int prev_crit_point = 0; int pos = 1; int first_crit_point = 0; int last_crit_point = 0; while(temp.next != null) { if((temp.val > prev_val && temp.val > temp.next.val) || (temp.val < prev_val && temp.val < temp.next.val)) { if(first_crit_point == 0) { first_crit_point = pos; last_crit_point = pos; prev_crit_point = pos; } else { prev_crit_point = last_crit_point; last_crit_point = pos; } } if(prev_crit_point != last_crit_point) { if(last_crit_point - prev_crit_point < min) min = last_crit_point - prev_crit_point; } pos++; prev_val = temp.val; temp = temp.next; } if(prev_crit_point != last_crit_point) return new int[] {min, last_crit_point - first_crit_point}; else return new int[] {-1, -1}; } }
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
current = head.next
ans = [100001,-1]
idx = 1
temp = [0,0]
previous_val = head.val
while current and current.next:
idx+=1
if (current.val > previous_val and current.val > current.next.val) or (current.val < previous_val and current.val < current.next.val):
if temp[0] == 0:
temp[0] = idx
else:
ans[1] = idx - temp[0]
ans[0] = min(ans[0], idx-temp[1])
temp[1] = idx
previous_val = current.val
current = current.next
if ans[0] == 100001:
ans[0] = -1
return ans
class Solution:
def passThePillow(self, n: int, time: int) -> int:
if (time // (n - 1)) % 2 == 0:
return time % (n - 1) + 1
else:
return n - (time % (n - 1))

public class Solution {
public int PassThePillow(int n, int time) {
if ((time / (n - 1) % 2 == 0)) return time % (n - 1) + 1;
return n - time % (n - 1);
}
}
return n - time % (n - 1) if (time // (n - 1)) % 2 == 1 else time % (n - 1) + 1
hồi tôi xài để chỉ định điểm patrol tiếp theo cho mấy thằng lính gác trong gamepublic class Solution
{
public int PassThePillow(int n, int time)
{
int twoTimeIndex = n * 2 - 2;
int remainder = time % twoTimeIndex;
return remainder < n ? remainder + 1 : twoTimeIndex - remainder + 1;
}
}

public class Solution {
public int PassThePillow(int n, int time) {
bool check = true;
int result = 1;
while(time > 0)
{
time--;
if(check)
result++;
else
result--;
if(result == n)
check = false;
if(result == 1)
check = true;
}
return result;
}
}
impl Solution {
pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
let (q, r) = (time / (n - 1), time % (n - 1));
if q % 2 == 0 {
1 + r
} else {
n - r
}
}
}
class Solution {
public int passThePillow(int n, int time) {
if(n==1) return 1;
time%=(2*n-2);
time+=1;//giây 1 trùng label 1 cho dễ nháp
if(time<=n) return time;
return n-(time-n);
}
}
class Solution {
/**
* @param Integer $n
* @param Integer $time
* @return Integer
*/
function passThePillow($n, $time) {
$holder = 1;
$rev = false; // check direction changes
while ($time) {
$holder += (!$rev) ? 1 : -1;
$rev = ($holder == $n || $holder == 1) ? !$rev : $rev;
$time--;
}
return $holder;
}
}
sameLoop
C#:public class Solution { public int PassThePillow(int n, int time) { bool check = true; int result = 1; while(time > 0) { time--; if(check) result++; else result--; if(result == n) check = false; if(result == 1) check = true; } return result; } }
class Solution {
public:
int passThePillow(int n, int time) {
while(time) {
for(int i = 1; i < n; i++) {
if(time == 0)
return i;
--time;
}
for(int i = n; i> 1; i--) {
if(time == 0)
return i;
--time;
}
}
return 1;
}
};
bài này thì trư giải dễ dàngcó phải dejavu ko chứ nhớ bài này gặp 1 lần trc đây rJava:class Solution { public int passThePillow(int n, int time) { if(n==1) return 1; time%=(2*n-2); time+=1;//giây 1 trùng label 1 cho dễ nháp if(time<=n) return time; return n-(time-n); } }![]()
cơm thêm cho ae lấy số:
class Solution {
public:
int numberOfChild(int n, int k) {
k %= (2 * n - 2);
if(k < n)
return k;
return (n - 1) - (k % (n - 1));
}
};
bài này thì trư giải dễ dàng![]()
C:class Solution { public: int numberOfChild(int n, int k) { k %= (2 * n - 2); if(k < n) return k; return (n - 1) - (k % (n - 1)); } };
class Solution {
public:
int passThePillow(int n, int time) {
return (time/(n-1)%2)?(n - time % (n -1)) : (time % (n - 1) + 1);
}
};
class Solution {
public:
int passThePillow(int n, int time) {
int currentPillowPosition = 1;
int currentTime = 0;
int direction = 1;
while (currentTime < time) {
if (0 < currentPillowPosition + direction &&
currentPillowPosition + direction <= n) {
currentPillowPosition += direction;
currentTime++;
} else {
// Reverse the direction if the next position is out of bounds
direction *= -1;
}
}
return currentPillowPosition;
}
};