Given a pattern
and a string s
, find if s
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern
and a non-empty word in s
.
Example 1:
Example 2:
Example 3:
1 <= pattern.length <= 300
pattern
contains only lower-case English letters.1 <= s.length <= 3000
s
contains only lowercase English letters and spaces ' '
.s
does not contain any leading or trailing spaces.s
are separated by a single space./**
* @param {string} pattern
* @param {string} s
* @return {boolean}
*/
var wordPattern = function(pattern, s) {
const strArr = s.split(' ');
const keyMap = new Map();
const valueMap = new Map();
if (pattern.length !== strArr.length) {
return false;
}
for (let i = 0; i < pattern.length; i++) {
const key = pattern[i];
const value = strArr[i];
if ((keyMap.has(key) && keyMap.get(key) !== value) ||
(valueMap.has(value) && valueMap.get(value) !== key)) {
return false;
}
keyMap.set(key, value);
valueMap.set(value, key);
}
return true;
};
You are given an integer array cookies
, where cookies[i]
denotes the number of cookies in the ith
bag. You are also given an integer k
that denotes the number of children to distribute all the bags of cookies to. All the cookies in the same bag must go to the same child and cannot be split up.
The unfairness of a distribution is defined as the maximum total cookies obtained by a single child in the distribution.
Return the minimum unfairness of all distributions.
2 <= cookies.length <= 8
1 <= cookies[i] <= 10^5
2 <= k <= cookies.length
/**
* @param {number[]} cookies
* @param {number} k
* @return {number}
*/
var distributeCookies = function(cookies, k) {
const totalCookies = cookies.length;
const children = new Array(k).fill(0);
let answer = Infinity;
const backtracking = (index, children, cookies) => {
if (index === totalCookies) {
const maxCookie = Math.max(...children);
answer = Math.min(answer, maxCookie);
return;
}
for (let i = 0; i < k; i++) {
children[i] += cookies[index];
backtracking(index + 1, children, cookies);
children[i] -= cookies[index];
}
}
backtracking(0, children, cookies);
return answer;
};
Given the head
of a linked list and a value x
, partition it such that all nodes less than x
come before nodes greater than or equal to x
.
You should preserve the original relative order of the nodes in each of the two partitions.
[0, 200]
.-100 <= Node.val <= 100
-200 <= x <= 200
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} x
* @return {ListNode}
*/
var partition = function(head, x) {
let beforeNodes = new ListNode(0);
let afterNodes = new ListNode(0);
let beforeCurr = beforeNodes;
let afterCurr = afterNodes;
while (head) {
if (head.val < x) {
beforeCurr.next = head;
beforeCurr = beforeCurr.next;
} else {
afterCurr.next = head;
afterCurr = afterCurr.next;
}
head = head.next;
}
afterCurr.next = null;
beforeCurr.next = afterNodes.next;
return beforeNodes.next;
};
You are given the head
of a linked list, which contains a series of integers separated by 0
's. The beginning and end of the linked list will have Node.val == 0
.
For every two consecutive 0
's, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0
's.
Return the head
of the modified linked list.
[3, 2 * 10^5]
.0 <= Node.val <= 1000
Node.val == 0
.Node.val == 0
./**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var mergeNodes = function(head) {
const nodes = new ListNode(0);
let currNode = nodes;
let sum = 0;
head = head.next;
while (head) {
if (head.val > 0) {
sum += head.val;
} else {
const node = new ListNode(sum);
currNode.next = node;
currNode = currNode.next;
sum = 0;
}
head = head.next;
}
return nodes.next;
};
정보 감사합니다.