【Lintcode】1785. Bank System
技術標籤:# 棧、佇列、串及其他資料結構java資料結構演算法leetcode
題目地址:
https://www.lintcode.com/problem/bank-system/description
要求設計一個銀行賬戶管理系統,實現下面三個函式:
1、void deposite(int id, int amount, long timestamp)
2、boolean withdraw(int id, int amount, long timestamp)
3、int[] check(int id, long startTime, long endTime)
第一個函式是在timestamp的時間向id的賬戶存amount這麼多錢,如果銀行裡沒有這個賬戶,則這個賬戶會被建立;第二個函式是在timestamp的時間從id的賬戶取amount這麼多錢,如果銀行裡沒有這個賬戶,或者有但是錢不夠,則會返回false,否則該賬戶的錢會減少amount,然後返回true;第三個函式是檢視id的賬戶在startTime和endTime這兩個時刻各自有多少錢,這兩個時刻檢視的錢都是這個時刻執行完存、取錢操作之後所剩的錢。如果check的時候該賬戶不存在,則返回一個長度
0
0
思路是對每個id的賬戶,開兩個列表,一個存操作的時間戳,另一個存每個時間戳對應的錢的數目。這樣通過雜湊表可以查詢id對應的賬戶,然後進行操作。對於check這個函式,在startTime有多少錢,實際上就是在timeStapms列表裡找到最後一個小於等於startTime的時間戳是什麼,這個時刻的錢就是startTime有多少錢;對於endTime也類似做。這可以用二分來查詢。程式碼如下:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BankSystem {
class Account {
private List<Integer> totalAmount;
private List<Long> timeStamps;
private int len;
public Account() {
totalAmount = new ArrayList<>();
timeStamps = new ArrayList< >();
}
public void deposite(int amount, long timeStamp) {
if (totalAmount.isEmpty()) {
totalAmount.add(amount);
} else {
totalAmount.add(totalAmount.get(len - 1) + amount);
}
timeStamps.add(timeStamp);
len++;
}
public boolean withdraw(int amount, long timeStamp) {
// 該賬戶不存在或者錢不夠,則返回false
if (totalAmount.isEmpty() || totalAmount.get(len - 1) < amount) {
return false;
}
totalAmount.add(totalAmount.get(len - 1) - amount);
timeStamps.add(timeStamp);
len++;
return true;
}
public int[] check(long start, long end) {
int[] res = new int[2];
int idx1 = binarySearch(start), idx2 = binarySearch(end);
res[0] = idx1 != -1 ? totalAmount.get(idx1) : 0;
res[1] = idx2 != -1 ? totalAmount.get(idx2) : 0;
return res;
}
private int binarySearch(long time) {
int l = 0, r = len - 1;
while (l < r) {
int m = l + (r - l + 1 >> 1);
if (timeStamps.get(m) <= time) {
l = m;
} else {
r = m - 1;
}
}
return timeStamps.get(l) <= time ? l : -1;
}
}
private Map<Integer, Account> map;
public BankSystem() {
// Write your code here
map = new HashMap<>();
}
/**
* @param id: user account id
* @param amount: the number of bank deposits
* @param timestamp: the data of bank transaction
* @return: nothing
*/
public void deposite(int id, int amount, long timestamp) {
// Write your code here
map.putIfAbsent(id, new Account());
map.get(id).deposite(amount, timestamp);
}
/**
* @param id: user account id
* @param amount : the number of bank withdraw
* @param timestamp: the data of bank transaction
* @return: if user account can not withdraw the number of amount,return false. else return true
*/
public boolean withdraw(int id, int amount, long timestamp) {
// Write your code here
if (!map.containsKey(id)) {
return false;
}
return map.get(id).withdraw(amount, timestamp);
}
/**
* @param id: user account id
* @param startTime: start time
* @param endTime: end time
* @return: need return two numbers,the first one is start time account balance,the second is end time account balance
*/
public int[] check(int id, long startTime, long endTime) {
// Write your code here
if (!map.containsKey(id)) {
return new int[0];
}
return map.get(id).check(startTime, endTime);
}
}
所有操作時間複雜度 O ( 1 ) O(1) O(1),空間取決於賬戶數量和時間戳數量。